- `中文翻译请稍做等待`
- Strings
- explode
- Count
- Contains
- ContainsAny
- ContainsRune
- LastIndex
- IndexByte
- IndexRune
- IndexAny
- LastIndexAny
- LastIndexByte
- SplitN
- SplitAfterN
- Split
- SplitAfter
- Fields
- FieldsFunc
- Join
- HasPrefix
- HasSuffix
- Map
- Repeat
- ToUpper
- ToLower
- ToTitle
- ToUpperSpecial
- ToLowerSpecial
- ToTitleSpecial
- ToValidUTF8
- Title
- TrimLeftFunc
- TrimRightFunc
- TrimFunc
- IndexFunc
- LastIndexFunc
- makeCutsetFunc
- Trim
- TrimLeft
- TrimRight
- TrimSpace
- TrimPrefix
- TrimSuffix
- Replace
- ReplaceAll
- EqualFold
- Index
Countexplode splits s into a slice of UTF-8 strings,
one string per Unicode character up to a maximum of n (n < 0 means no limit).
Invalid UTF-8 sequences become correct encodings of U+FFFD.
统计字符串 substr 在字符串 s 里出现的次数,如果substr参数为""(空串),则返回 1 + s的长度
// Count counts the number of non-overlapping instances of substr in s.
// If substr is an empty string, Count returns 1 + the number of Unicode code points in s.
使用方法:count :=strings.Count(s, substr string)
例如:"l"在"allen"出现2次
func main() {
str1 := "allen"
substr := "l"
count := strings.Count(str1, substr)
fmt.Print(count, "n")
}
Contains
判断字符串 s 是否包含 substr
// Contains reports whether substr is within s.
使用方法:include := strings.Contains(s, substr string)
例如:"allen"里包含了字符串"al",返回 布尔值 true
func main() {
str1 := "allen"
substr := "al"
include := strings.Contains(str1, substr)
fmt.Print(include, "n")
}
ContainsAny
判断字符串 substr 中任何一个字母是否存在于字符串 s 中
// ContainsAny reports whether any Unicode code points in chars are within s.
使用方法:include := strings.ContainsAny(s, chars string)
例如:
字符串"lsp"中的字母"l"存在于"allen"中,返回布尔值 true
字符串"sp"中没有存在于"allen"中的字母,返回布尔值 false
func main() {
str1 := "allen"
substr := "ses"
include := strings.ContainsAny(str1, substr)
fmt.Print(include, "n")
}
注:存在一个或多个都为true,完全不存在才是 false
ContainsRune
判断 符文 s 指定的字符是否存在于字符串 substr 中
// ContainsRune reports whether the Unicode code point r is within s.
使用方法:include := strings.ContainsRune(s string, r rune)
例如:符文'a'存在于字符串"allen"中,返回布尔值 true
func main() {
str1 := "allen"
include := strings.ContainsRune(str1, 'a')
fmt.Print(include, "n")
}
注:符文'a'是单引号,对应ASCII码表的十进制 97 ,使用fmt.Print('a')打印结果为 97
LastIndex
LastIndex返回 s 中 substr 的最后一个实例的索引,如果 s 中不存在substr,则返回-1
// LastIndex returns the index of the last instance of substr in s, or -1 if substr is not present in s.
使用方法:index := strings.LastIndex(s, substr string)
例如:
字符串"en"存在于字符串"allen"中,索引为3
字符串"l"存在于字符串"allen"中,索引为2(最后一个实例的位置)
func main() {
str1 := "allen"
substr := "en"
index := strings.LastIndex(str1, substr)
fmt.Print(index, "n")
}
IndexByte
IndexByte返回 s 中 c 的第一个实例的索引,如果s中不存在c,则返回-1。
// IndexByte returns the index of the first instance of c in s, or -1 if c is not present in s.
使用方法:index := strings.IndexByte(s string, c byte)
例如:符文'l'存在于字符串"allen"中,返回第一个实例索引1
func main() {
str1 := "allen"
index := strings.IndexByte(str1, 'l')
fmt.Print(index, "n")
}
IndexRune
// IndexRune returns the index of the first instance of the Unicode code point // r, or -1 if rune is not present in s. // If r is utf8.RuneError, it returns the first instance of any // invalid UTF-8 byte sequence.IndexAny
// IndexAny 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.LastIndexAny
// LastIndexAny 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.LastIndexByte
// LastIndexByte returns the index of the last instance of c in s, or -1 if c is not present in s.SplitN
// SplitN slices s into substrings separated by sep and returns a slice of // the substrings between those separators. // // The count determines the number of substrings to return: // n > 0: at most n substrings; the last substring will be the unsplit remainder. // n == 0: the result is nil (zero substrings) // n < 0: all substrings // // Edge cases for s and sep (for example, empty strings) are handled // as described in the documentation for Split.SplitAfterN
// SplitAfterN slices s into substrings after each instance of sep and // returns a slice of those substrings. // // The count determines the number of substrings to return: // n > 0: at most n substrings; the last substring will be the unsplit remainder. // n == 0: the result is nil (zero substrings) // n < 0: all substrings // // Edge cases for s and sep (for example, empty strings) are handled // as described in the documentation for SplitAfter.Split
// Split slices s into all substrings separated by sep and returns a slice of // the substrings between those separators. // // If s does not contain sep and sep is not empty, Split returns a // slice of length 1 whose only element is s. // // If sep is empty, Split splits after each UTF-8 sequence. If both s // and sep are empty, Split returns an empty slice. // // It is equivalent to SplitN with a count of -1.SplitAfter
// SplitAfter slices s into all substrings after each instance of sep and // returns a slice of those substrings. // // If s does not contain sep and sep is not empty, SplitAfter returns // a slice of length 1 whose only element is s. // // If sep is empty, SplitAfter splits after each UTF-8 sequence. If // both s and sep are empty, SplitAfter returns an empty slice. // // It is equivalent to SplitAfterN with a count of -1.Fields
// Fields splits the 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.FieldsFunc
// Fields###### splits the string s at each run of Unicode code points c satisfying f(c) // and returns an array of slices of s. If all code points in s satisfy f(c) or the // string is empty, an empty slice is returned. // // Fields###### makes no guarantees about the order in which it calls f(c) // and assumes that f always returns the same value for a given c.Join
// Join concatenates the elements of its first argument to create a single string. The separator // string sep is placed between elements in the resulting string.HasPrefix
// HasPrefix tests whether the string s begins with prefix.HasSuffix
// HasSuffix tests whether the string s ends with suffix.Map
// Map returns a copy of the string s with all its characters modified // according to the mapping function. If mapping returns a negative value, the character is // dropped from the string with no replacement.Repeat
Repeat返回一个新字符串,该字符串由字符串s的多个副本组成
// Repeat returns a new string consisting of count copies of the string s.
//
// It panics if count is negative or if
// the result of (len(s) * count) overflows.
使用方法:strings.Repeat(s string, count int)
例如:
可以理解成3个"aaaba"拼接成"aaabaaaabaaaaba"
如果第二个参数为2的话就是"aaabaaaaba"
func main() {
res := strings.Repeat("aaaba", 3)
fmt.Println(res)
}
ToUpper
// ToUpper returns s with all Unicode letters mapped to their upper case.ToLower
// ToLower returns s with all Unicode letters mapped to their lower case.ToTitle
// ToTitle returns a copy of the string s with all Unicode letters mapped to // their Unicode title case.ToUpperSpecial
// ToUpperSpecial returns a copy of the string s with all Unicode letters mapped to their // upper case using the case mapping specified by c.ToLowerSpecial
// ToLowerSpecial returns a copy of the string s with all Unicode letters mapped to their // lower case using the case mapping specified by c.ToTitleSpecial
// ToTitleSpecial returns a copy of the string s with all Unicode letters mapped to their // Unicode title case, giving priority to the special casing rules.ToValidUTF8
// ToValidUTF8 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.Title
// Title returns a copy of the string s with all Unicode letters that begin words // mapped to their Unicode title case. // // BUG(rsc): The rule Title uses for word boundaries does not handle Unicode punctuation properly.TrimLeftFunc
// TrimLeft## returns a slice of the string s with all leading // Unicode code points c satisfying f(c) removed.TrimRightFunc
// TrimRight## returns a slice of the string s with all trailing // Unicode code points c satisfying f(c) removed.TrimFunc
// Trim###### returns a slice of the string s with all leading // and trailing Unicode code points c satisfying f(c) removed.IndexFunc
// Index###### returns the index into s of the first Unicode // code point satisfying f(c), or -1 if none do.LastIndexFunc
// LastIndex###### returns the index into s of the last // Unicode code point satisfying f(c), or -1 if none do.makeCutsetFunc Trim
// Trim returns a slice of the string s with all leading and // trailing Unicode code points contained in cutset removed.TrimLeft
// TrimLeft returns a slice of the string s with all leading // Unicode code points contained in cutset removed. // // To remove a prefix, use TrimPrefix instead.TrimRight
// TrimRight returns a slice of the string s, with all trailing // Unicode code points contained in cutset removed. // // To remove a suffix, use TrimSuffix instead.TrimSpace
// TrimSpace returns a slice of the string s, with all leading // and trailing white space removed, as defined by Unicode.TrimPrefix
// TrimPrefix returns s without the provided leading prefix string. // If s doesn't start with prefix, s is returned unchanged.TrimSuffix
// TrimSuffix returns s without the provided trailing suffix string. // If s doesn't end with suffix, s is returned unchanged.Replace
// Replace returns a copy of the string s with the first n // non-overlapping instances of old replaced by new. // If old is empty, it matches at the beginning of the string // and after each UTF-8 sequence, yielding up to k+1 replacements // for a k-rune string. // If n < 0, there is no limit on the number of replacements.ReplaceAll
// ReplaceAll returns a copy of the string s with all // non-overlapping instances of old replaced by new. // If old is empty, it matches at the beginning of the string // and after each UTF-8 sequence, yielding up to k+1 replacements // for a k-rune string.EqualFold
// EqualFold reports whether s and t, interpreted as UTF-8 strings, // are equal under Unicode case-folding, which is a more general // form of case-insensitivity.Index
Index返回s中substr的第一个实例的索引,如果s中不存在substr,则返回-1。
// Index returns the index of the first instance of substr in s, or -1 if substr is not present in s.
使用方法:strings.Index(s, substr string)
例如:
substr存在于s中,开始的索引位置为1
func main() {
res := strings.Index("ababa", "baba")
fmt.Println(res)
}



