Summary and sharing of commonly used strings functions in Go

藏色散人
Release: 2021-10-21 11:48:10
forward
2070 people have browsed it

This article is provided by the go language tutorial column to summarize and share the commonly used strings functions in Golang. I hope it will be helpful to friends in need!

Summary and sharing of commonly used strings functions in Go

Commonly used strings functions in Golang

##6 . 7 . 8. 9. #10. Returns the first occurrence of the substring in the string The index value, if not returned -1 strings.Index("NLT_abc", "abc") // 4Return the index of the last occurrence of the substring in the string, if not return -1 strings.LastIndex("go golang", "go")Replace the specified substring with another substring strings.Replace("go go hello", "go", "go language", n), n can specify you I hope to replace a few, if n=-1 means replace all
Function Introduction
len(str) 1. Statistics on string length, by bytes len(str)

2. String traversal, processing Chinese r:=[]rune(str)

3. Convert string to integer n, err := strconv.Atoi("12")

4. Integer to string str = strconv.Itoa(12345)

5. String to []byte var bytes = []byte("hello go")

[]byte to string str = string([]byte{97, 98, 99})

10 base to 2, 8, 16 base: str = strconv.FormatInt(123, 2) // 2-> 8, 16

Find whether the substring is in the specified string strings.Contains("seafood", "foo") //true

Count how many specified substrings a string has strings.Count("ceheese", "e") //4

Case-insensitive string comparison (== is case-sensitive) fmt.Println(strings.EqualFold(" abc", "Abc")) // true

##11.

12.

13.
##14.
According to a specified character, it is Split identifier, split a string into a string array strings.Split("hello,wrold,ok", ",")
15.
Convert the letters of the string to upper and lower case: strings.ToLower("Go") // go strings.ToUpper("Go") // GO
16.
Remove the spaces on the left and right sides of the string: strings.TrimSpace(" tn a lone gopher ntrn ")
17.
Remove the specified characters on the left and right sides of the string: strings.Trim("! hello! ", " !")
18.
Remove the specified characters on the left side of the string: strings.TrimLeft("! hello! ", " !")
19.
Remove the specified characters on the right side of the string: strings.TrimRight("! hello! ", " !")
20.
Determine whether the string begins with the specified string: strings.HasPrefix("ftp://192.168.10.1 ", "ftp")
21.
Determine whether the string ends with the specified string: strings.HasSuffix( "NLT_abc.jpg", "abc") //false

The above is the detailed content of Summary and sharing of commonly used strings functions in Go. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template