Preface

Go字符切片为内存中的字节序列提供了抽象,而Go字符串则代表了采用UTF-8编码的Unicode字符的数组

API类别:

  • 查找
  • 替换
  • 比较
  • 分割
  • 拼接
  • 修剪
  • 变换
  • 对接I/O

strings包与bytes包的大部分api基本相同

查找

定性查找

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
fmt.Println(strings.Contains("Golang", "Go")) //true
fmt.Println(bytes.Contains([]byte("Golang"), []byte("Go"))) //true
fmt.Println(bytes.Contains([]byte("Golang"), nil)) //true

//第二个参数可当作字符集
fmt.Println(strings.ContainsAny("Golang", "c++")) //false
fmt.Println(bytes.ContainsAny([]byte("Golang"), "c++")) //false

//是否包含该字符码
fmt.Println(strings.ContainsRune("Golang", 97)) //true
fmt.Println(bytes.ContainsRune([]byte("Golang"), rune('中'))) //false

//前缀后缀
fmt.Println(strings.HasPrefix("Golang", "Go")) //true
fmt.Println(bytes.HasPrefix([]byte("Golang"), []byte("Go"))) //true
fmt.Println(strings.HasSuffix("Golang", "lang")) //true
fmt.Println(bytes.HasSuffix([]byte("Golang"), []byte("Go"))) //false

定位查找

1
2
3
4
5
6
7
8
9
10
11
12
//bytes同理

//返回下标
fmt.Println(strings.Index("Learn Go, let's go!", "Go")) //6
//第二个参数为字符集
fmt.Println(strings.IndexAny("Learn Go, let's go!", "java")) //2
//字符码
fmt.Println(strings.IndexRune("Learn Go, let's go!", rune('a'))) //2

//倒序
fmt.Println(strings.LastIndex("Learn Go, let's Go!", "Go")) // 16
fmt.Println(strings.LastIndexAny("Learn Go, let's Go!", "vue")) //11

替换

strings.NewReplacer是strings独有的,bytes没有,其他的都一样

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//bytes同理

// 原字符串, 旧值, 新值, 次数
fmt.Println(strings.Replace("I love go, go, go", "go", "blockchain", 1))
//I love blockchain, go, go

fmt.Println(strings.Replace("I love go, go, go", "go", "blockchain", 2))
//I love blockchain, blockchain, go

//-1代表全部
fmt.Println(strings.Replace("I love go, go, go", "go", "blockchain", -1))
//I love blockchain, blockchain, blockchain

fmt.Println(strings.Replace("math", "", "x", -1)) //xmxaxtxhx

fmt.Println(strings.ReplaceAll("I love go, go, go", "go", "blockchain"))
//I love blockchain, blockchain, blockchain

replacer := strings.NewReplacer("java", "go", "python", "go") // bytes没有
fmt.Println(replacer.Replace("I love java, python!!")) //I love go, go!!

比较

string可以用原生==比较,[]byte不行

1
2
3
4
5
6
7
8
9
//比较
fmt.Println("abc" == "bcd") //false
fmt.Println(bytes.Equal([]byte{'a', 'b', 'c'}, []byte{'b', 'c', 'd'})) //false

//忽略大小写
fmt.Println(strings.EqualFold("abc", "ABC")) //true

//根据字符码比较
fmt.Println(bytes.Compare([]byte{'a', 'b', 'c'}, []byte{'b', 'c', 'd'})) // -1

分割

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//分割

//忽略空白字符
fmt.Println(strings.Fields("go java python")) //[go java python]

//自定义空白字符
splitFunc := func(r rune) bool {
return r == rune('\n')
}
fmt.Println(strings.FieldsFunc("\tgo \njava \t python\u0085", splitFunc))
//[ go java python]

//自定义分隔符
fmt.Println(strings.Split("a,b,c", ",")) //[a b c]
//分割每个字符
fmt.Println(strings.Split("partyNeverEnd", "")) //[p a r t y N e v e r E n d]
//指定分割段数
fmt.Println(strings.SplitN("a,b,c,d", ",", 2)) //[a b,c,d]
fmt.Println(strings.SplitN("a,b,c,d", ",", 3)) //[a b c,d]

//保留分割符
fmt.Println(strings.SplitAfter("a,b,c,d", ",")) //[a, b, c, d]
fmt.Println(strings.SplitAfterN("a,b,c,d", ",", 2)) //[a, b,c,d]

拼接

  • strings.builder
  • bytes.buffer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//拼接
s := []string{"I", "love", "Go"}
fmt.Println(strings.Join(s, " ")) //I love Go

var builder strings.Builder //strings
for i, w := range s {
builder.WriteString(w)
if i != len(s)-1 {
builder.WriteString(" ")
}
}
fmt.Println(builder.String()) //I love Go

b := [][]byte{[]byte("I"), []byte("love"), []byte("Go")}
var buf bytes.Buffer //bytes
for i, w := range b {
buf.Write(w)
if i != len(b)-1 {
buf.WriteString(" ")
}
}
fmt.Println(buf.String()) //I love Go

修剪

1
2
3
4
5
6
7
8
9
10
11
12
13
//修剪
//去掉空白字符
fmt.Println(strings.TrimSpace("\t\n\nI love Go\t\n\r")) //I love Go

//从首尾两端分别找到第一个不在修剪字符集合中的字符
fmt.Println(strings.Trim("I don't love Go", "Io")) // don't love G

//可将第二个参数理解为字符集
fmt.Println(strings.TrimLeft("\t\nI love Go\n\rfff", "\t\n\r f")) //I love Go
//fff
//第二个参数为字符串
fmt.Println(strings.TrimPrefix("I I love Go", "I ")) //I love Go

变换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//变换
fmt.Println(strings.ToUpper("a")) //A
fmt.Println(strings.ToLower("A")) //a

trans := func(r rune) rune {
switch {
case r == 'p':
return 'g'
case r == 'y':
return 'o'
case r == 't':
return 'l'
case r == 'h':
return 'a'
case r == 'o':
return 'n'
case r == 'n':
return 'g'
}
return r
}
fmt.Println(strings.Map(trans, "python")) //golang

对接I/O

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
var buf bytes.Buffer
var s = "I love Go!"

_, err := io.Copy(&buf, strings.NewReader(s))
if err != nil {
panic(err)
}
fmt.Println(buf.String()) //I love Go

buf.Reset()
var b = []byte("I love Go!")
_, err := io.Copy(&buf, bytes.NewReader(b))
if err != nil {
panic(err)
}
fmt.Println(buf.String()) //I love Go