Go strings包
Go标准库strings提供丰富的字符串处理函数。
字符串查找
Contains系列
Go
import "strings"
// 包含检查
strings.Contains("hello world", "world") // true
strings.Contains("hello", "x") // false
// 包含任意字符
strings.ContainsAny("hello", "aeiou") // true
// 包含 rune
strings.ContainsRune("hello", 'e') // true
Index系列
Go
// 查找位置
strings.Index("hello", "l") // 2(首次出现)
strings.LastIndex("hello", "l") // 3(最后出现)
// 查找任意字符位置
strings.IndexAny("hello", "aeiou") // 1(e的位置)
// 查找 rune 位置
strings.IndexRune("hello", 'l') // 2
// 未找到返回 -1
strings.Index("hello", "x") // -1
字符串判断
前缀后缀
Go
// 前缀判断
strings.HasPrefix("hello world", "hello") // true
strings.HasPrefix("hello", "world") // false
// 后缀判断
strings.HasSuffix("hello.txt", ".txt") // true
strings.HasSuffix("hello", ".txt") // false
空白判断
Go
// 空字符串判断
strings.TrimSpace(" hello ") // "hello"
// 其他空白函数
strings.Trim("xxhelloxx", "x") // "hello"
strings.TrimLeft("xxhello", "x") // "hello"
strings.TrimRight("helloxx", "x") // "hello"
strings.TrimPrefix("hello.txt", "hello") // ".txt"
strings.TrimSuffix("hello.txt", ".txt") // "hello"
字符串分割
Split系列
Go
// 分割字符串
parts := strings.Split("a,b,c", ",")
// ["a", "b", "c"]
// 限制分割数量
parts := strings.SplitN("a,b,c,d", ",", 2)
// ["a", "b,c,d"]
// 按空白分割
parts := strings.Fields("hello world")
// ["hello", "world"]
// 按空白分割(保留空白)
lines := strings.SplitAfter("a,b,c", ",")
// ["a,", "b,", "c"]
Fields按空白分割,自动处理多个空白字符。
字符串拼接
Join函数
Go
// 拼接字符串数组
parts := []string{"a", "b", "c"}
result := strings.Join(parts, ",")
// "a,b,c"
// 无分隔符拼接
result := strings.Join(parts, "")
// "abc"
Builder高效拼接
Go
// strings.Builder高效拼接
var b strings.Builder
b.WriteString("hello")
b.WriteString(" ")
b.WriteString("world")
result := b.String()
// "hello world"
// Builder性能优于多次+=
字符串替换
Replace系列
Go
// 替换
strings.Replace("hello world", "world", "Go", 1)
// "hello Go"
// 替换全部
strings.ReplaceAll("a,b,a", "a", "x")
// "x,b,x"
// 替换指定数量(-1替换全部)
strings.Replace("a,b,a", "a", "x", 2)
// "x,b,x"
Replacer批量替换
Go
// 创建替换器
r := strings.NewReplacer(
"a", "1",
"b", "2",
"c", "3",
)
result := r.Replace("abc")
// "123"
// 可复用替换器
大小写转换
转换函数
Go
// 大小写转换
strings.ToUpper("hello") // "HELLO"
strings.ToLower("HELLO") // "hello"
strings.Title("hello world") // "Hello World"(首字母大写)
// 标题格式已弃用,推荐使用cases包
字符串比较
Compare函数
Go
// 字符串比较
strings.Compare("a", "b") // -1(a < b)
strings.Compare("b", "a") // 1(b > a)
strings.Compare("a", "a") // 0(相等)
// 通常直接用 == 比更简洁
字符串重复
Repeat函数
Go
// 重复字符串
strings.Repeat("ab", 3) // "ababab"
strings.Repeat("x", 5) // "xxxxx"
字符串映射
Map函数
Go
// 字符映射转换
result := strings.Map(func(r rune) rune {
if r >= 'a' && r <= 'z' {
return r - 32 // 转大写
}
return r
}, "hello")
// "HELLO"
字符串计数
Count函数
Go
// 计数子串出现次数
strings.Count("hello", "l") // 2
strings.Count("abcabc", "ab") // 2
strings.Count("hello", "") // 6(空串返回长度+1)
EqualFold
忽略大小写比较
Go
// 忽略大小写相等判断
strings.EqualFold("HELLO", "hello") // true
strings.EqualFold("Go", "go") // true
常用函数表
| 函数 | 用途 | 示例 |
|---|---|---|
| Contains | 包含检查 | Contains(s, sub) |
| HasPrefix | 前缀判断 | HasPrefix(s, pre) |
| HasSuffix | 后缀判断 | HasSuffix(s, suf) |
| Index | 查找位置 | Index(s, sub) |
| Split | 分割 | Split(s, sep) |
| Join | 拼接 | Join(arr, sep) |
| Replace | 替换 | Replace(s, old, new, n) |
| ToUpper | 大写 | ToUpper(s) |
| ToLower | 小写 | ToLower(s) |
| TrimSpace | 去空白 | TrimSpace(s) |
| Repeat | 重复 | Repeat(s, n) |
| Count | 计数 | Count(s, sub) |
Builder方法
| 方法 | 用途 |
|---|---|
| WriteString | 写入字符串 |
| Write | 写入字节 |
| String | 获取结果 |
| Reset | 清空Builder |
| Len | 当前长度 |
| Cap | 当前容量 |
要点总结
- Contains判断是否包含子串
- Index返回首次出现位置
- HasPrefix/HasSuffix判断前缀后缀
- Split分割,Join拼接
- Replace替换,-1替换全部
- ToUpper/ToLower大小转换
- TrimSpace去除两端空白
- Repeat重复字符串
- Builder高效拼接大量字符串
- EqualFold忽略大小写比较
- Fields按空白分割字符串
📝 发现内容有误?点击此处直接编辑