- 统计字符串的长度,按字节 len(str) (内建函数,在文档builtin查询)
![Go 字符串常用函数汇总及详解_第1张图片](http://img.e-com-net.com/image/info8/a9aec2a51bbb43459a0548eddacd4ca3.jpg)
-- golang的编码统一为utf-8 (ascii的字符(字母和数字) 占一个字节,汉字占用3个字节)
str := "hello北"
fmt.Println("str len=", len(str)) // 8
![](http://img.e-com-net.com/image/info8/02949e298bca418e9509769d91730978.png)
- 字符串遍历,同时处理有中文的问题 r := []rune(str)
-- 字符串遍历,同时处理有中文的问题 r := []rune(str)
str2 := "hello北京"
r := []rune(str2)
for i := 0; i < len(r); i++ {
fmt.Printf("字符=%c\n", r[i])
}
![Go 字符串常用函数汇总及详解_第2张图片](http://img.e-com-net.com/image/info8/d4ee1e4c4c72403898e1ca53db0f5a9f.png)
- 字符串转整数: n, err := strconv.Atoi("12")
![Go 字符串常用函数汇总及详解_第3张图片](http://img.e-com-net.com/image/info8/d7271d82c9ad433ba7040ee191cc805d.jpg)
Nil:表示无值,任何变量在没有被赋值之前的值都为nil。
-- 字符串转整数: n, err := strconv.Atoi("12")
n, err := strconv.Atoi("123")
if err != nil {
fmt.Println("转换错误", err)
}else {
fmt.Println("转成的结果是", n)
}
![](http://img.e-com-net.com/image/info8/b5a0180c8ce04092a9b6800db88b1774.png)
- 整数转字符串 str = strconv.Itoa(12345)
str := strconv.Itoa(12345)
fmt.Printf("str=%v, str=%T\n", str, str)
![](http://img.e-com-net.com/image/info8/50b60a6f729644e2a8cafe37b69660b6.png)
- 字符串 转 []byte: var bytes = []byte("hello go"),转为ASCII编码。
var bytes = []byte("hello go")
fmt.Printf("bytes=%v\n", bytes)
![](http://img.e-com-net.com/image/info8/d9e5a8a79bbb4d75826b0545717c59e3.png)
- []byte 转 字符串: str = string([]byte{97, 98, 99})。将ASCII编码转字符。
str = string([]byte{97, 98, 99})
fmt.Printf("str=%v\n", str)
![](http://img.e-com-net.com/image/info8/47b3a73aaec84154ab31e4b25cd576f2.png)
- 10进制转 2, 8, 16进制: str = strconv.FormatInt(123, 2),返回对应的字符串
![Go 字符串常用函数汇总及详解_第4张图片](http://img.e-com-net.com/image/info8/c6699cd2692f47118ebc97ea38efee68.jpg)
-- 10进制转 2, 8, 16进制: str = strconv.FormatInt(123, 2),返回对应的字符串
str := strconv.FormatInt(123, 2)
fmt.Printf("123对应的二进制是=%v\n", str)
str = strconv.FormatInt(123, 16)
fmt.Printf("123对应的16进制是=%v\n", str)
![](http://img.e-com-net.com/image/info8/6bc0c018ce05452a8eed620342df986f.jpg)
- 查找子串是否在指定的字符串中: strings.Contains("seafood", "foo")
![Go 字符串常用函数汇总及详解_第5张图片](http://img.e-com-net.com/image/info8/ecb973a0599341c799751fff504d0b89.jpg)
b := strings.Contains("seafood", "foo")
fmt.Printf("b=%v\n", b)
![](http://img.e-com-net.com/image/info8/a638ea7d1e1f4265953eea9ea924d18f.png)
- 统计一个字符串有几个指定的子串 : strings.Count("ceheese", "e")
![Go 字符串常用函数汇总及详解_第6张图片](http://img.e-com-net.com/image/info8/7c50b26d07554179b027da6f253d1ae3.jpg)
-- 统计一个字符串有几个指定的子串 : strings.Count("ceheese", "e") //4
num := strings.Count("ceheese", "e")
fmt.Printf("num=%v\n", num)
![](http://img.e-com-net.com/image/info8/152f2ebcf25e4a3b8bb3890dc9022c02.png)
- 不区分大小写的字符串比较(==是区分字母大小写的): fmt.Println(strings.EqualFold("abc""Abc"))
b = strings.EqualFold("abc", "Abc")
fmt.Printf("b=%v\n", b) //true
fmt.Println("结果","abc" == "Abc") // false //区分字母大小写
![](http://img.e-com-net.com/image/info8/b48c393ad78c41758af09f7f55555958.png)
- 返回子串在字符串第一次出现的 index 值,如果没有返回-1 : strings.Index("NLT_abc", "abc") // 4
![Go 字符串常用函数汇总及详解_第7张图片](http://img.e-com-net.com/image/info8/395634ea7f424693bd036410cbaa6600.jpg)
-- 返回子串在字符串第一次出现的index值,如果没有返回-1 :
strings.Index("NLT_abc", "abc") // 4
index := strings.Index("NLT_abcabcabc", "abc") // 4
fmt.Printf("index=%v\n",index)
![](http://img.e-com-net.com/image/info8/dc662e6911584f14a41e465622b30aee.png)
- 返回子串在字符串最后一次出现的 index,如没有返回-1 : strings.LastIndex("go golang", "go")
-- 返回子串在字符串最后一次出现的index,如没有返回-1 : strings.LastIndex("go golang", "go")
index := strings.LastIndex("go golang", "go") //3
fmt.Printf("index=%v\n",index)
![](http://img.e-com-net.com/image/info8/ef5206745db8404bb13d8dbf121aa17d.png)
- 将指定的子串替换成 另外一个子串: strings.Replace("go go hello", "go", "go 语言", n) n 可以指定你希望替换几个,如果 n=-1 表示全部替换,返回一个新字符串,原先字符串不变
str2 := "go go hello"
str := strings.Replace(str2, "go", "北京", -1)
fmt.Printf("str=%v str2=%v\n", str, str2)
- 按 照 指 定 的 某 个 字 符 , 为 分 割 标 识 , 将 一 个 字 符 串 拆 分 成 字 符 串 数 组 :strings.Split("hello,wrold,ok", ",")
strArr := strings.Split("hello,wrold,ok", ",")
for i := 0; i < len(strArr); i++ {
fmt.Printf("str[%v]=%v\n", i, strArr[i])
}
fmt.Printf("strArr=%v\n", strArr)
![](http://img.e-com-net.com/image/info8/10ccf6dcbae24533b9b0ca0ae88586cf.png)
- 将字符串的字母进行大小写的转换: strings.ToLower("Go") // go strings.ToUpper("Go") // GO
str := "goLang Hello"
str1 := strings.ToLower(str)
str2 := strings.ToUpper(str)
fmt.Printf("str=%v\n", str1)
fmt.Printf("str=%v\n", str2)
![](http://img.e-com-net.com/image/info8/58f3742f1ec84bb1b0e853521ddc8cf8.png)
- 将字符串左右两边的空格去掉: strings.TrimSpace(" tn a lone gopher ntrn ")
-- 将字符串左右两边的空格去掉: strings.TrimSpace(" tn a lone gopher ntrn "
str := strings.TrimSpace(" tn a lone gopher ntrn ")
fmt.Printf("str=%q\n", str)
![](http://img.e-com-net.com/image/info8/11f91cf933264d9986a5d8a8658d1088.png)
- 将字符串左右两边指定的字符去掉 : strings.Trim("! hello! ", " !") // ["hello"] //将左右两边 !和 " "去掉
str := strings.Trim("! he!llo! ", " !")
fmt.Printf("str=%q\n", str)
![](http://img.e-com-net.com/image/info8/e0f7aa649a7c40a5bd257b0f3a00c69d.png)
- 将字符串左边指定的字符去掉 :strings.TrimLeft("! hello! ", " !") //将左边 ! 和 " "去掉
- 将字符串右边指定的字符去掉 :strings.TrimRight("! hello! ", " !") //将右边 ! 和 " "去掉
- 判断字符串是否以指定的字符串开头: strings.HasPrefix("ftp://192.168.10.1", "ftp") // true
b := strings.HasPrefix("ftp://192.168.10.1", "hsp") //true
fmt.Printf("b=%v\n", b)
![](http://img.e-com-net.com/image/info8/ab3864b5d3a544c2b508ec2cf14b70cd.png)
- 判断字符串是否以指定的字符串结束: strings.HasSuffix("NLT_abc.jpg", "abc") //false