-
Notifications
You must be signed in to change notification settings - Fork 8
/
string-functions.html
205 lines (160 loc) · 11.6 KB
/
string-functions.html
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Go by Example 中文版: 字符串函数</title>
<link rel=stylesheet href="site.css">
</head>
<script>
onkeydown = (e) => {
if (e.key == "ArrowLeft") {
window.location.href = 'recover';
}
if (e.key == "ArrowRight") {
window.location.href = 'string-formatting';
}
}
</script>
<body>
<div class="example" id="string-functions">
<h2><a href="./">Go by Example 中文版</a>: 字符串函数</h2>
<table>
<tr>
<td class="docs">
<p>标准库的 <code>strings</code> 包提供了很多有用的字符串相关的函数。
这儿有一些用来让你对 <code>strings</code> 包有一个初步了解的例子。</p>
</td>
<td class="code empty leading">
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<a href="https://play.studygolang.com/p/fxOwKEDxUyX"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
<pre class="chroma"><span class="kn">package</span> <span class="nx">main</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"><span class="kn">import</span> <span class="p">(</span>
<span class="s">"fmt"</span>
<span class="nx">s</span> <span class="s">"strings"</span>
<span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>我们给 <code>fmt.Println</code> 一个较短的别名,
因为我们随后会大量的使用它。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="kd">var</span> <span class="nx">p</span> <span class="p">=</span> <span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"><span class="kd">func</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>这是一些 <code>strings</code> 中有用的函数例子。
由于它们都是包的函数,而不是字符串对象自身的方法,
这意味着我们需要在调用函数时,将字符串作为第一个参数进行传递。
你可以在 <a href="http://golang.org/pkg/strings/"><code>strings</code></a> 包文档中找到更多的函数。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nf">p</span><span class="p">(</span><span class="s">"Contains: "</span><span class="p">,</span> <span class="nx">s</span><span class="p">.</span><span class="nf">Contains</span><span class="p">(</span><span class="s">"test"</span><span class="p">,</span> <span class="s">"es"</span><span class="p">))</span>
<span class="nf">p</span><span class="p">(</span><span class="s">"Count: "</span><span class="p">,</span> <span class="nx">s</span><span class="p">.</span><span class="nf">Count</span><span class="p">(</span><span class="s">"test"</span><span class="p">,</span> <span class="s">"t"</span><span class="p">))</span>
<span class="nf">p</span><span class="p">(</span><span class="s">"HasPrefix: "</span><span class="p">,</span> <span class="nx">s</span><span class="p">.</span><span class="nf">HasPrefix</span><span class="p">(</span><span class="s">"test"</span><span class="p">,</span> <span class="s">"te"</span><span class="p">))</span>
<span class="nf">p</span><span class="p">(</span><span class="s">"HasSuffix: "</span><span class="p">,</span> <span class="nx">s</span><span class="p">.</span><span class="nf">HasSuffix</span><span class="p">(</span><span class="s">"test"</span><span class="p">,</span> <span class="s">"st"</span><span class="p">))</span>
<span class="nf">p</span><span class="p">(</span><span class="s">"Index: "</span><span class="p">,</span> <span class="nx">s</span><span class="p">.</span><span class="nf">Index</span><span class="p">(</span><span class="s">"test"</span><span class="p">,</span> <span class="s">"e"</span><span class="p">))</span>
<span class="nf">p</span><span class="p">(</span><span class="s">"Join: "</span><span class="p">,</span> <span class="nx">s</span><span class="p">.</span><span class="nf">Join</span><span class="p">([]</span><span class="kt">string</span><span class="p">{</span><span class="s">"a"</span><span class="p">,</span> <span class="s">"b"</span><span class="p">},</span> <span class="s">"-"</span><span class="p">))</span>
<span class="nf">p</span><span class="p">(</span><span class="s">"Repeat: "</span><span class="p">,</span> <span class="nx">s</span><span class="p">.</span><span class="nf">Repeat</span><span class="p">(</span><span class="s">"a"</span><span class="p">,</span> <span class="mi">5</span><span class="p">))</span>
<span class="nf">p</span><span class="p">(</span><span class="s">"Replace: "</span><span class="p">,</span> <span class="nx">s</span><span class="p">.</span><span class="nf">Replace</span><span class="p">(</span><span class="s">"foo"</span><span class="p">,</span> <span class="s">"o"</span><span class="p">,</span> <span class="s">"0"</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">))</span>
<span class="nf">p</span><span class="p">(</span><span class="s">"Replace: "</span><span class="p">,</span> <span class="nx">s</span><span class="p">.</span><span class="nf">Replace</span><span class="p">(</span><span class="s">"foo"</span><span class="p">,</span> <span class="s">"o"</span><span class="p">,</span> <span class="s">"0"</span><span class="p">,</span> <span class="mi">1</span><span class="p">))</span>
<span class="nf">p</span><span class="p">(</span><span class="s">"Split: "</span><span class="p">,</span> <span class="nx">s</span><span class="p">.</span><span class="nf">Split</span><span class="p">(</span><span class="s">"a-b-c-d-e"</span><span class="p">,</span> <span class="s">"-"</span><span class="p">))</span>
<span class="nf">p</span><span class="p">(</span><span class="s">"ToLower: "</span><span class="p">,</span> <span class="nx">s</span><span class="p">.</span><span class="nf">ToLower</span><span class="p">(</span><span class="s">"TEST"</span><span class="p">))</span>
<span class="nf">p</span><span class="p">(</span><span class="s">"ToUpper: "</span><span class="p">,</span> <span class="nx">s</span><span class="p">.</span><span class="nf">ToUpper</span><span class="p">(</span><span class="s">"test"</span><span class="p">))</span>
<span class="nf">p</span><span class="p">()</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>虽然不是 <code>strings</code> 的函数,但仍然值得一提的是,
获取字符串长度(以字节为单位)以及通过索引获取一个字节的机制。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nf">p</span><span class="p">(</span><span class="s">"Len: "</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="s">"hello"</span><span class="p">))</span>
<span class="nf">p</span><span class="p">(</span><span class="s">"Char:"</span><span class="p">,</span> <span class="s">"hello"</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
<span class="p">}</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>注意,上面的 <code>len</code> 以及索引工作在字节级别上。
Go 使用 UTF-8 编码字符串,因此通常按原样使用。
如果您可能使用多字节的字符,则需要使用可识别编码的操作。
详情请参考 <a href="https://blog.golang.org/strings">strings, bytes, runes and characters in Go</a>。</p>
</td>
<td class="code empty">
</td>
</tr>
</table>
<table>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"><span class="gp">$</span> go run string-functions.go
<span class="go">Contains: true
</span><span class="go">Count: 2
</span><span class="go">HasPrefix: true
</span><span class="go">HasSuffix: true
</span><span class="go">Index: 1
</span><span class="go">Join: a-b
</span><span class="go">Repeat: aaaaa
</span><span class="go">Replace: f00
</span><span class="go">Replace: f0o
</span><span class="go">Split: [a b c d e]
</span><span class="go">ToLower: test
</span><span class="go">ToUpper: TEST</span></pre>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code">
<pre class="chroma"><span class="go">Len: 5
</span><span class="go">Char: 101</span></pre>
</td>
</tr>
</table>
<p class="next">
下一个例子: <a href="string-formatting">字符串格式化</a>
</p>
<p class="footer">
<a href="https://twitter.com/mmcgrana">@mmcgrana</a> 和<a href="https://eli.thegreenplace.net">Eli Bendersky</a>编写 | <a href="https://github.com/gobyexample-cn">gobyexample-cn</a> 翻译 | <a href="https://github.com/gobyexample-cn/gobyexample/issues">反馈</a> | <a href="https://github.com/gobyexample-cn/gobyexample">源码</a> | <a href="https://github.com/mmcgrana/gobyexample#license">license</a> </p>
</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A s \"strings\"\u000A)\u000A');codeLines.push('var p \u003D fmt.Println\u000A');codeLines.push('func main() {\u000A');codeLines.push(' p(\"Contains: \", s.Contains(\"test\", \"es\"))\u000A p(\"Count: \", s.Count(\"test\", \"t\"))\u000A p(\"HasPrefix: \", s.HasPrefix(\"test\", \"te\"))\u000A p(\"HasSuffix: \", s.HasSuffix(\"test\", \"st\"))\u000A p(\"Index: \", s.Index(\"test\", \"e\"))\u000A p(\"Join: \", s.Join([]string{\"a\", \"b\"}, \"-\"))\u000A p(\"Repeat: \", s.Repeat(\"a\", 5))\u000A p(\"Replace: \", s.Replace(\"foo\", \"o\", \"0\", -1))\u000A p(\"Replace: \", s.Replace(\"foo\", \"o\", \"0\", 1))\u000A p(\"Split: \", s.Split(\"a-b-c-d-e\", \"-\"))\u000A p(\"ToLower: \", s.ToLower(\"TEST\"))\u000A p(\"ToUpper: \", s.ToUpper(\"test\"))\u000A p()\u000A');codeLines.push(' p(\"Len: \", len(\"hello\"))\u000A p(\"Char:\", \"hello\"[1])\u000A}\u000A');codeLines.push('');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>
</html>