1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
import "strings"
s := "Hello, World"
// 查找
strings.Contains(s, "World") // true
strings.HasPrefix(s, "Hello") // true
strings.HasSuffix(s, "World") // true
strings.Index(s, "World") // 7
// 变换
strings.ToUpper(s) // "HELLO, WORLD"
strings.ToLower(s) // "hello, world"
strings.TrimSpace(" hi ") // "hi"
strings.Trim("--hi--", "-") // "hi"
// 分割与合并
strings.Split("a,b,c", ",") // ["a", "b", "c"]
strings.Join([]string{"a","b"}, "-") // "a-b"
// 替换
strings.Replace("foo bar foo", "foo", "baz", 1) // "baz bar foo"(替换1次)
strings.ReplaceAll("foo bar foo", "foo", "baz") // "baz bar baz"
// 重复
strings.Repeat("Go", 3) // "GoGoGo"
|