1. compare
a := "gopher"
b := "hello world"
fmt.Println(strings.Compare(a, b))
fmt.Println(strings.Compare(a, a))
fmt.Println(strings.Compare(b, a))
fmt.Println(strings.EqualFold("GO", "go"))
fmt.Println(strings.EqualFold("壹", "一"))
输出:
-1
0
1
true
false
说明:
如果两个字符串相等,返回为 0。如果 a 小于 b ,返回 -1 ,反之返回 1
2. Contains
// 子串 substr 在 s 中,返回 true
func Contains(s, substr string) bool
3. Count
计算子串(模式串)出现次数,实现的是 Rabin-Karp 算法
4. Index
func Index(s, sep string) int 子串第一次出现的位置
没找到则返回-1
func IndexByte(s string, c byte) int
字符c在s中第一次出现的位置
不存在则返回-1。
5.Split
字符串切割,特例:如果一个空字符串通过strings
包的Split
函数进行切割,那么结果是一个长度为1的数组,里面的内容是一个空字符串。
a := strings.Split("", ";")
fmt.Printf("%d****%s****\n", len(a), a[0])
再比如:
s = strings.Split("abc,abc", ",")
fmt.Println(s, len(s))
s = strings.Split("abc,abc", "|")
fmt.Println(s, len(s))
结果:
[abc abc] 2
[abc,abc] 1
关于类型转换:
请看:https://blog.youkuaiyun.com/icebergliu1234/article/details/104983944
参考:https://blog.youkuaiyun.com/li_101357/article/details/80241224