This is a creation in Article, where the information may have evolved or changed.
This article is reproduced, original: Golang study notes (3)--string manipulation
1, Contains
func Contains(s, substr string) bool
This function is to find out whether a character exists in the string and returns True.
package mainimport "fmt"import "strings"func main(){ fmt.Println(strings.Contains("chain", "ch")) fmt.Println(strings.Contains("ch", "chain")) fmt.Println(strings.Contains("chain", "")) //true fmt.Println(strings.Contains("", "")) //true 这里要特别注意 fmt.Println(strings.Contains("我是中国人", "我")) //true}
Operation Result:
2, Containsany
func ContainsAny(s, chars string) bool
This is whether the query string contains more than one character.
package mainimport "fmt"import "strings"func main(){ fmt.Println(strings.ContainsAny("chain", "b")) // false fmt.Println(strings.ContainsAny("chain", "c & i")) // true fmt.Println(strings.ContainsAny("chain", "")) // false fmt.Println(strings.ContainsAny("", "")) // false}
Operation Result:
3, Containsrune
func ContainsRune(s string, r rune) bool
, the side of course is whether the string contains the rune type, where the rune type is UTF8. Runecountstring can fully represent the type of all Unicode characters
package mainimport "fmt"import "strings"func main(){ fmt.Println(strings.ContainsRune("chain", rune('c'))) //true fmt.Println(strings.ContainsRune("chain", 99)) //true fmt.Println(strings.Contains("我是中国人", "我")) //true}
Operation Result:
Where 99 is the Unicode encoding of C
4. Count
func Count(s, sep string) int
The function of this is to output the number of characters that match in a string.
package mainimport "fmt"import "strings"func main(){ fmt.Println(strings.Count("chainn", "nn")) //1 fmt.Println(strings.Count("chainn", "n")) //2 fmt.Println(strings.Count("chain", "")) // before & after each rune result:6}
Operation Result:
5. Index
func Index(s, sep string) int
This function is to find the string, then return the current position, enter the string type, and then the position information of Int.
package mainimport "fmt"import "strings"func main(){ fmt.Println(strings.Index("chain", "h")) //1 fmt.Println(strings.Index("chainn", "n")) //4 fmt.Println(strings.Index("chainn", "q")) //4 fmt.Println(strings.Index("我是中国人", "中")) // 返回 6}}
Operation Result:
Note :
Index is counted starting at 0
6, Indexany
func IndexAny(s, chars string) int
This function is the same as the lookup, where the string first appears, and returns 1 if it does not exist.
package mainimport "fmt"import "strings"func main(){ fmt.Println(strings.IndexAny("chainn", "n")) //4 fmt.Println(strings.IndexAny("我是中国人", "中")) // 在存在返回 6 fmt.Println(strings.IndexAny("我是中国人", "和")) // 在存在返回 -1}
Operation Result:
7, Indexbyte
func IndexByte(s string, c byte) int
, this function still finds the position of the first thick line, except that this time C is of type Byte, it finds the return position and cannot find the return-1.
package mainimport "fmt"import "strings"func main(){ fmt.Println(strings.IndexByte("hello chain", 'c')) //6 fmt.Println(strings.IndexByte("hello chain", 'b')) //-1 //fmt.Println(strings.IndexAny("我是中国人", "中")) // 编译报错}
Operation Result:
8, Indexrune
func IndexRune(s string, r rune) int
, or find a location, except this time it's rune type.
package mainimport "fmt"import "strings"func main(){ fmt.Println(strings.IndexRune("chain", rune('c'))) //0 fmt.Println(strings.IndexRune("chain", 's')) // -1 fmt.Println(strings.IndexRune("我是中国人", '中')) //6}
Operation Result:
9, Indexfunc
func IndexFunc(s string, f func(rune) bool) int
This function everyone knows, is through the type of conversion to use the function to find the location, we come to the code to see ha.
package mainimport "fmt"import "strings"func main() { fmt.Println(strings.IndexFunc("chaina", split)) //2 fmt.Println(strings.IndexFunc("chbin", split)) //-1}func split(r rune) bool { if r == 'a' { return true } return false}
10, LastIndex
func LastIndex(s, sep string) int
See this everyone may also understand the search is the last position, just opposite the index.
package mainimport "fmt"import "strings"func main() { fmt.Println(strings.LastIndex("chaina", "a")) // 5}
Operation Result:
11, Lastindexany
func LastIndexAny(s, chars string) int
This is the opposite of Indexany, and the last one to find
package mainimport "fmt"import "strings"func main() { fmt.Println(strings.LastIndexAny("chaina", "a")) // 5}
Operation Result:
12, Equalfold
func EnqualFold(s string, t string) bool
, a two string comparison, ignoring case, returns the bool type.
package mainimport "fmt"import "strings"func main() { fmt.Println(strings.EqualFold("chain", "CHAIN")) // true fmt.Println(strings.EqualFold("chain", "CHAI")) // false}
Operation Result:
13. Join
func Join(s []string, seq string) string
The string array is stitched into a string by the specified delimiter.
package mainimport "fmt"import "strings"func main() { s := []string{"foo", "bar", "baz"} fmt.Println(strings.Join(s, ", ")) // 返回字符串:foo, bar, baz}
Operation Result:
14. Map
func Map(mapping func(rune)rune, s string) string
, if the mapping method returns a valid string, the method returns a copied string that has been modified by the mapping method.
package mainimport "fmt"import "strings"func main() { rot13 := func(r rune) rune { switch { case r >= 'A' && r <= 'Z': return 'A' + (r-'A'+13)%26 case r >= 'a' && r <= 'z': return 'a' + (r-'a'+13)%26 } return r } fmt.Println(strings.Map(rot13, "'Twas brillig and the slithy gopher..."))}
Operation Result:
15, Repeat
func Repeat(s string, count int)string
, the Change method returns a new string that repeats the specified number of times.
package mainimport "fmt"import "strings"func main() { fmt.Println("ba" + strings.Repeat("na", 2)) //banana}
Operation Result:
16. Replace
func Replace(s, old, new string, count int)string
Returns a new string that is the original string, the string that needs to be replaced, the string to replace, the s
old
number of substitutions, new
old
count
and if-1, replace all.
package mainimport "fmt"import "strings"func main() { fmt.Println(strings.Replace("oink oink oink", "k", "ky", 5)) fmt.Println(strings.Replace("oink oink oink", "k", "ky", 2)) fmt.Println(strings.Replace("oink oink oink", "k", "ky", -1))}
Operation Result:
17. Split
func Split(s, seq string)[]string
Splits a string into an array of strings according to the specified string.
package mainimport "fmt"import "strings"func main() { fmt.Printf("%q\n", strings.Split("a,b,c", ",")) fmt.Printf("%q\n", strings.Split("a man a plan a canal panama", "a ")) fmt.Printf("%q\n", strings.Split(" xyz ", "")) fmt.Printf("%q\n", strings.Split("", "Bernardo O'Higgins"))}
Operation Result:
18, SPLITN
func SplitN(s, seq string, count int)[]string
Splits a string into a string array of the specified number of elements, according to the specified string. The array returned by the method will not retain the delimiter. count
the parameter is 1 when the effect is as Split
.
package mainimport "fmt"import "strings"func main() { fmt.Printf("%q\n", strings.SplitN("/home/m_ta/src", "/", 1)) fmt.Printf("%q\n", strings.SplitN("/home/m_ta/src", "/", 2)) fmt.Printf("%q\n", strings.SplitN("/home/m_ta/src", "/", -1)) fmt.Printf("%q\n", strings.SplitN("home,m_ta,src", ",", 2)) fmt.Printf("%q\n", strings.SplitN("#home#m_ta#src", "#", -1)) }
Operation Result:
19, Splitafter
func SplitAfter(s, seq string)[]string
Splits a string into an array of strings according to the specified string. The array returned by the method preserves the delimiter and the end of each element.
package mainimport "fmt"import "strings"func main() { fmt.Printf("%q\n", strings.SplitAfter("/home/m_ta/src", "/")) //["/" "home/" "m_ta/" "src"]}
Operation Result:
20, Splitaftern
func SplitAfterN(s, seq string, count int)[]string
Splits a string into a string array of the specified number of elements, according to the specified string. The array returned by the method preserves the delimiter and the end of each element. count
the parameter is 1 when the effect is as SplitAfter
.
package mainimport "fmt"import "strings"func main() { fmt.Printf("%q\n", strings.SplitAfterN("/home/m_ta/src", "/", 2)) //["/" "home/m_ta/src"] fmt.Printf("%q\n", strings.SplitAfterN("#home#m_ta#src", "#", -1)) //["/" "home/" "m_ta/" "src"]}
Operation Result:
21. Title
func Title(s string)string
The method returns a new string that changes the first letter of the original string to uppercase and has no effect on the Chinese.
package mainimport "fmt"import "strings"func main() { fmt.Println(strings.Title("her royal highness")) fmt.Println(strings.Title("her rOYal highness")) fmt.Println(strings.Title("我是中国人"))}
Operation Result:
22, Totitle
func ToTitle(s string)string
Converts a string to uppercase.
package mainimport "fmt"import "strings"func main() { fmt.Println(strings.ToTitle("loud noises")) fmt.Println(strings.ToTitle("loud 中国"))}
Operation Result:
23, ToLower
func ToLower(s string)string
Converts a string to a lowercase letter.
package mainimport "fmt"import "strings"func main() { fmt.Println(strings.ToLower("CHain")) //chain}
Operation Result:
24, ToUpper
func ToUpper(s string)string
Converts a string to uppercase.
package mainimport "fmt"import "strings"func main() { fmt.Println(strings.ToUpper("Chain"))}
Operation Result:
25. Trim
func Trim(s, cutset string)string
Removes the specified characters from the string.
package mainimport "fmt"import "strings"func main() { fmt.Println( strings.Trim("!!!Chain!!!", "!"))}
Operation Result:
26, Trimleft
func TrimLeft(s, cutset string)string
Removes the specified character from the left side of the string.
package mainimport "fmt"import "strings"func main() { fmt.Println( strings.TrimLeft("!!!Chain!!!", "!"))}
Operation Result:
27, Trimspace
func TrimSpace(s stirng)string
Removes the blank part of the string.
package mainimport "fmt"import "strings"func main() { fmt.Println( strings.TrimSpace("\t\n a lone gopher \n\t\r\n"))}
Operation Result:
Finish
This article is original, reproduced please indicate the source
Previous section: Golang study Notes (2)--functions