转义符
字符串查询
strings.Contains
package main
import (
"fmt"
"strings"
)
func main() {
var name string = "aabbccddd"
var s = strings.Contains(name, "a")
fmt.Println(strings.Contains(name, "a")) // true
fmt.Println(strings.Index(name, "c")) // 4
}
in
字符串统计个数
var name string = "aabbccddd"
fmt.Println(strings.Count(name, "b")) // 2
字符串查询的前缀和后缀
fmt.Println(strings.HasPrefix(name, "aa")) // 前缀
fmt.Println(strings.HasSuffix(name, "dd")) // 后缀
字符串大小写转换
var name string = "aabbccddd"
var s string = "AAA"
fmt.Println(strings.ToUpper(name)) // AABBCCDDD
fmt.Println(strings.ToLower(s)) // aaa
字符串比较
fmt.Println(strings.Compare(a:"ab",b;"b")) //字符的比较就是ascii的比较 返回-1,1, 0
fmt.Println(strings.Compare(a:"b",b:"a")) //字符的比较就是ascii的比较 返回-1,1, 0
fmt.Println(strings.Compare(a:"b", b: "b")) //字符的比较就是ascii的比较返回-1,1, 0
字符串去除空格
fmt.Println(strings.TrimSpace(" ABC ")) // ABC 去除两边的空格
fmt.Println(strings.TrimLeft("abc", "a")) //bc 去除左边的a
fmt.Println(strings.Trim("abca", "a")) //bc 去除两边的a
字符串分隔
fmt.Println(strings.Split("one,two,three,four,five", ",")) // 数组 [one two three four five]
字符串合并
strings.Join
arr := strings.Split("one,two,three,four,five", ",")
fmt.Println(strings.Join(arr, "-")) // 输出字符串 one-two-three-four-five
字符串替换
fmt.Println(strings.Replace("tony:18,boy", "18", "20", 1)) // 18 替换成 20
fmt.Println(strings.Replace("tony:18,linda:18,nick:20", "18", "20", 1)) // tony:20,linda:18,nick:20 替换1个18
fmt.Println(strings.Replace("tony:18,linda:18,nick:20", "18", "20", 2)) // tony:20,linda:20,nick:20 替换2个18