字符串常用方法都在strings包中
- 高性能字符串拼接
var builder strings.Builder
builder.WriteString("用户名")
builder.WriteByte(97)
str := builder.String()
fmt.Println(str)
- 包含字符串,返回bool
strings.Contains(str, "yo")
- 统计出现次数,返回int
strings.Count(str, "yo")
- 分割,返回slice
strings.Split(str, "yo")
- 前缀判断,返回bool
strings.HasPrefix(str, "yo")
- 后缀判断,返回bool
strings.HasSuffix(str, "yo")
- 出现位置,返回int
strings.Index(str, "a")
- 替换,返回字符串,最后一个参数小于0代表全部替换
strings.Replace(str, "old", "new", -1)
- 大小写转换,返回字符串
strings.ToLower("GO")
strings.ToUpper("GO")
- 清除两端字符,返回字符串,去掉的字符可以为多个
strings.Trim("GO", " ")
strings.TrimLeft("GO", " ")
strings.TrimRight("GO", " ")
标签:返回,yo,字符串,GO,str,Go,方法,strings
From: https://www.cnblogs.com/forlive/p/17198719.html