📜  Golang 中的字符串包

📅  最后修改于: 2021-10-25 03:02:46             🧑  作者: Mango

Go 语言提供了一个字符串包,其中包含不同类型的函数来操作 UTF-8 编码的字符串。要访问字符串包的函数,您需要借助 import 关键字在程序中导入一个字符串包。

Function Description
func Compare This function is used to return an integer comparing two strings lexicographically.
func Contains This function is used to check whether substr is within s or not.
func ContainsAny This function is used to check whether any Unicode code points in chars are within s or not.
func ContainsRune This function is used to check whether the Unicode code point r is within s or not.
func Count This function is used to count the number of non-overlapping instances of substr in given string s.
func EqualFold This function is used to check whether s and t, interpreted as UTF-8 strings, are equal under Unicode case-folding, which is a more general form of case-insensitivity or not.
func Fields This function is used to splits the given string s around each instance of one or more consecutive white space characters, as defined by unicode.IsSpace, returning a slice of substrings of s or an empty slice if s contains only white space.
func FieldsFunc This function is used to splits the string s at each run of Unicode code points c satisfying f(c) and returns an array of slices of s.
func HasPrefix This function is used to cehck whether the string s begins with prefix or not.
HasSuffix This function is used to check whether the string s ends with suffix or not.
func Index This function is used to returns the index of the first instance of substr in s or -1 if substr is not present in s.
func IndexAny This function is used to returns the index of the first instance of any Unicode code point from chars in s, or -1 if no Unicode code point from chars is present in s.
func IndexByte This function is used to returns the index of the first instance of c in s, or -1 if c is not present in s.
func IndexFunc This function is used to returns the index into s of the first Unicode code point satisfying f(c) or -1 if none do.
func IndexRune This function is used to returns the index of the first instance of the Unicode code point r, or -1 if rune is not present in s.
func Join This function is used to concatenate the elements of its first argument to create a single string.
func LastIndex This function is used to returns the index of the last instance of substr in s, or -1 if substr is not present in s.
func LastIndexAny This function is used to returns the index of the last instance of any Unicode code point from chars in s, or -1 if no Unicode code point from chars is present in s.
func LastIndexByte This function is used to returns the index of the last instance of c in s, or -1 if c is not present in s.
func LastIndexFunc This function is used to returns the index into s of the last Unicode code point satisfying f(c), or -1 if none do.
func Map This function is used to returns a copy of the string s with all its characters modified according to the mapping function.
func Repeat This function is used to returns a new string consisting of count copies of the string s.
func Replace This function is used to returns a copy of the string s with the first n non-overlapping instances of old replaced by new.
func ReplaceAll This function is used to returns a copy of the string s with all non-overlapping instances of old replaced by new.
func Split This function is used to slices s into all substrings separated by sep and returns a slice of the substrings between those separators.
func SplitAfter This function is used to slices s into all substrings after each instance of sep and returns a slice of those substrings.
func SplitAfterN This function is used to slices s into substrings after each instance of sep and returns a slice of those substrings.
func SplitN This function is used to slices s into substrings separated by sep and returns a slice of the substrings between those separators.
func Title This function is used to returns a copy of the string s with all Unicode letters that begin words mapped to their Unicode title case.
func ToLower This method is used to returns s with all Unicode letters mapped to their lower case.
func ToLowerSpecial This function is used to returns a copy of the string s with all Unicode letters mapped to their lower case using the case mapping specified by c.
func ToTitle This function is used to returns a copy of the string s with all Unicode letters mapped to their Unicode title case.
func ToTitleSpecial This function is used to returns a copy of the string s with all Unicode letters mapped to their Unicode title case, giving priority to the special casing rules.
func ToUpper This function is used to returns s with all Unicode letters mapped to their upper case.
func ToUpperSpecial This function is used to returns a copy of the string s with all Unicode letters mapped to their upper case using the case mapping specified by c.
func ToValidUTF8 This function is used to returns a copy of the string s with each run of invalid UTF-8 byte sequences replaced by the replacement string, which may be empty.
func Trim This function is used to returns a slice of the given string with all leading and trailing Unicode code points contained in cutset removed.
func TrimFunc This function is used to returns a slice of the given string with all leading and trailing Unicode code points c satisfying f(c) removed.
func TrimLeft This method is used to returns a slice of the given string with all leading Unicode code points contained in cutset removed.
func TrimLeftFunc This function is used to returns a slice of the given string with all leading Unicode code points c satisfying f(c) removed.
func TrimPrefix This function is used to returns s without the provided leading prefix string. If s doesn’t start with prefix, s is returned unchanged.
func TrimRight This function is used to returns a slice of the given string, with all trailing Unicode code points contained in cutset removed.
func TrimRightFunc This function is used to returns a slice of the given string with all trailing Unicode code points c satisfying f(c) removed.
func TrimSpace This function is used to returns a slice of given the string, with all leading and trailing white space removed, as defined by Unicode.
func TrimSuffix This function is used to returns s without the provided trailing suffix string.

类型生成器

Method Description
func (*Builder) Cap This method is used to returns the capacity of the builder’s underlying byte slice.
func (*Builder) Grow This method is used to grows b’s capacity if necessary, to guarantee space for another n bytes.
func (*Builder) Len This method is used to returns the number of accumulated bytes.
func (*Builder) Reset This method is used to resets the Builder to be empty.
func (*Builder) String This method is used to returns the accumulated string.
func (*Builder) Write This method is used to appends the contents of p to b’s buffer.
func (*Builder) WriteByte This method is used to appends the byte c to b’s buffer.
func (*Builder) WriteRune This method is used to appends the UTF-8 encoding of Unicode code point r to b’s buffer.
func (*Builder) WriteString This method is used to appends the contents of str to b’s buffer. It returns the length of str and a nil error.

类型阅读器

Method Description
func NewReader This function is used to returns a new Reader reading from s.
func (*Reader) Len This method is used to returns the number of bytes of the unread portion of the string.
func (*Reader) Reset This method is used to resets the Reader to be reading from s.
func (*Reader) Seek This method is used to implements the io.Seeker interface.
func (*Reader) Size This method is used to returns the original length of the underlying string.
func (*Reader) WriteTo This method is used to implements the io.WriterTo interface.

类型替换器

Method Description
func NewReplacer This function is used to returns a new Replacer from a list of old, new string pairs.
func (*Replacer) Replace This method is used to returns a copy of s with all replacements performed.
func (*Replacer) WriteString This method is used to writes s to w with all replacements performed.

示例 1:

// Golang program to illustrate the use of 
// the strings.Compare() Function 
package main 
    
import ( 
    "fmt"
    "strings"
) 
    
func main() { 
    
    var r1 = "Geeks"
    var r2 = "GeeksforGeeks"
    var r3 = "Geeks"
    
    // using the function 
    fmt.Println(strings.Compare(r1, r2)) 
    fmt.Println(strings.Compare(r2, r3)) 
    fmt.Println(strings.Compare(r3, r1)) 
    
} 

输出:

-1
1
0

示例 2:

// Golang program to illustrate 
// the strings.IndexAny() Function 
package main 
  
import ( 
    "fmt"
    "strings"
) 
  
// Main function 
func main() { 
  
    // using the function 
    fmt.Println(strings.IndexAny("Hey GFG?", "y")) 
      
} 

输出:

2