|
9 | 9 |
|
10 | 10 | ```Go
|
11 | 11 |
|
12 |
| - fmt.Println(strings.Contains("seafood", "foo")) |
13 |
| - fmt.Println(strings.Contains("seafood", "bar")) |
14 |
| - fmt.Println(strings.Contains("seafood", "")) |
15 |
| - fmt.Println(strings.Contains("", "")) |
16 |
| - //Output: |
17 |
| - //true |
18 |
| - //false |
19 |
| - //true |
20 |
| - //true |
| 12 | +fmt.Println(strings.Contains("seafood", "foo")) |
| 13 | +fmt.Println(strings.Contains("seafood", "bar")) |
| 14 | +fmt.Println(strings.Contains("seafood", "")) |
| 15 | +fmt.Println(strings.Contains("", "")) |
| 16 | +//Output: |
| 17 | +//true |
| 18 | +//false |
| 19 | +//true |
| 20 | +//true |
21 | 21 |
|
22 | 22 | ```
|
23 | 23 |
|
|
27 | 27 |
|
28 | 28 | ```Go
|
29 | 29 |
|
30 |
| - s := []string{"foo", "bar", "baz"} |
31 |
| - fmt.Println(strings.Join(s, ", ")) |
32 |
| - //Output:foo, bar, baz |
| 30 | +s := []string{"foo", "bar", "baz"} |
| 31 | +fmt.Println(strings.Join(s, ", ")) |
| 32 | +//Output:foo, bar, baz |
33 | 33 | ```
|
34 | 34 |
|
35 | 35 | - func Index(s, sep string) int
|
|
38 | 38 |
|
39 | 39 | ```Go
|
40 | 40 |
|
41 |
| - fmt.Println(strings.Index("chicken", "ken")) |
42 |
| - fmt.Println(strings.Index("chicken", "dmr")) |
43 |
| - //Output:4 |
44 |
| - //-1 |
| 41 | +fmt.Println(strings.Index("chicken", "ken")) |
| 42 | +fmt.Println(strings.Index("chicken", "dmr")) |
| 43 | +//Output:4 |
| 44 | +//-1 |
45 | 45 | ```
|
46 | 46 | - func Repeat(s string, count int) string
|
47 | 47 |
|
48 | 48 | 重复s字符串count次,最后返回重复的字符串
|
49 | 49 |
|
50 | 50 | ```Go
|
51 | 51 |
|
52 |
| - fmt.Println("ba" + strings.Repeat("na", 2)) |
53 |
| - //Output:banana |
| 52 | +fmt.Println("ba" + strings.Repeat("na", 2)) |
| 53 | +//Output:banana |
54 | 54 | ```
|
55 | 55 | - func Replace(s, old, new string, n int) string
|
56 | 56 |
|
57 | 57 | 在s字符串中,把old字符串替换为new字符串,n表示替换的次数,小于0表示全部替换
|
58 | 58 |
|
59 | 59 | ```Go
|
60 | 60 |
|
61 |
| - fmt.Println(strings.Replace("oink oink oink", "k", "ky", 2)) |
62 |
| - fmt.Println(strings.Replace("oink oink oink", "oink", "moo", -1)) |
63 |
| - //Output:oinky oinky oink |
64 |
| - //moo moo moo |
| 61 | +fmt.Println(strings.Replace("oink oink oink", "k", "ky", 2)) |
| 62 | +fmt.Println(strings.Replace("oink oink oink", "oink", "moo", -1)) |
| 63 | +//Output:oinky oinky oink |
| 64 | +//moo moo moo |
65 | 65 | ```
|
66 | 66 | - func Split(s, sep string) []string
|
67 | 67 |
|
68 | 68 | 把s字符串按照sep分割,返回slice
|
69 | 69 |
|
70 | 70 | ```Go
|
71 | 71 |
|
72 |
| - fmt.Printf("%q\n", strings.Split("a,b,c", ",")) |
73 |
| - fmt.Printf("%q\n", strings.Split("a man a plan a canal panama", "a ")) |
74 |
| - fmt.Printf("%q\n", strings.Split(" xyz ", "")) |
75 |
| - fmt.Printf("%q\n", strings.Split("", "Bernardo O'Higgins")) |
76 |
| - //Output:["a" "b" "c"] |
77 |
| - //["" "man " "plan " "canal panama"] |
78 |
| - //[" " "x" "y" "z" " "] |
79 |
| - //[""] |
| 72 | +fmt.Printf("%q\n", strings.Split("a,b,c", ",")) |
| 73 | +fmt.Printf("%q\n", strings.Split("a man a plan a canal panama", "a ")) |
| 74 | +fmt.Printf("%q\n", strings.Split(" xyz ", "")) |
| 75 | +fmt.Printf("%q\n", strings.Split("", "Bernardo O'Higgins")) |
| 76 | +//Output:["a" "b" "c"] |
| 77 | +//["" "man " "plan " "canal panama"] |
| 78 | +//[" " "x" "y" "z" " "] |
| 79 | +//[""] |
80 | 80 | ```
|
81 | 81 |
|
82 | 82 | - func Trim(s string, cutset string) string
|
|
85 | 85 |
|
86 | 86 | ```Go
|
87 | 87 |
|
88 |
| - fmt.Printf("[%q]", strings.Trim(" !!! Achtung !!! ", "! ")) |
89 |
| - //Output:["Achtung"] |
| 88 | +fmt.Printf("[%q]", strings.Trim(" !!! Achtung !!! ", "! ")) |
| 89 | +//Output:["Achtung"] |
90 | 90 | ```
|
91 | 91 |
|
92 | 92 | - func Fields(s string) []string
|
|
95 | 95 |
|
96 | 96 | ```Go
|
97 | 97 |
|
98 |
| - fmt.Printf("Fields are: %q", strings.Fields(" foo bar baz ")) |
99 |
| - //Output:Fields are: ["foo" "bar" "baz"] |
| 98 | +fmt.Printf("Fields are: %q", strings.Fields(" foo bar baz ")) |
| 99 | +//Output:Fields are: ["foo" "bar" "baz"] |
100 | 100 | ```
|
101 | 101 |
|
102 | 102 | ## 字符串转换
|
|
106 | 106 |
|
107 | 107 | ```Go
|
108 | 108 |
|
109 |
| - package main |
110 |
| - |
111 |
| - import ( |
112 |
| - "fmt" |
113 |
| - "strconv" |
114 |
| - ) |
115 |
| - |
116 |
| - func main() { |
117 |
| - str := make([]byte, 0, 100) |
118 |
| - str = strconv.AppendInt(str, 4567, 10) |
119 |
| - str = strconv.AppendBool(str, false) |
120 |
| - str = strconv.AppendQuote(str, "abcdefg") |
121 |
| - str = strconv.AppendQuoteRune(str, '单') |
122 |
| - fmt.Println(string(str)) |
123 |
| - } |
| 109 | +package main |
| 110 | + |
| 111 | +import ( |
| 112 | + "fmt" |
| 113 | + "strconv" |
| 114 | +) |
| 115 | + |
| 116 | +func main() { |
| 117 | + str := make([]byte, 0, 100) |
| 118 | + str = strconv.AppendInt(str, 4567, 10) |
| 119 | + str = strconv.AppendBool(str, false) |
| 120 | + str = strconv.AppendQuote(str, "abcdefg") |
| 121 | + str = strconv.AppendQuoteRune(str, '单') |
| 122 | + fmt.Println(string(str)) |
| 123 | +} |
124 | 124 | ```
|
125 | 125 |
|
126 | 126 | - Format 系列函数把其他类型的转换为字符串
|
127 | 127 | ```Go
|
128 | 128 |
|
129 |
| - package main |
130 |
| - |
131 |
| - import ( |
132 |
| - "fmt" |
133 |
| - "strconv" |
134 |
| - ) |
135 |
| - |
136 |
| - func main() { |
137 |
| - a := strconv.FormatBool(false) |
138 |
| - b := strconv.FormatFloat(123.23, 'g', 12, 64) |
139 |
| - c := strconv.FormatInt(1234, 10) |
140 |
| - d := strconv.FormatUint(12345, 10) |
141 |
| - e := strconv.Itoa(1023) |
142 |
| - fmt.Println(a, b, c, d, e) |
143 |
| - } |
| 129 | +package main |
| 130 | + |
| 131 | +import ( |
| 132 | + "fmt" |
| 133 | + "strconv" |
| 134 | +) |
| 135 | + |
| 136 | +func main() { |
| 137 | + a := strconv.FormatBool(false) |
| 138 | + b := strconv.FormatFloat(123.23, 'g', 12, 64) |
| 139 | + c := strconv.FormatInt(1234, 10) |
| 140 | + d := strconv.FormatUint(12345, 10) |
| 141 | + e := strconv.Itoa(1023) |
| 142 | + fmt.Println(a, b, c, d, e) |
| 143 | +} |
144 | 144 |
|
145 | 145 | ```
|
146 | 146 |
|
147 | 147 | - Parse 系列函数把字符串转换为其他类型
|
148 | 148 |
|
149 | 149 | ```Go
|
150 | 150 |
|
151 |
| - package main |
152 |
| - |
153 |
| - import ( |
154 |
| - "fmt" |
155 |
| - "strconv" |
156 |
| - ) |
157 |
| - func checkError(e error){ |
158 |
| - if e != nil{ |
159 |
| - fmt.Println(e) |
160 |
| - } |
161 |
| - } |
162 |
| - func main() { |
163 |
| - a, err := strconv.ParseBool("false") |
164 |
| - checkError(err) |
165 |
| - b, err := strconv.ParseFloat("123.23", 64) |
166 |
| - checkError(err) |
167 |
| - c, err := strconv.ParseInt("1234", 10, 64) |
168 |
| - checkError(err) |
169 |
| - d, err := strconv.ParseUint("12345", 10, 64) |
170 |
| - checkError(err) |
171 |
| - e, err := strconv.Atoi("1023") |
172 |
| - checkError(err) |
173 |
| - fmt.Println(a, b, c, d, e) |
174 |
| - } |
| 151 | +package main |
| 152 | + |
| 153 | +import ( |
| 154 | + "fmt" |
| 155 | + "strconv" |
| 156 | +) |
| 157 | +func checkError(e error){ |
| 158 | + if e != nil{ |
| 159 | + fmt.Println(e) |
| 160 | + } |
| 161 | +} |
| 162 | +func main() { |
| 163 | + a, err := strconv.ParseBool("false") |
| 164 | + checkError(err) |
| 165 | + b, err := strconv.ParseFloat("123.23", 64) |
| 166 | + checkError(err) |
| 167 | + c, err := strconv.ParseInt("1234", 10, 64) |
| 168 | + checkError(err) |
| 169 | + d, err := strconv.ParseUint("12345", 10, 64) |
| 170 | + checkError(err) |
| 171 | + e, err := strconv.Atoi("1023") |
| 172 | + checkError(err) |
| 173 | + fmt.Println(a, b, c, d, e) |
| 174 | +} |
175 | 175 |
|
176 | 176 | ```
|
177 | 177 |
|
|
0 commit comments