@@ -10,76 +10,76 @@ The following functions are from the `strings` package. See the official documen
10
10
11
11
Check if string `s` contains string `substr`, returns a boolean value.
12
12
``` Go
13
- fmt.Println (strings.Contains (" seafood" , " foo" ))
14
- fmt.Println (strings.Contains (" seafood" , " bar" ))
15
- fmt.Println (strings.Contains (" seafood" , " " ))
16
- fmt.Println (strings.Contains (" " , " " ))
17
-
18
- // Output:
19
- // true
20
- // false
21
- // true
22
- // true
13
+ fmt.Println (strings.Contains (" seafood" , " foo" ))
14
+ fmt.Println (strings.Contains (" seafood" , " bar" ))
15
+ fmt.Println (strings.Contains (" seafood" , " " ))
16
+ fmt.Println (strings.Contains (" " , " " ))
17
+
18
+ // Output:
19
+ // true
20
+ // false
21
+ // true
22
+ // true
23
23
```
24
24
- func Join(a [ ] string, sep string) string
25
25
26
26
Combine strings from slice with separator `sep`.
27
27
``` Go
28
- s := []string {" foo" , " bar" , " baz" }
29
- fmt.Println (strings.Join (s, " , " ))
30
- // Output:foo, bar, baz
28
+ s := []string {" foo" , " bar" , " baz" }
29
+ fmt.Println (strings.Join (s, " , " ))
30
+ // Output:foo, bar, baz
31
31
```
32
32
- func Index(s, sep string) int
33
33
34
34
Find index of `sep` in string `s`, returns -1 if it's not found.
35
35
``` Go
36
- fmt.Println (strings.Index (" chicken" , " ken" ))
37
- fmt.Println (strings.Index (" chicken" , " dmr" ))
38
- // Output:4
39
- // -1
36
+ fmt.Println (strings.Index (" chicken" , " ken" ))
37
+ fmt.Println (strings.Index (" chicken" , " dmr" ))
38
+ // Output:4
39
+ // -1
40
40
```
41
41
- func Repeat(s string, count int) string
42
42
43
43
Repeat string `s` `count` times.
44
44
``` Go
45
- fmt.Println (" ba" + strings.Repeat (" na" , 2 ))
46
- // Output:banana
45
+ fmt.Println (" ba" + strings.Repeat (" na" , 2 ))
46
+ // Output:banana
47
47
```
48
48
- func Replace(s, old, new string, n int) string
49
49
50
50
Replace string `old` with string `new` in string `s`. `n` is the number of replacements. If n is less than 0, replace all instances.
51
51
``` Go
52
- fmt.Println (strings.Replace (" oink oink oink" , " k" , " ky" , 2 ))
53
- fmt.Println (strings.Replace (" oink oink oink" , " oink" , " moo" , -1 ))
54
- // Output:oinky oinky oink
55
- // moo moo moo
52
+ fmt.Println (strings.Replace (" oink oink oink" , " k" , " ky" , 2 ))
53
+ fmt.Println (strings.Replace (" oink oink oink" , " oink" , " moo" , -1 ))
54
+ // Output:oinky oinky oink
55
+ // moo moo moo
56
56
```
57
57
- func Split(s, sep string) [ ] string
58
58
59
59
Split string `s` with separator `sep` into a slice.
60
60
``` Go
61
- fmt.Printf (" %q \n " , strings.Split (" a,b,c" , " ," ))
62
- fmt.Printf (" %q \n " , strings.Split (" a man a plan a canal panama" , " a " ))
63
- fmt.Printf (" %q \n " , strings.Split (" xyz " , " " ))
64
- fmt.Printf (" %q \n " , strings.Split (" " , " Bernardo O'Higgins" ))
65
- // Output:["a" "b" "c"]
66
- // ["" "man " "plan " "canal panama"]
67
- // [" " "x" "y" "z" " "]
68
- // [""]
61
+ fmt.Printf (" %q \n " , strings.Split (" a,b,c" , " ," ))
62
+ fmt.Printf (" %q \n " , strings.Split (" a man a plan a canal panama" , " a " ))
63
+ fmt.Printf (" %q \n " , strings.Split (" xyz " , " " ))
64
+ fmt.Printf (" %q \n " , strings.Split (" " , " Bernardo O'Higgins" ))
65
+ // Output:["a" "b" "c"]
66
+ // ["" "man " "plan " "canal panama"]
67
+ // [" " "x" "y" "z" " "]
68
+ // [""]
69
69
```
70
70
- func Trim(s string, cutset string) string
71
71
72
72
Remove `cutset` of string `s` if it's leftmost or rightmost.
73
73
``` Go
74
- fmt.Printf (" [%q ]" , strings.Trim (" !!! Achtung !!! " , " ! " ))
75
- Output :[" Achtung" ]
74
+ fmt.Printf (" [%q ]" , strings.Trim (" !!! Achtung !!! " , " ! " ))
75
+ Output:[" Achtung" ]
76
76
```
77
77
- func Fields(s string) [ ] string
78
78
79
79
Remove space items and split string with space into a slice.
80
80
``` Go
81
- fmt.Printf (" Fields are: %q " , strings.Fields (" foo bar baz " ))
82
- // Output:Fields are: ["foo" "bar" "baz"]
81
+ fmt.Printf (" Fields are: %q " , strings.Fields (" foo bar baz " ))
82
+ // Output:Fields are: ["foo" "bar" "baz"]
83
83
```
84
84
85
85
## strconv
@@ -88,72 +88,72 @@ The following functions are from the `strconv` package. As usual, please see off
88
88
89
89
- Append series, convert data to string, and append to current byte slice.
90
90
``` Go
91
- package main
92
-
93
- import (
94
- " fmt"
95
- " strconv"
96
- )
97
-
98
- func main () {
99
- str := make ([]byte , 0 , 100 )
100
- str = strconv.AppendInt (str, 4567 , 10 )
101
- str = strconv.AppendBool (str, false )
102
- str = strconv.AppendQuote (str, " abcdefg" )
103
- str = strconv.AppendQuoteRune (str, ' 单' )
104
- fmt.Println (string (str))
105
- }
91
+ package main
92
+
93
+ import (
94
+ " fmt"
95
+ " strconv"
96
+ )
97
+
98
+ func main () {
99
+ str := make ([]byte , 0 , 100 )
100
+ str = strconv.AppendInt (str, 4567 , 10 )
101
+ str = strconv.AppendBool (str, false )
102
+ str = strconv.AppendQuote (str, " abcdefg" )
103
+ str = strconv.AppendQuoteRune (str, ' 单' )
104
+ fmt.Println (string (str))
105
+ }
106
106
```
107
107
- Format series, convert other data types into string.
108
108
``` Go
109
- package main
110
-
111
- import (
112
- " fmt"
113
- " strconv"
114
- )
115
-
116
- func main () {
117
- a := strconv.FormatBool (false )
118
- b := strconv.FormatFloat (123.23 , ' g' , 12 , 64 )
119
- c := strconv.FormatInt (1234 , 10 )
120
- d := strconv.FormatUint (12345 , 10 )
121
- e := strconv.Itoa (1023 )
122
- fmt.Println (a, b, c, d, e)
123
- }
109
+ package main
110
+
111
+ import (
112
+ " fmt"
113
+ " strconv"
114
+ )
115
+
116
+ func main () {
117
+ a := strconv.FormatBool (false )
118
+ b := strconv.FormatFloat (123.23 , ' g' , 12 , 64 )
119
+ c := strconv.FormatInt (1234 , 10 )
120
+ d := strconv.FormatUint (12345 , 10 )
121
+ e := strconv.Itoa (1023 )
122
+ fmt.Println (a, b, c, d, e)
123
+ }
124
124
```
125
125
- Parse series, convert strings to other types.
126
126
``` Go
127
- package main
128
-
129
- import (
130
- " fmt"
131
- " strconv"
132
- )
133
-
134
- func main () {
135
- a , err := strconv.ParseBool (" false" )
136
- if err != nil {
137
- fmt.Println (err)
138
- }
139
- b , err := strconv.ParseFloat (" 123.23" , 64 )
140
- if err != nil {
141
- fmt.Println (err)
142
- }
143
- c , err := strconv.ParseInt (" 1234" , 10 , 64 )
144
- if err != nil {
145
- fmt.Println (err)
146
- }
147
- d , err := strconv.ParseUint (" 12345" , 10 , 64 )
148
- if err != nil {
149
- fmt.Println (err)
150
- }
151
- e , err := strconv.Itoa (" 1023" )
152
- if err != nil {
153
- fmt.Println (err)
154
- }
155
- fmt.Println (a, b, c, d, e)
127
+ package main
128
+
129
+ import (
130
+ " fmt"
131
+ " strconv"
132
+ )
133
+
134
+ func main () {
135
+ a , err := strconv.ParseBool (" false" )
136
+ if err != nil {
137
+ fmt.Println (err)
138
+ }
139
+ b , err := strconv.ParseFloat (" 123.23" , 64 )
140
+ if err != nil {
141
+ fmt.Println (err)
142
+ }
143
+ c , err := strconv.ParseInt (" 1234" , 10 , 64 )
144
+ if err != nil {
145
+ fmt.Println (err)
146
+ }
147
+ d , err := strconv.ParseUint (" 12345" , 10 , 64 )
148
+ if err != nil {
149
+ fmt.Println (err)
150
+ }
151
+ e , err := strconv.Itoa (" 1023" )
152
+ if err != nil {
153
+ fmt.Println (err)
156
154
}
155
+ fmt.Println (a, b, c, d, e)
156
+ }
157
157
```
158
158
## Links
159
159
0 commit comments