go语言字符串函数详解
字符串常用函数
1. 统计字符串的长度,按字节len(str)
// golang的编码统一为utf-8,(ascli的字符(字母和数字)占一个字节,汉字占三个字节)
str := "hello上海"
fmt.Println("str len=", len(str))
输出结果:
str len= 11
2. 字符串遍历,同时处理有中文的问题r:=[]rune(str)
str := "hello上海"
r := []rune(str)
for index, value := range r {
fmt.Printf("index=%d,value=%c\n", index, value)
}
输出结果:
index=0,value=h
index=1,value=e
index=2,value=l
index=3,value=l
index=4,value=o
index=5,value=上
index=6,value=海
3. 字符串转整数
str := "123456"
result, err := strconv.Atoi(str)
if err != nil {
fmt.Println("转换错误,", err)
} else {
fmt.Printf("result的类型为%T,result=%d\n", result, result)
}
输出结果:
result的类型为int,result=123456
4. 整数转字符串
num := 123456
result := strconv.Itoa(num)
fmt.Printf("result的类型为%T,result=%v\n", result, result)
输出结果:
result的类型为string,result=123456
5. 字符串转[]byte: var bytes = []byte(str)
var bytes = []byte("hello go")
fmt.Printf("bytes=%v\n", bytes)
输出结果:
bytes=[104 101 108 108 111 32 103 111]
6. []byte转字符串:str:=string([]byte{[104 101 108 108 111 32 103 111]})
str := string([]byte{104, 101, 108, 108, 111, 32, 103, 111})
fmt.Printf("str=%v\n", str)
输出结果:
str=hello go
7. 10进制转2,8,16进制:str:=strconv.FormatInt(456,2)
str2 := strconv.FormatInt(456, 2)
str8 := strconv.FormatInt(456, 8)
str16 := strconv.FormatInt(456, 16)
fmt.Printf("456对应的二进制是%v\n", str2)
fmt.Printf("456对应的八进制是%v\n", str8)
fmt.Printf("456对应的十六进制是%v\n", str16)
输出结果:
456对应的二进制是111001000
456对应的八进制是710
456对应的十六进制是1c8
8. 查找子串是否在指定的字符串中:strings.Contains("hello go","go")
b := strings.Contains("hello go", "go")
fmt.Printf("b=%v\n", b)
输出结果:
b=true
9. 统计一个字符串有几个指定的子串:strings.Count("adfcaaafgxcda","a")
num := strings.Count("adfcaaafgxcda", "a")
fmt.Printf("num=%v\n", num)
输出结果:
num=5
10. 不区分大小写的字符串比较(==是区分字母大小写),strings.EqualFold("aGB", "AGB")
b1 := strings.EqualFold("aGB", "AGB")
b2 := ("aGB" == "AGB")
fmt.Printf("b1=%v\n", b1)
fmt.Printf("b2=%v\n", b2)
输出结果:
b1=true
b2=false
11. 返回子串在字符串第一次出现的index值,如果没有返回-1,strings.Index("asdfhjjvghja", "jj")
index1 := strings.Index("aabbaaddbaacc", "aa")
index2 := strings.Index("aabbaaddbaacc", "gg")
fmt.Printf("index1=%v\n", index1)
fmt.Printf("index2=%v\n", index2)
输出结果:
index1=0
index2=-1
12. 返回子串在字符串最后一次出现的index,如果没有返回-1,strings.LastIndex("aabbaaddbaacc", "aa")
index1 := strings.LastIndex("aabbaaddbaacc", "aa")
index2 := strings.LastIndex("aabbaaddbaacc", "gg")
fmt.Printf("index1=%v\n", index1)
fmt.Printf("index2=%v\n", index2)
输出结果:
index1=9
index2=-1
13. 将指定的子串替换成另外一个子串,strings.Replace("aabbaaddbaacc", "aa", "gg", n),你可以指定你替换几个,如果n=-1表示全部替换
str1 := strings.Replace("aabbaaddbaacc", "aa", "gg", -1)
str2 := strings.Replace("aabbaaddbaacc", "aa", "gg", 2)
fmt.Printf("str1=%v\n", str1)
fmt.Printf("str2=%v\n", str2)
输出结果:
str1=ggbbggddbggcc
str2=ggbbggddbaacc
14. 按照指定的某个字符,为分隔标识,将一个字符串拆分成字符串数组,strings.Split("hello,world,ok", ",")
strs := strings.Split("hello,world,ok", ",")
fmt.Printf("strs=%v\n", strs)
输出结果:
strs=[hello world ok]
15. 将字符串进行大小写转换,strings.ToLower("dFsdfJNS"),strings.ToUpper("dFsdfJNS")
lower := strings.ToLower("dFsdfJNS")
fmt.Printf("lower=%v\n", lower)
upper := strings.ToUpper("dFsdfJNS")
fmt.Printf("upper=%v\n", upper)
输出结果:
lower=dfsdfjns
upper=DFSDFJNS
16. 将字符串左右两边的空格去掉,strings.TrimSpace(" welcone to china ")
str := strings.TrimSpace(" welcone to china ")
fmt.Printf("str=%v\n", str)
输出结果:
str=welcone to china
17. 将字符串左右两边指定的字符去掉,strings.Trim(" !golang! a", " !a")
str := strings.Trim(" !golang! a", " !a")
fmt.Printf("str=%v\n", str)
输出结果:
str=golang
18. 将字符串左边指定的字符去掉,strings.TrimLeft(" !golang! a", " !a")
str := strings.TrimLeft(" !golang! a", " !a")
fmt.Printf("str=%v\n", str)
输出结果:
str=golang! a
19. 将字符串右边指定的字符去掉,strings.TrimRight(" !golang! a", " !a")
str := strings.TrimRight(" !golang! a", " !a")
fmt.Printf("str=%v\n", str)
输出结果:
str= !golang
20. 判断字符串是否以指定的字符串开头:strings.HasPrefix("http://www.baidu.com", "http")
b := strings.HasPrefix("http://www.baidu.com", "http")
fmt.Printf("b=%v\n", b)
输出结果:
b=true
21. 判断字符串是否以指定字符串结束:strings.HasSuffix("http://www.baidu.com", ".com")
b := strings.HasSuffix("http://www.baidu.com", ".com")
fmt.Printf("b=%v\n", b)
输出结果:
b=true