Go package: strings

Go 的 strings 包中包含许多处理字符串的函数

官方文档:https://golang.org/pkg/strings/

前缀、后缀

判断字符串前缀、后缀

// 判断字符串 s 是否以 prefix 开头
func HasPrefix(s, prefix string) bool

// 判断字符串 s 是否以 suffix 结尾
func HasSuffix(s, suffix string) bool

查找子字符串

查找字符串中是否包含子字符串

// 查找字符串 s 中是否包含 substr
func Contains(s, substr string) bool

// 查找字符串 s 中是否包含 chars 中任意字符
func ContainsAny(s, chars string) bool

查找字符串中子字符串的位置

// 查找字符串 s 中第一次出现 substr 的位置,如果不存在返回 -1
func Index(s, substr string) int

// 查找字符串 s 中最后一次出现 substr 的位置,如果不存在返回 -1
func LastIndex(s, substr string) int

// 查找字符串 s 中第一次出现 chars 中任意字符的位置,如果不存在返回 -1
func IndexAny(s, chars string) int

// 查找字符串 s 中最后一次出现 chars 中任意字符的位置,如果不存在返回 -1
func LastIndexAny(s, chars string) int

// 查找字符串 s 中第一次出现 c 字节的位置,如果不存在返回 -1
func IndexByte(s string, c byte) int

修剪

Trim 通过 Unicode 编码是否一致判断去除的片段,cutset 中包含的是要去除的字节,顺序不一致也会被删除

Trim 会连续去除,直至指定位置不存在该片段,但是 TrimPrefixTrimSuffix 只会去除一次,用于去除前、后缀

// 返回字符串 s 在前后分别去除 cutset 中包含字节的切片
func Trim(s string, cutset string) string

// 返回字符串 s 在前后分别去除空格的切片
// 等于 func Trim(s string, “ ”) string
func TrimSpace(s string) string

// 返回字符串 s 在开头去除 cutset 中包含字节的切片
func TrimLeft(s string, cutset string) string

// 返回字符串 s 在结尾去除 cutset 中包含字节的切片
func TrimRight(s string, cutset string) string

示例:

package main

import (
        "fmt"
        "strings"
)

func main() {
        a := "hello world"
        b := strings.TrimLeft(a, "lhed")
        c := strings.TrimPrefix(a, "hel")
        d := strings.Trim(a, "lhed")
        fmt.Println(a)
        fmt.Println(b)
        fmt.Println(c)
        fmt.Println(d)
}

输出结果:

hello world
o world
lo world
o wor

分割

将一个字符串以指定分隔符进行分隔,返回一个切片

// 以一个或多个空格为分隔符,对字符串 s 进行分割,空字符串会被舍弃
func Fields(s string) []string

// 以一个字符串为分隔符,对字符串 s 进行分割
// 如果分隔符为空,则将每个字符进行切割
func Split(s, sep string) []string

// 在一个字符串 sep 之后对字符串 s 进行分割
// 如果字符串 sep 为空,则对每个字符进行切割
func SplitAfter(s, sep string) []string

// 以一个字符串为分隔符,从第一个分隔符开始,将字符串 s 最多分割成 n 部分
// 当 n == 0 时,返回 nil;当 n == -1 时,相当于 `Split`
func SplitN(s, sep string, n int) []string

// 和 `SplitAfter` 的区别相当于 `Split` 和 `SplitN`
func SplitAfterN(s, sep string, n int) []string

示例:

package main

import (
        "fmt"
        "strings"
)

func main() {
        a := " hello, world,  !"
        b := strings.Fields(a)
        c := strings.Split(a, " ")
        d := strings.Split(a, ",")
        e := strings.SplitAfter(a, ",")
        f := strings.SplitAfterN(a, ",", 2)

        fmt.Println(b, len(b))
        fmt.Println(c, len(c))
        fmt.Println(d, len(d))
        fmt.Println(e, len(e))
        fmt.Println(f, len(f))
}

输出结果:

[hello, world, !] 3
[ hello, world,  !] 5
[ hello  world   !] 3
[ hello,  world,   !] 3
[ hello,  world,  !] 2

替换

替换会搜素字符串中所有符合条件的子字符串,之后将其替换为新字符串

// 将字符串 s 中包含的 old 字符串替换为 new 字符串,从最左边开始一共替换 n 次
// 如果 old 为空,则从字符串开头开始,字符串开头结尾和每个字符之间添加一个 new,一共添加 n 个
// 如果 n < 0 则替换次数没有限制,相当于 `ReplaceAll`
func Replace(s, old, new string, n int) string

// 和 `Replace` 相似,没有限制
func ReplaceAll(s, old, new string) string

示例:

package main

import (
        "fmt"
        "strings"
)

func main() {
        a := " hello world "
        b := strings.Replace(a, " ", ",", 1)
        c := strings.Replace(a, "", ",", 3)
        d := strings.Replace(a, " ", ",", -1)
        e := strings.ReplaceAll(a, " ", ",")

        fmt.Println(b)
        fmt.Println(c)
        fmt.Println(d)
        fmt.Println(e)
}

输出结果:

,hello world 
, ,h,ello world 
,hello,world,
,hello,world,

计数

// 字符串 s 包含子字符串 substr 的数量
// 如果 substr 为空,则返回字符串长度 + 1(字符间隙的数量)
func Count(s, substr string) int

重复

// 将字符串 s 重复 count 次,拼接成一个字符串
// count < 0 时会 panic
func Repeat(s string, count int) string

大小写转换

// 将字符串内所有字母转换成小写
func ToLower(s string) string

// 将字符串中所有字母转换成大写
func ToUpper(s string) string

// 每个单词的第一个字母转换为标题大小写
func Title(s string) string

// 将字符串内所有字母转换为标题大小写
func ToTitle(s string) string

拼接

拼接是切割的反操作

// 以 sep 为分隔符,将 a 中元素拼接起来
func Join(a []string, sep string) string