(资料图片)
Contains:判断字符串中是否包含指定字符串
str1 := "itceshi"result := strings.Contains(str1, "cesh")fmt.Println(result)
join:字符串连接 类似php的implode
str := []string{"a", "b", "c", "d", "e", "f"}join := strings.Join(str, "")fmt.Println("join:", join)
Split:字符串分割,返回切片 类似php的explode
str := "qqhao123@163.com"split := strings.Split(str, "@")fmt.Println("split:", split)
Index:查找指定字符串所在的位置,返回索引,找不到返回-1
str := "qqhao123"result3 := strings.Index(str, "hao")fmt.Println("result:", result3)
Repeat:重复字符串次数,返回重复的字符串
str := "Aceceshi"result4 := strings.Repeat(str, 3)fmt.Println("result:", result4)
Replace:
func Replace(s, old, new string, n int) string
在s字符串中,把old字符串替换为new字符串,n表示替换的次数,小于0表示全部替换
str := "Azzhao"strings.Replace(str, "z", "Z", 1)// 替换第一个strings.Replace(str, "z", "Z", -1)// 全部替换
Trim:
func Trim(s string, cutset string) string
t在s字符串的头部和尾部去除cutset指定的字符串
str := " Azzhao@163.com "trim := strings.Trim(str, " ") // 去掉两头空格fmt.Println("trim:", trim)
Fields:去除字符串的空格符,并且按照空格分割返回slice
str := " Azzhao@163.com "fields := strings.Fields(str)fmt.Println("fields:", fields)
热门推荐
1.golang的string、int、int64、float64互相转换
2.每日一招:搭建Jenkins,Gitlab自动化打包部署项目
3.每日一招:docker安装easyswoole
4.Linux安装MYSQL8.0
5.个人可以做哪些副业兼职赚钱?