-
Notifications
You must be signed in to change notification settings - Fork 8
/
strings-and-runes.html
282 lines (223 loc) · 13.2 KB
/
strings-and-runes.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Go by Example 中文版: 字符串和rune类型</title>
<link rel=stylesheet href="site.css">
</head>
<script>
onkeydown = (e) => {
if (e.key == "ArrowLeft") {
window.location.href = 'pointers';
}
if (e.key == "ArrowRight") {
window.location.href = 'structs';
}
}
</script>
<body>
<div class="example" id="strings-and-runes">
<h2><a href="./">Go by Example 中文版</a>: 字符串和rune类型</h2>
<table>
<tr>
<td class="docs">
<p>Go语言中的字符串是一个只读的byte类型的切片。
Go语言和标准库特别对待字符串 - 作为以
<a href="https://en.wikipedia.org/wiki/UTF-8">UTF-8</a> 为编码的文本容器。
在其他语言当中, 字符串由”字符”组成。
在Go语言当中,字符的概念被称为 <code>rune</code> - 它是一个表示
Unicode 编码的整数。
<a href="https://go.dev/blog/strings">这个Go博客</a> 很好的介绍了这个主题。</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/CikqIUP5w0x"><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="s">"unicode/utf8"</span>
<span class="p">)</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>s</code> 是一个 <code>string</code> 分配了一个 literal value
表示泰语中的单词 “hello” 。
Go 字符串是 UTF-8 编码的文本。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="kd">const</span> <span class="nx">s</span> <span class="p">=</span> <span class="s">"สวัสดี"</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>因为字符串等价于 <code>[]byte</code>,
这会产生存储在其中的原始字节的长度。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</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="nx">s</span><span class="p">))</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>对字符串进行索引会在每个索引处生成原始字节值。
这个循环生成构成<code>s</code>中 Unicode 的所有字节的十六进制值。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="k">for</span> <span class="nx">i</span> <span class="o">:=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="p"><</span> <span class="nb">len</span><span class="p">(</span><span class="nx">s</span><span class="p">);</span> <span class="nx">i</span><span class="o">++</span> <span class="p">{</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Printf</span><span class="p">(</span><span class="s">"%x "</span><span class="p">,</span> <span class="nx">s</span><span class="p">[</span><span class="nx">i</span><span class="p">])</span>
<span class="p">}</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">()</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>要计算字符串中有多少rune,我们可以使用<code>utf8</code>包。
注意<code>RuneCountInString</code>的运行时取决于字符串的大小。
因为它必须按顺序解码每个 UTF-8 rune。
一些泰语字符由多个 UTF-8 code point 表示,
所以这个计数的结果可能会令人惊讶。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="s">"Rune count:"</span><span class="p">,</span> <span class="nx">utf8</span><span class="p">.</span><span class="nf">RuneCountInString</span><span class="p">(</span><span class="nx">s</span><span class="p">))</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p><code>range</code> 循环专门处理字符串并解码每个 <code>rune</code> 及其在字符串中的偏移量。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="k">for</span> <span class="nx">idx</span><span class="p">,</span> <span class="nx">runeValue</span> <span class="o">:=</span> <span class="k">range</span> <span class="nx">s</span> <span class="p">{</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Printf</span><span class="p">(</span><span class="s">"%#U starts at %d\n"</span><span class="p">,</span> <span class="nx">runeValue</span><span class="p">,</span> <span class="nx">idx</span><span class="p">)</span>
<span class="p">}</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>我们可以通过显式使用 <code>utf8.DecodeRuneInString</code> 函数来实现相同的迭代。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="s">"\nUsing DecodeRuneInString"</span><span class="p">)</span>
<span class="k">for</span> <span class="nx">i</span><span class="p">,</span> <span class="nx">w</span> <span class="o">:=</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="p"><</span> <span class="nb">len</span><span class="p">(</span><span class="nx">s</span><span class="p">);</span> <span class="nx">i</span> <span class="o">+=</span> <span class="nx">w</span> <span class="p">{</span>
<span class="nx">runeValue</span><span class="p">,</span> <span class="nx">width</span> <span class="o">:=</span> <span class="nx">utf8</span><span class="p">.</span><span class="nf">DecodeRuneInString</span><span class="p">(</span><span class="nx">s</span><span class="p">[</span><span class="nx">i</span><span class="p">:])</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Printf</span><span class="p">(</span><span class="s">"%#U starts at %d\n"</span><span class="p">,</span> <span class="nx">runeValue</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span>
<span class="nx">w</span> <span class="p">=</span> <span class="nx">width</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>这演示了将 <code>rune</code> value 传递给函数。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nf">examineRune</span><span class="p">(</span><span class="nx">runeValue</span><span class="p">)</span>
<span class="p">}</span>
<span class="p">}</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"><span class="kd">func</span> <span class="nf">examineRune</span><span class="p">(</span><span class="nx">r</span> <span class="kt">rune</span><span class="p">)</span> <span class="p">{</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>用单引号括起来的值是 <em>rune literals</em>.
我们可以直接将 <code>rune</code> value 与 rune literal 进行比较。</p>
</td>
<td class="code">
<pre class="chroma">
<span class="k">if</span> <span class="nx">r</span> <span class="o">==</span> <span class="sc">'t'</span> <span class="p">{</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="s">"found tee"</span><span class="p">)</span>
<span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="nx">r</span> <span class="o">==</span> <span class="sc">'ส'</span> <span class="p">{</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="s">"found so sua"</span><span class="p">)</span>
<span class="p">}</span>
<span class="p">}</span>
</pre>
</td>
</tr>
</table>
<table>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"><span class="gp">$</span> go run strings-and-runes.go
<span class="go">Len: 18
</span><span class="go">e0 b8 aa e0 b8 a7 e0 b8 b1 e0 b8 aa e0 b8 94 e0 b8 b5
</span><span class="go">Rune count: 6
</span><span class="go">U+0E2A 'ส' starts at 0
</span><span class="go">U+0E27 'ว' starts at 3
</span><span class="go">U+0E31 'ั' starts at 6
</span><span class="go">U+0E2A 'ส' starts at 9
</span><span class="go">U+0E14 'ด' starts at 12
</span><span class="go">U+0E35 'ี' starts at 15</span></pre>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code">
<pre class="chroma"><span class="go">Using DecodeRuneInString
</span><span class="go">U+0E2A 'ส' starts at 0
</span><span class="go">found so sua
</span><span class="go">U+0E27 'ว' starts at 3
</span><span class="go">U+0E31 'ั' starts at 6
</span><span class="go">U+0E2A 'ส' starts at 9
</span><span class="go">found so sua
</span><span class="go">U+0E14 'ด' starts at 12
</span><span class="go">U+0E35 'ี' starts at 15</span></pre>
</td>
</tr>
</table>
<p class="next">
下一个例子: <a href="structs">结构体</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 \"unicode/utf8\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' const s \u003D \"สวัสดี\"\u000A');codeLines.push(' fmt.Println(\"Len:\", len(s))\u000A');codeLines.push(' for i :\u003D 0; i \u003C len(s); i++ {\u000A fmt.Printf(\"%x \", s[i])\u000A }\u000A fmt.Println()\u000A');codeLines.push(' fmt.Println(\"Rune count:\", utf8.RuneCountInString(s))\u000A');codeLines.push(' for idx, runeValue :\u003D range s {\u000A fmt.Printf(\"%#U starts at %d\\n\", runeValue, idx)\u000A }\u000A');codeLines.push(' fmt.Println(\"\\nUsing DecodeRuneInString\")\u000A for i, w :\u003D 0, 0; i \u003C len(s); i +\u003D w {\u000A runeValue, width :\u003D utf8.DecodeRuneInString(s[i:])\u000A fmt.Printf(\"%#U starts at %d\\n\", runeValue, i)\u000A w \u003D width\u000A');codeLines.push(' examineRune(runeValue)\u000A }\u000A}\u000A');codeLines.push('func examineRune(r rune) {\u000A');codeLines.push(' if r \u003D\u003D \'t\' {\u000A fmt.Println(\"found tee\")\u000A } else if r \u003D\u003D \'ส\' {\u000A fmt.Println(\"found so sua\")\u000A }\u000A}\u000A');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>
</html>