Commonly used system functions for strings
- Go language standard library documentation
- 1. The length of the statistical string (in bytes) len(str)
- 2. String traversal, processing Chinese
- 3. Convert string to integer
- 4. Integer to string
- 5. Convert string to []byte
- 6, [] byte string skewer
- 7, decimal to 2, 8, hexadecimal
- 8. Find whether the substring is in the specified string
- 9. Count how many specified substrings of a string
- 10. Case-insensitive string comparison
- 11. Return the position of the first occurrence of the substring in the string
- 12. Return the position of the last occurrence of the substring in the string
- 13. Replace the specified substring with another substring
- 14. Split the string according to the specified characters
- 15. Convert the letters of the string to case
- 17. Remove the specified characters on the left and right sides of the string
- 18. Determine whether the string starts with the specified string
- 19. Determine whether the string ends with the specified string
Go language standard library documentation
1. The length of the statistical string (in bytes) len(str)
package main
import (
"fmt"
)
func main(){
//统计字符串的长度,按字节len(str)
str := "Casey胡"
fmt.Println("str len =",len(str))
}
operation result:
2. String traversal, processing Chinese
package main
import (
"fmt"
)
func main(){
//字符串遍历,同时处理有中文的问题 r := []rune(str)
str := "Casey胡"
r := []rune(str)
for i := 0; i < len(r); i++{
fmt.Printf("字符=%c\n",r[i])
}
}
operation result:
3. Convert string to integer
package main
import (
"fmt"
"strconv"
)
func main(){
n, err := strconv.Atoi("123")
if err != nil{
fmt.Println("转换错误....")
}else{
fmt.Println("转成的结果为:", n)
}
}
operation result:
4. Integer to string
package main
import (
"fmt"
"strconv"
)
func main(){
str := strconv.Itoa(123456)
fmt.Printf("str = %v,str = %T",str,str)
}
operation result:
5. Convert string to []byte
package main
import (
"fmt"
//"strconv"
)
func main(){
var bytes = []byte("hello Go!")
fmt.Printf("bytes = %v",bytes)
}
operation result:
6, [] byte string skewer
package main
import (
"fmt"
//"strconv"
)
func main(){
str := string([]byte{
97,98,99})
fmt.Printf("str = %v\n",str)
}
operation result:
7, decimal to 2, 8, hexadecimal
package main
import (
"fmt"
"strconv"
)
func main(){
str := strconv.FormatInt(123,2)
fmt.Printf("123对应的2进制是=%v\n",str)
str = strconv.FormatInt(123,8)
fmt.Printf("123对应的8进制是=%v\n",str)
str = strconv.FormatInt(123,16)
fmt.Printf("123对应的16进制是=%v\n",str)
}
operation result:
8. Find whether the substring is in the specified string
package main
import (
"fmt"
//"strconv"
"strings"
)
func main(){
//字符串"Casey"中是否含有子串"sey",是返回true,否返回false
b := strings.Contains("Casey","sey")
fmt.Printf("b = %v\n",b)
}
operation result:
9. Count how many specified substrings of a string
package main
import (
"fmt"
//"strconv"
"strings"
)
func main(){
//字符串"Casey"中是否含有子串"sey",是返回true,否返回false
num := strings.Count("CaseyCasey","sey")
fmt.Printf("num = %v\n",num)
}
operation result:
10. Case-insensitive string comparison
package main
import (
"fmt"
//"strconv"
"strings"
)
func main(){
b := strings.EqualFold("abc","aBc")//true
fmt.Printf("b = %v\n",b)
//==是区分字母大小写的
fmt.Println("结果 =","abc" == "aBc")//false
}
operation result:
11. Return the position of the first occurrence of the substring in the string
package main
import (
"fmt"
//"strconv"
"strings"
)
func main(){
//返回子串在字符串中第一次出现的index值,如果没有则返回-1
index := strings.Index("CaseyCasey","sey")
fmt.Printf("index = %v\n",index)
}
operation result:
12. Return the position of the last occurrence of the substring in the string
package main
import (
"fmt"
//"strconv"
"strings"
)
func main(){
//返回子串在字符串中最后一次出现的index值,如果没有则返回-1
index := strings.LastIndex("CaseyCasey","sey")
fmt.Printf("index = %v\n",index)
}
operation result:
13. Replace the specified substring with another substring
package main
import (
"fmt"
//"strconv"
"strings"
)
func main(){
//将指定子串替换成另一个子串
str := "go go Casey"
//-1代表全部替换,若希望替换1个,则该位置的值改为1即可
str1 := strings.Replace(str,"go","胡",-1)
fmt.Println("str1 =",str1)
}
operation result:
14. Split the string according to the specified characters
package main
import (
"fmt"
//"strconv"
"strings"
)
func main(){
//按照指定的某个字符,为分割符,讲一个字符拆分为字符串数组
str := "go/go/Casey"
strArr := strings.Split(str,"/")
for i := 0; i < len(strArr); i++{
fmt.Printf("strArr[%v] = %v\n",i,strArr[i])
}
fmt.Printf("strArr = %v\n",strArr)
}
operation result:
15. Convert the letters of the string to case
package main
import (
"fmt"
"strings"
)
func main(){
str := "Casey"
str1 := strings.ToLower(str)
str2 := strings.ToUpper(str)
fmt.Printf("str = %v,str1 = %v,str2 = %v\n", str, str1, str2)
}
operation result:
17. Remove the specified characters on the left and right sides of the string
package main
import (
"fmt"
"strings"
)
func main(){
//将字符串左右两边的空格去掉
str := strings.TrimSpace("hello go go")
fmt.Printf("str = %v\n",str)
//将字符串左右两边指定的字符去掉
str = strings.Trim("! hello !!","!")
fmt.Printf("str = %v\n",str)
//将字符串左边指定的字符去掉
str = strings.TrimLeft("! hello !!","!")
fmt.Printf("str = %v\n",str)
//将字符串右边指定的字符去掉
str = strings.TrimRight("! hello !!","!")
fmt.Printf("str = %v\n",str)
}
operation result:
18. Determine whether the string starts with the specified string
package main
import (
"fmt"
"strings"
)
func main(){
//判断字符串是否以指定字符串开头
b := strings.HasPrefix("Casey go go","Casey")
fmt.Printf("b = %v\n",b)
}
operation result:
19. Determine whether the string ends with the specified string
package main
import (
"fmt"
"strings"
)
func main(){
//判断字符串是否以指定字符串结束
b := strings.HasSuffix("Casey go","go")
fmt.Printf("b = %v\n",b)
}
operation result: