字符串相关操作
1.Sprintf/Sprintln
输出一个字符串到变量中,Sprintln自带换行
b := 666
s := fmt.Sprintf("%d", b)
fmt.Printf("s: %v, %T\n", s, s)
//结果:
s : 666, string
2.头尾处理函数
strings.HasPrefix(s string, prefix string) bool{}:判断s中是否以prefix为前缀
strings.HasSuffix(s string, suffix string) bool{}:判断s中是否以suffix为后缀
s := "**hello,world "
fmt.Printf("strings.HasPrefix(s, \"*\"): %v\n", strings.HasPrefix(s, "*"))
fmt.Printf("strings.HasPrefix(s, \" \"): %v\n", strings.HasPrefix(s, " "))
fmt.Printf("strings.HasSuffix(s, \" \"): %v\n", strings.HasSuffix(s, " "))
//结果:
strings.HasPrefix(s, "*"): true
strings.HasPrefix(s, " "): false
strings.HasSuffix(s, " "): true
3.位置索引函数
strings.Index(s string, substr string) int{}:从左往右寻找substr出现在s中的第一个位置,有则返回索引值,否则返回-1
strings.LastIndex(s string, substr string) int{}:从右往左寻找substr出现在s中的第一个位置,有则返回索引值,否则返回-1
strings.IndexAny(s string, substr string) int{}:从左到右寻找substr的任意字符出现在s的第一个位置
s := "**hello,world "
fmt.Printf("strings.Index(s, \"lan\"): %v\n", strings.Index(s, "lan"))
fmt.Printf("strings.LastIndex(s, \"l\"): %v\n", strings.LastIndex(s, "l"))
fmt.Printf("strings.IndexAny(s, \"xle\"): %v\n", strings.IndexAny(s, "xle"))
//结果:
strings.Index(s, "lan"): -1
strings.LastIndex(s, "l"): 11
strings.IndexAny(s, "xle"): 3
4.替换函数
strings.Replace(s string, oldstr string, newstr string, count int) string{}:在字符串s中,将oldstr替换为newstr,count为替换次数,-1为全部替换,0为不替换;若oldstr为空,则插入count次newstr
s := "**hello,world "
fmt.Printf("strings.Replace(s, \"*\", \"x\", -1): %v\n", strings.Replace(s, "*", "x", -1))
fmt.Printf("strings.Replace(s, \"*\", \"x\", 0): %v\n", strings.Replace(s, "*", "x", 0))
fmt.Printf("strings.Replace(s, \"*\", \"x\", 1): %v\n", strings.Replace(s, "*", "x", 1))
fmt.Printf("strings.Replace(s, \"\", \"x\", 1): %v\n", strings.Replace(s, "", "x", 1))
fmt.Printf("strings.Replace(s, \"\", \"x\", -1): %v\n", strings.Replace(s, "", "x", -1))
//结果:
strings.Replace(s, "*", "x", -1): xxhello,world
strings.Replace(s, "*", "x", 0): **hello,world
strings.Replace(s, "*", "x", 1): x*hello,world
strings.Replace(s, "", "x", 1): x**hello,world
strings.Replace(s, "", "x", -1): x*x*xhxexlxlxox,xwxoxrxlxdx x
5.统计次数
strings.Count(s string, substr string) int{}:统计s中substr出现的次数
s := "**hello,world "
fmt.Printf("strings.Count(s, \"l\"): %v\n", strings.Count(s, "l"))
//结果:
strings.Count(s, "l"): 3
6.重复次数
strings.Repeat(s string, count int) string{}:将s重复count次
s := "come on!"
fmt.Printf("strings.Repeat(s, 3): %v\n", strings.Repeat(s, 3))
//结果:
strings.Repeat(s, 3): come on!come on!come on!
7.去除字符
strings.Trim(s string, cutstr string) string{}:去除字符串s首尾的cutstr字符串,若不存在则返回s
s := "!six six six!"
fmt.Printf("strings.Trim(s, \" \"): %v\n", strings.Trim(s, "!"))
fmt.Printf("strings.Trim(s, \" \"): %v\n", strings.Trim(s, " "))
//结果:
strings.Trim(s, " "): six six six
strings.Trim(s, " "): !six six six!
strings.TrimSpace(s string){}:去除字符串s的首尾空白字符
s := " six six six! "
fmt.Printf("strings.TrimSpace(s): %v\n", strings.TrimSpace(s))
//结果:
strings.TrimSpace(s): six six six!
strings.TrimLeft(s string, cutstr string) string{}:从左到右删除所有指定的字符,直到不包含cutstr的字符(并不是只能删cutstr,而是cutstr以及它的子串都删)
strings.TrimRight(s string, cutstr string) string{}:从右到左删除所有指定的字符,直到不包含cutstr的字符
s := "six six six!"
fmt.Printf("strings.TrimLeft(s, \"six\"): %v\n", strings.TrimLeft(s, "six")) //不包含空格
fmt.Printf("strings.TrimLeft(s, \"six \"): %v\n", strings.TrimLeft(s, "six ")) //包含空格
fmt.Printf("strings.TrimRight(s, \"six\"): %v\n", strings.TrimRight(s, "six!"))
//结果:
strings.TrimLeft(s, "six"): six six!
strings.TrimLeft(s, "six "): !
strings.TrimRight(s, "six"): six six
8.分割函数
strings.Split(s string, sep string) []string{}:按照sep分割字符串s,返回字符串切片,分割后不包含sep
strings.SplitAfter(s string, sep string) []string{}:按照sep分割字符串s,返回字符串切片,分割后包含sep
s := " six six six!"
fmt.Printf("strings.Split(s, \" \"): %v\n", strings.Split(s, " "))
for i, v := range strings.Split(s, " ") {
fmt.Printf("i: %v, v: %v\n", i, v)
}
fmt.Printf("strings.SplitAfter(s, \" \"): %v\n", strings.SplitAfter(s, " "))
for i, v := range strings.Split(s, " ") {
fmt.Printf("i: %v, v: %v\n", i, v)
}
//结果:
strings.Split(s, " "): [ six six six!]
i: 0, v:
i: 1, v:
i: 2, v: six
i: 3, v: six
i: 4, v: six!
strings.SplitAfter(s, " "): [ six six six!]
i: 0, v:
i: 1, v:
i: 2, v: six
i: 3, v: six
i: 4, v: six!
9.Join函数
strings.Join(s []string, sep string) string{}:将字符串切片以sep为分隔符连接在一起
slice := []string{"123", "456", "789"}
fmt.Printf("strings.Join(slice, \".\"): %v\n", strings.Join(slice, "."))
//结果:
strings.Join(slice, "."): 123.456.789
10.字符串与数字转换函数
strconv.Itoa(n int) string{}:整数转字符串
i := 123456
fmt.Printf("strconv.Itoa(i): %#v\n", strconv.Itoa(i))
//结果:
strconv.Itoa(i): "123456"
strconv.Atoi(s string) (int, error){}:字符串转整数
s1 := "123456"
s2 := "abc"
n1, err := strconv.Atoi(s1)
fmt.Printf("strconv.Atoi(s1): %#v, err : %v\n", n1, err)
n2, err := strconv.Atoi(s2)
fmt.Printf("strconv.Atoi(s2): %v, err : %v\n", n2, err)
//结果:
strconv.Atoi(s1): 123456, err : <nil>
strconv.Atoi(s2): 0, err : strconv.Atoi: parsing "abc": invalid syntax
11.包含操作
strings.Contains(s string, substr string) bool{}:查找s中是否存在sbustr子串,存在返回true
s := "sixsix six"
fmt.Printf("strings.Contains(s, \"ix\"): %v\n", strings.Contains(s, "ix"))
//结果:
strings.Contains(s, "ix"): true
strings.ContainsAny(s string, substr string) bool{}:查找s中是否包含substr中的任意字符,存在返回true
s := "sixsix six"
fmt.Printf("strings.ContainsAny(s, \"ib\"): %v\n", strings.ContainsAny(s, "ib"))
fmt.Printf("strings.ContainsAny(s, \" \"): %v\n", strings.ContainsAny(s, " "))
//结果:
strings.ContainsAny(s, "ib"): true
strings.ContainsAny(s, " "): true
strings.ContainsRune(s string, r rune) bool{}:查找字符串s中是否存在字符r
s := "sixsix six"
fmt.Printf("strings.ContainsRune(s, 'i'): %v\n", strings.ContainsRune(s, 'i'))
fmt.Printf("strings.ContainsRune(s, 'c'): %v\n", strings.ContainsRune(s, 'c'))
//结果:
strings.ContainsRune(s, 'i'): true
strings.ContainsRune(s, 'c'): false
12.比较函数
strings.Compare(a string, b string) int{}:若a==b,则返回0;若a<b,则返回-1;若a>b,则返回1
fmt.Printf("strings.Compare(\"abc\", \"bcd\"): %v\n", strings.Compare("abc", "bcd"))
fmt.Printf("strings.Compare(\"999\", \"777\"): %v\n", strings.Compare("999", "777"
//结果:
strings.Compare("abc", "bcd"): -1
strings.Compare("999", "777"): 1
13.克隆函数
strings.Clone(s string) string{}:复制一个字符串s,底层调用切片copy函数
s := "!six six six!"
fmt.Printf("strings.Clone(s): %v\n", strings.Clone(s))
//结果:
strings.Clone(s): !six six six!
14.拼接操作
s1 := "hello "
s2 := "world"
//第一种
fmt.Printf("(s1 + s2): %v\n", (s1 + s2))
//第二种
fmt.Printf("(s1 + s2): %v\n", fmt.Sprintf("%s%s", s1, s2))
//第三种
s := []string{s1, s2}
fmt.Printf("(s1 + s2): %v\n", strings.Join(s, ""))
//第四种
var buf bytes.Buffer
buf.WriteString(s1)
buf.WriteString(s2)
fmt.Printf("(s1 + s2): %v\n", buf.String())
//第五种
var b strings.Builder
b.WriteString(s1)
b.WriteString(s2)
fmt.Printf("(s1 + s2): %v\n", b.String())
//结果:
(s1 + s2): hello world