字符串方法
- 字符串是否包含子字符串
fmt.Println("Contains:", strings.Contains("test", "es"))
Contains: true
- 字符串包含子字符串数量
fmt.Println("Count:", strings.Count("test", "t"))
Count: 2
- 字符串是否包含前缀
fmt.Println("HasPrefix:", strings.HasPrefix("test", "te"))
HasPrefix: true
- 字符串是否包含后缀
fmt.Println("HasSuffix:", strings.HasSuffix("test", "st"))
HasSuffix: true
- 字符串中子字符串的位置
fmt.Println("Index:", strings.Index("test", "e"))
Index: 1
- 字符串拼接
fmt.Println("Join:", strings.Join([]string{"a", "b"}, "-"))
Join: a-b
- 字符串重复
fmt.Println("Repeat:", strings.Repeat("a", 5))
Repeat: aaaaa
- 字符串替换子字符串
fmt.Println("Replace:", strings.Replace("foo", "o", "0", -1))
fmt.Println("Replace:", strings.Replace("foo", "o", "0", 1))
Replace: f00
Replace: f0o
第4个参数表示替换的数量。参数小于0,即没有数量限制,否则,仅替换指定数量的子字符串。
- 字符串分割
fmt.Println("Split:", strings.Split("a-b-c-d-e", "-"))
Split: [a b c d e]
- 字符串大小写转换
fmt.Println("ToLower:", strings.ToLower("TEST"))
fmt.Println("ToUpper:", strings.ToUpper("test"))
ToLower: test
ToUpper: TEST
字符串格式化
- 格式化结构体
p := point{1, 2}
fmt.Printf("struct1: %v\n", p)
fmt.Printf("struct2: %+v\n", p)
fmt.Printf("struct3: %#v\n", p)
struct1: {1 2}
struct2: {x:1 y:2}
struct3: main.point{x:1, y:2}
- 格式化数据类型
fmt.Printf("type: %T\n", "string")
fmt.Printf("type: %T\n", rune(10))
fmt.Printf("type: %T\n", byte(10))
fmt.Printf("type: %T\n", int64(10))
fmt.Printf("type: %T\n", float64(10))
fmt.Printf("type: %T\n", point{1, 2})
type: string
type: int32
type: uint8
type: int64
type: float64
type: main.point
- 格式化基本类型数据
fmt.Printf("bool: %t\n", true)
fmt.Printf("int: %d\n", 123)
fmt.Printf("bin: %b\n", 14)
fmt.Printf("char: %c\n", 33)
fmt.Printf("hex: %x\n", 456)
fmt.Printf("float1: %f\n", 78.9)
fmt.Printf("float2: %e\n", 123400000.0)
fmt.Printf("float3: %E\n", 123400000.0)
bool: true
int: 123
bin: 1110
char: !
hex: 1c8
float1: 78.900000
float2: 1.234000e+08
float3: 1.234000E+08
- 格式化字符串类型数据
fmt.Printf("str1: %s\n", "\"string\"")
fmt.Printf("str2: %q\n", "\"string\"")
fmt.Printf("str3: %v\n", "\"string\"")
fmt.Printf("str4: %x\n", "hex this")
str1: “string”
str2: ““string””
str3: “string”
str4: 6865782074686973
- 格式化指针
p := point{1, 2}
fmt.Printf("pointer: %p\n", &p)
pointer: 0xc0000b2010
- 格式化对齐
fmt.Printf("width1: |%6d|%6d|\n", 12, 345)
fmt.Printf("width2: |%6.2f|%6.2f|\n", 1.2, 3.45)
fmt.Printf("width3: |%-6.2f|%-6.2f|\n", 1.2, 3.45)
fmt.Printf("width4: |%6s|%6s|\n", "foo", "b")
fmt.Printf("width5: |%-6s|%-6s|\n", "foo", "b")
width1: | 12| 345|
width2: | 1.20| 3.45|
width3: |1.20 |3.45 |
width4: | foo| b|
width5: |foo |b |
- 格式化字符串获取
s := fmt.Sprintf("sprintf: a %s", "string")
fmt.Println(s)
sprintf: a string
- 格式化字符串并写入命令行
fmt.Fprintf(os.Stderr, "io: an %s\n", "error")
io: an error
字符串长度
s := "学而不思则罔,思而不学则殆。"
fmt.Println(len(s))
42
fmt.Println(utf8.RuneCountInString(s))
14
获取字符串长度应使用utf8.RuneCountInString
字符串遍历
- 直接遍历
for i := 0; i < len(s); i++ {
fmt.Printf("%x ", s[i])
}
e5 ad a6 e8 80 8c e4 b8 8d e6 80 9d e5 88 99 e7 bd 94 ef bc 8c e6 80 9d e8 80 8c e4 b8 8d e5 ad a6 e5 88 99 e6 ae 86 e3 80 82
range
迭代
for i, v := range s {
fmt.Printf("%d -> %#U\n", i, v)
}
0 -> U+5B66 ‘学’
3 -> U+800C ‘而’
6 -> U+4E0D ‘不’
9 -> U+601D ‘思’
12 -> U+5219 ‘则’
15 -> U+7F54 ‘罔’
18 -> U+FF0C ‘,’
21 -> U+601D ‘思’
24 -> U+800C ‘而’
27 -> U+4E0D ‘不’
30 -> U+5B66 ‘学’
33 -> U+5219 ‘则’
36 -> U+6B86 ‘殆’
39 -> U+3002 ‘。’
utf8.DecodeRuneInString
获取字符宽度
for i := 0; i < len(s); {
r, size := utf8.DecodeRuneInString(s[i:])
fmt.Printf("%d -> %#U\n", i, r)
i += size
}
0 -> U+5B66 ‘学’
3 -> U+800C ‘而’
6 -> U+4E0D ‘不’
9 -> U+601D ‘思’
12 -> U+5219 ‘则’
15 -> U+7F54 ‘罔’
18 -> U+FF0C ‘,’
21 -> U+601D ‘思’
24 -> U+800C ‘而’
27 -> U+4E0D ‘不’
30 -> U+5B66 ‘学’
33 -> U+5219 ‘则’
36 -> U+6B86 ‘殆’
39 -> U+3002 ‘。’
从以上运行结果得出结论,建议使用range
或utf8.DecodeRuneInString
。
字符比较
func runeEqual(r rune) {
if r == '学' {
fmt.Println("it is 学")
} else if r == '思' {
fmt.Println("it is 思")
} else {
fmt.Printf("it is not 学 or 思, it is %#U\n", r)
}
}
for _, v := range s {
runeEqual(v)
}
it is 学
it is not 学 or 思, it is U+800C ‘而’
it is not 学 or 思, it is U+4E0D ‘不’
it is 思
it is not 学 or 思, it is U+5219 ‘则’
it is not 学 or 思, it is U+7F54 ‘罔’
it is not 学 or 思, it is U+FF0C ‘,’
it is 思
it is not 学 or 思, it is U+800C ‘而’
it is not 学 or 思, it is U+4E0D ‘不’
it is 学
it is not 学 or 思, it is U+5219 ‘则’
it is not 学 or 思, it is U+6B86 ‘殆’
it is not 学 or 思, it is U+3002 ‘。’