1. strings和strconv使用
strings.HasPrefix(s string, prefix string) bool:判断字符串s是否以prefix开头
str := "hul uwah ahahAha" a := strings.HasPrefix(str, "hulu")
- strings.HasSuffix(s string, suffix string) bool:判断字符串s是否以suffix结尾。
b := strings.HasSuffix(str, "haha")
fmt.Println("结尾=>", b)
- strings.Index(s string, str string) int:判断str在s中首次出现的位置,如果没有出现,则返回-1
c := strings.Index(str, "haha")
fmt.Println("包含=>", c)
- strings.LastIndex(s string, str string) int:判断str在s中最后出现的位置,如果没有出现,则返回-1
d := strings.LastIndex(str, "haha")
fmt.Println("最后出现的位置=>", d)
- strings.Replace(str string, old string, new string, n int):字符串替换
e := strings.Replace(str, "haha", "123", 3)
fmt.Println("替换=>", e)
- strings.Count(str string, substr string)int:字符串计数
f := strings.Count(str, "ha")
fmt.Println("计数出现次数=>", f)
- strings.Repeat(str string, count int)string:重复count次str
g := strings.Repeat(str, 2)
fmt.Println("字符串加倍=>", g)
- strings.ToLower(str string)string:转为小写
h := strings.ToLower(str)
fmt.Println("转小写=>", h)
- strings.ToUpper(str string)string:转为大写
i :=strings.ToUpper(str)
fmt.Println("转大写=>", i)
- strings.TrimSpace(str string):去掉字符串首尾空白字符
j := strings.TrimSpace(str)
fmt.Println("去掉收尾空白字符串=>",j)
- strings.Trim(str string, cut string):去掉字符串首尾cut字符
k :=strings.Trim(str,"hua") //只要前后在俩面就会处理
fmt.Println("去掉字符串前后=>",k)
- strings.TrimLeft(str string, cut string):去掉字符串首cut字符
l :=strings.TrimLeft(str,"hua")
fmt.Println("去掉字符串首=>",l)
- strings.TrimRight(str string, cut string):去掉字符串首cut字符
m :=strings.TrimRight(str,"hua")
fmt.Println("去掉字符串尾=>",m)
- strings.Field(str string):返回str空格分隔的所有子串的slice
n :=strings.Fields(str)
fmt.Println("返回str空格分隔的所有子串的slice=>",n)
- strings.Split(str string, split string):返回str split分隔的所有子串的slice
o := strings.Split(str,"")
fmt.Println("split分隔的所有子串的slice=>",o)
- strings.Join(s1 []string, sep string):用sep把s1中的所有元素链接起来
s := []string{"jiqimao","kabuda",}
p := strings.Join(s,"")
fmt.Println(p,str)
- strconv.Itoa(i int):把一个整数i转成字符串
fmt.Println(strconv.Itoa(18))
- strconv.Atoi(str string)(int, error):把一个字符串转成整数
q,_ := strconv.Atoi("56")
fmt.Println(q)