-
Notifications
You must be signed in to change notification settings - Fork 400
/
04.5.3.md
144 lines (117 loc) · 4.97 KB
/
04.5.3.md
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# **关于字符串处理的包**
Go在`strings`包里面提供了很多操作UTF-8字符串的强大工具,大部分的工具将会在`useStrings.go`介绍,与文件IO相关的 操作将会在*第8章*详细介绍。
`useStrings.go`的第一部分代码:
> ```go
> package main
>
> import (
> "fmt"
> s "strings"
> "unicode"
> )
>
> var f = fmt.Printf
> ```
> `s "strings"`的导包方式是非常推荐的,意味着我们给strings包起了一个简短的包名,这样就可以使用`s.FunctionName()`而不必使用`strings.FunctionName()`。
当你频繁使用某个包中的函数的时候,也可以将该函数赋值给一个变量,像上面的代码一样,`var f = fmt.Printf`,接下来就可以用`f`代替`fmt.Printf()`。要注意的是不要过度使用该特性,不然你的代码可读性会变得很差。
第二部分代码:
> ```go
> unc main() {
> upper := s.ToUpper("Hello there!")
> f("To Upper: %s\n", upper)
> f("To Lower: %s\n", s.ToLower("Hello THERE"))
>
> f("%s\n", s.Title("tHis wiLL be A title!"))
>
> f("EqualFold: %v\n", s.EqualFold("Mihalis", "MIHAlis"))
> f("EqualFold: %v\n", s.EqualFold("Mihalis", "MIHAli"))
> ```
这段代码提供了很多玩转字符串的例子,`strings.EqualFold()`函数能够判断两个字符串是否相同。
第三部分代码:
> ```go
> f("Prefix: %v\n", s.HasPrefix("Mihalis", "Mi"))
> f("Prefix: %v\n", s.HasPrefix("Mihalis", "mi"))
> f("Suffix: %v\n", s.HasSuffix("Mihalis", "is"))
> f("Suffix: %v\n", s.HasSuffix("Mihalis", "IS"))
>
> f("Index: %v\n", s.Index("Mihalis", "ha"))
> f("Index: %v\n", s.Index("Mihalis", "Ha"))
> f("Count: %v\n", s.Count("Mihalis", "i"))
> f("Count: %v\n", s.Count("Mihalis", "I"))
> f("Repeat: %s\n", s.Repeat("ab", 5))
>
> f("TrimSpace: %s\n", s.TrimSpace(" \tThis is a line. \n"))
> f("TrimLeft: %s", s.TrimLeft(" \tThis is a\t line. \n", "\n\t "))
> f("TrimRight: %s\n", s.TrimRight(" \tThis is a\t line. \n", "\n\t "))
> ```
`strings.Count()`函数能够计算第二个参数(字符串类型),在第一个参数(字符串类型)中出现的次数;`strings.HasPrefix()`函数判断字符串是否是以某字符串开头,如果是的话便返回`true`;同样,`strings.HasSuffix()`判断字符串是否以某字符串结尾。
第四部分代码:
> ```go
> f("Compare: %v\n", s.Compare("Mihalis", "MIHALIS"))
> f("Compare: %v\n", s.Compare("Mihalis", "Mihalis"))
> f("Compare: %v\n", s.Compare("MIHALIS", "MIHalis"))
>
> f("Fields: %v\n", s.Fields("This is a string!"))
> f("Fields: %v\n", s.Fields("Thisis\na\tstring!"))
>
> f("%s\n", s.Split("abcd efg", ""))
> f("%s\n", s.Replace("abcd efg", "", "_", -1))
> f("%s\n", s.Replace("abcd efg", "", "_", 4))
> f("%s\n", s.Replace("abcd efg", "", "_", 2))
> ```
这部分展示了`strings`比较高级的一些函数。`strings.Split()`能够以特定字符分割字符串,并返回一个**字符串切片**。
`strings.Compare()`函数用于比较两个字符串是否相等,相等返回`true`,否则返回1或者-1(strings.Compare(a,b),如果a<b则返回1,a<b返回-1,所谓a>b,意思是出现在字母表中的顺序,译者加)。
`strings.Fields()`按照空格将字符串分割。
> `strings.Split()`非常有用,你迟早会在自己的程序中用到它!
最后一部分代码:
> ```go
> lines := []string{"Line 1", "Line 2", "Line 3"}
> f("Join: %s\n", s.Join(lines, "+++"))
>
> f("SplitAfter: %s\n", s.SplitAfter("123++432++", "++"))
>
> trimFunction := func(c rune) bool {
> return !unicode.IsLetter(c)
> }
> f("TrimFunc: %s\n", s.TrimFunc("123 abc ABC \t .", trimFunction))
> }
> ```
最后一部分代码提供的方法非常好理解,而且功能强大:
`strings.Replace()`函数需要4个参数,第一个是你要处理的字符串,第二个是你准备替换的字符,第三个是你要用该字符去替换,第四个参数是你要替换的数量,如果是`-1`就意味着你会替换所有要替换的字符。
`strings.TrimFunc()`函数可按照自定义函数获取你感兴趣的内容;
`strings.SplitAfter()`函数基于指定分隔符将字符串分割。
执行`useStrings.go`的输出如下:
> $ go run useStrings.go
>
> ```shell
> To Upper: HELLO THERE!
> To Lower: hello there
> THis WiLL Be A Title!
> EqualFold: true
> EqualFold: false
> Prefix: true
> Prefix: false
> Suffix: true
> Suffix: false
> Index: 2
> Index: -1
> Count: 2
> Count: 0
> Repeat: ababababab
> TrimSpace: This is a line.
> TrimLeft: This is a line.
> TrimRight: This is a line.
> Compare: 1
> Compare: 0
> Compare: -1
> Fields: [This is a string!]
> Fields: [Thisis a string!]
> [a b c d e f g]
> _a_b_c_d_ _e_f_g_
> _a_b_c_d efg
> _a_bcd efg
> Join: Line 1+++Line 2+++Line 3
> SplitAfter: [123++ 432++ ]
> TrimFunc: abc ABC
> ```
官方`strings`包中的方法远比`useStrings.go`中所展示的多,如果你正在写的代码是关于文本处理的,那么你应该阅读[]`strings`的官方文档](https://golang.org/pkg/strings)以了解更多。