-
Notifications
You must be signed in to change notification settings - Fork 0
/
String.html
344 lines (344 loc) · 21.9 KB
/
String.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="generator" content="Asciidoctor 2.0.12">
<meta name="author" content="v0.1">
<title>String class</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:300,300italic,400,400italic,600,600italic%7CNoto+Serif:400,400italic,700,700italic%7CDroid+Sans+Mono:400,700">
<link rel="stylesheet" href="./asciidoctor.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="./rouge-old_github.css">
<meta name="keywords" content="Agate, programming language, scripting, gamedev" />
<meta name="description" content="Agate, a simple and embeddable programming language" />
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/site.webmanifest">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">
<meta name="msapplication-TileColor" content="#da532c">
<meta name="theme-color" content="#ffffff">
</head>
<body class="article toc2 toc-right">
<div id="header">
<h1><code>String</code> class</h1>
<div class="details">
<span id="author" class="author">v0.1</span><br>
</div>
<div id="toc" class="toc2">
<div id="toctitle">Table of Contents</div>
<ul class="sectlevel1">
<li><a href="#_methods">Methods</a>
<ul class="sectlevel2">
<li><a href="#_clone"><strong>clone</strong>()</a></li>
<li><a href="#_cmp"><strong>cmp</strong>(other)</a></li>
<li><a href="#_contains"><strong>contains</strong>(other)</a></li>
<li><a href="#_ends_with"><strong>ends_with</strong>(other)</a></li>
<li><a href="#_find"><strong>find</strong>(needle)</a></li>
<li><a href="#_find_from"><strong>find</strong>(needle, start)</a></li>
<li><a href="#_hash"><strong>hash</strong></a></li>
<li><a href="#_iterate"><strong>iterate</strong>(iterator), <strong>iterator_value</strong>(iterator)</a></li>
<li><a href="#_ltrim"><strong>ltrim</strong>(), <strong>ltrim</strong>(chars)</a></li>
<li><a href="#_replace"><strong>replace</strong>(old,new)</a></li>
<li><a href="#_rtrim"><strong>rtrim</strong>(), <strong>rtrim</strong>(chars)</a></li>
<li><a href="#_split"><strong>split</strong>(sep)</a></li>
<li><a href="#_starts_with"><strong>starts_with</strong>(other)</a></li>
<li><a href="#_to_f"><strong>to_f</strong></a></li>
<li><a href="#_to_i"><strong>to_i</strong></a></li>
<li><a href="#_to_s"><strong>to_s</strong></a></li>
<li><a href="#_trim"><strong>trim</strong>(), <strong>trim</strong>(chars)</a></li>
</ul>
</li>
<li><a href="#_operators">Operators</a>
<ul class="sectlevel2">
<li><a href="#_plus"><strong>+</strong>(other)</a></li>
<li><a href="#_times"><strong>*</strong>(count)</a></li>
</ul>
</li>
</ul>
</div>
</div>
<div id="content">
<div id="preamble">
<div class="sectionbody">
<div class="paragraph">
<p>A string is a sequence of characters. It is represented internally by an array of <a href="https://en.wikipedia.org/wiki/UTF-8">UTF-8</a> bytes.</p>
</div>
<div class="paragraph">
<p><a href="core.html">< Back to core unit</a></p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_methods"><a class="anchor" href="#_methods"></a>Methods</h2>
<div class="sectionbody">
<div class="sect2">
<h3 id="_clone"><a class="anchor" href="#_clone"></a><strong>clone</strong>()</h3>
<div class="paragraph">
<p>Get a copy of the string.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="s">"the cake is a lie"</span><span class="p">.</span><span class="n">clone</span><span class="p">())</span><span class="w"> </span><span class="c1">#> the cake is a lie</span></code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_cmp"><a class="anchor" href="#_cmp"></a><strong>cmp</strong>(other)</h3>
<div class="paragraph">
<p>Compare the string to another string. Return an <a href="Int.html"><code>Int</code></a> indicating if the string is less than <code>other</code> with a negative value, or if the string is greater than <code>other</code> with a positive value, or if the string is equal to <code>other</code> with 0.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="s">"the cake"</span><span class="p">.</span><span class="n">cmp</span><span class="p">(</span><span class="s">"a lie"</span><span class="p">)</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="mi">0</span><span class="p">)</span><span class="w"> </span><span class="c1">#> true</span><span class="err">
</span><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="s">"the cake"</span><span class="p">.</span><span class="n">cmp</span><span class="p">(</span><span class="s">"the cake"</span><span class="p">)</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">0</span><span class="p">)</span><span class="w"> </span><span class="c1">#> true</span></code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_contains"><a class="anchor" href="#_contains"></a><strong>contains</strong>(other)</h3>
<div class="paragraph">
<p>Return <code>true</code> if <code>other</code> is a part of the string. <code>other</code> must be a character or a string.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="s">"the cake is a lie"</span><span class="p">.</span><span class="n">contains</span><span class="p">(</span><span class="s">"cake"</span><span class="p">))</span><span class="w"> </span><span class="c1">#> true</span><span class="err">
</span><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="s">"the cake is a lie"</span><span class="p">.</span><span class="n">contains</span><span class="p">(</span><span class="sc">'x'</span><span class="p">))</span><span class="w"> </span><span class="c1">#> false</span></code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_ends_with"><a class="anchor" href="#_ends_with"></a><strong>ends_with</strong>(other)</h3>
<div class="paragraph">
<p>Return <code>true</code> is the string ends with <code>other</code>. <code>other</code> must be a string.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="s">"the cake is a lie"</span><span class="p">.</span><span class="n">ends_with</span><span class="p">(</span><span class="s">"cake"</span><span class="p">))</span><span class="w"> </span><span class="c1">#> false</span><span class="err">
</span><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="s">"東京"</span><span class="p">.</span><span class="n">ends_with</span><span class="p">(</span><span class="s">"京"</span><span class="p">))</span><span class="w"> </span><span class="c1">#> true</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>See also: <a href="#_starts_with"><strong>starts_with</strong>(other)</a></p>
</div>
</div>
<div class="sect2">
<h3 id="_find"><a class="anchor" href="#_find"></a><strong>find</strong>(needle)</h3>
<div class="paragraph">
<p>Find <code>needle</code> in the string and return an iterator to the beginning of the first occurrence of <code>needle</code>, or <code>-1</code> if <code>needle</code> was not found.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="s">"the cake is a lie"</span><span class="p">.</span><span class="n">find</span><span class="p">(</span><span class="s">"cake"</span><span class="p">))</span><span class="w"> </span><span class="c1">#> 4</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>See also: <a href="#_find_from"><strong>find</strong>(needle, start)</a></p>
</div>
</div>
<div class="sect2">
<h3 id="_find_from"><a class="anchor" href="#_find_from"></a><strong>find</strong>(needle, start)</h3>
<div class="paragraph">
<p>Find <code>needle</code> in the string, starting at iterator <code>start</code>, and return an iterator to the beginning of the first occurrence of <code>needle</code>, or <code>-1</code> if <code>needle</code> was not found.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="s">"the cake is a lie"</span><span class="p">.</span><span class="n">find</span><span class="p">(</span><span class="s">"cake"</span><span class="p">,</span><span class="w"> </span><span class="mi">10</span><span class="p">))</span><span class="w"> </span><span class="c1">#> -1</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>See also: <a href="#_find"><strong>find</strong>(needle)</a></p>
</div>
</div>
<div class="sect2">
<h3 id="_hash"><a class="anchor" href="#_hash"></a><strong>hash</strong></h3>
<div class="paragraph">
<p>Compute a hash for the string.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="s">"the cake is a lie"</span><span class="p">.</span><span class="n">hash</span><span class="p">)</span><span class="w"> </span><span class="c1">#> -5017247130378745027</span></code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_iterate"><a class="anchor" href="#_iterate"></a><strong>iterate</strong>(iterator), <strong>iterator_value</strong>(iterator)</h3>
<div class="paragraph">
<p>Implement the <a href="lang.html#_iterator_protocol">iterator protocol</a> to iterate over the string.</p>
</div>
</div>
<div class="sect2">
<h3 id="_ltrim"><a class="anchor" href="#_ltrim"></a><strong>ltrim</strong>(), <strong>ltrim</strong>(chars)</h3>
<div class="paragraph">
<p>Remove some characters from the beginning of the string, and return a new string. <code>chars</code> contain the characters that should be removed. By default, it consists in whitespace characters: <code>" \n\t\r"</code>.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="s">" </span><span class="se">\n\r</span><span class="s">the cake is a lie"</span><span class="p">.</span><span class="n">ltrim</span><span class="p">())</span><span class="w"> </span><span class="c1">#> the cake is a lie</span><span class="err">
</span><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="s">"the cake is a lie"</span><span class="p">.</span><span class="n">ltrim</span><span class="p">(</span><span class="s">" eht"</span><span class="p">))</span><span class="w"> </span><span class="c1">#> cake is a lie</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>See also: <a href="#_rtrim"><strong>rtrim</strong>(), <strong>rtrim</strong>(chars)</a>, <a href="#_trim"><strong>trim</strong>(), <strong>trim</strong>(chars)</a></p>
</div>
</div>
<div class="sect2">
<h3 id="_replace"><a class="anchor" href="#_replace"></a><strong>replace</strong>(old,new)</h3>
<div class="paragraph">
<p>Replace all the occurrences of <code>old</code> in the string with <code>new</code> and return a new string.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="s">"the cake is a lie"</span><span class="p">.</span><span class="n">replace</span><span class="p">(</span><span class="s">"cake"</span><span class="p">,</span><span class="w"> </span><span class="s">"pie"</span><span class="p">).</span><span class="n">replace</span><span class="p">(</span><span class="s">"lie"</span><span class="p">,</span><span class="w"> </span><span class="s">"fake"</span><span class="p">))</span><span class="err">
</span><span class="c1">#> the pie is a fake</span></code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_rtrim"><a class="anchor" href="#_rtrim"></a><strong>rtrim</strong>(), <strong>rtrim</strong>(chars)</h3>
<div class="paragraph">
<p>Remove some characters from the end of the string, and return a new string. <code>chars</code> contain the characters that should be removed. By default, it consists in whitespace characters: <code>" \n\t\r"</code>.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="s">"the cake is a lie </span><span class="se">\n\r</span><span class="s">"</span><span class="p">.</span><span class="n">rtrim</span><span class="p">())</span><span class="w"> </span><span class="c1">#> the cake is a lie</span><span class="err">
</span><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="s">"the cake is a lie"</span><span class="p">.</span><span class="n">rtrim</span><span class="p">(</span><span class="s">" eht"</span><span class="p">))</span><span class="w"> </span><span class="c1">#> the cake is a li</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>See also: <a href="#_ltrim"><strong>ltrim</strong>(), <strong>ltrim</strong>(chars)</a>, <a href="#_trim"><strong>trim</strong>(), <strong>trim</strong>(chars)</a></p>
</div>
</div>
<div class="sect2">
<h3 id="_split"><a class="anchor" href="#_split"></a><strong>split</strong>(sep)</h3>
<div class="paragraph">
<p>Split the string thanks to the separator <code>sep</code> and return an <a href="Array.html"><code>Array</code></a> of strings. <code>sep</code> must be a character or a string.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="s">"the cake is a lie"</span><span class="p">.</span><span class="n">split</span><span class="p">(</span><span class="sc">' '</span><span class="p">))</span><span class="w"> </span><span class="c1">#> [the, cake, is, a, lie]</span></code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_starts_with"><a class="anchor" href="#_starts_with"></a><strong>starts_with</strong>(other)</h3>
<div class="paragraph">
<p>Return <code>true</code> is the string starts with <code>other</code>. <code>other</code> must be a string.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="s">"the cake is a lie"</span><span class="p">.</span><span class="n">starts_with</span><span class="p">(</span><span class="s">"cake"</span><span class="p">))</span><span class="w"> </span><span class="c1">#> false</span><span class="err">
</span><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="s">"東京"</span><span class="p">.</span><span class="n">starts_with</span><span class="p">(</span><span class="s">"東"</span><span class="p">))</span><span class="w"> </span><span class="c1">#> true</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>See also: <a href="#_ends_with"><strong>ends_with</strong>(other)</a></p>
</div>
</div>
<div class="sect2">
<h3 id="_to_f"><a class="anchor" href="#_to_f"></a><strong>to_f</strong></h3>
<div class="paragraph">
<p>Convert the string to a <a href="Float.html"><code>Float</code></a>.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="s">"42.0"</span><span class="p">.</span><span class="n">to_f</span><span class="p">)</span><span class="w"> </span><span class="c1">#> 42</span></code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_to_i"><a class="anchor" href="#_to_i"></a><strong>to_i</strong></h3>
<div class="paragraph">
<p>Convert the string to an <a href="Int.html"><code>Int</code></a>.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="s">"42"</span><span class="p">.</span><span class="n">to_i</span><span class="p">)</span><span class="w"> </span><span class="c1">#> 42</span></code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_to_s"><a class="anchor" href="#_to_s"></a><strong>to_s</strong></h3>
<div class="paragraph">
<p>Return the string.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="s">"the cake is a lie"</span><span class="p">.</span><span class="n">to_s</span><span class="p">)</span><span class="w"> </span><span class="c1">#> the cake is a lie</span></code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_trim"><a class="anchor" href="#_trim"></a><strong>trim</strong>(), <strong>trim</strong>(chars)</h3>
<div class="paragraph">
<p>Remove some characters from the beginning and the end of the string, and return a new string. <code>chars</code> contain the characters that should be removed. By default, it consists in whitespace characters: <code>" \n\t\r"</code>.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="s">" </span><span class="se">\n\r</span><span class="s">the cake is a lie </span><span class="se">\n\r</span><span class="s">"</span><span class="p">.</span><span class="n">trim</span><span class="p">())</span><span class="w"> </span><span class="c1">#> the cake is a lie</span><span class="err">
</span><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="s">"the cake is a lie"</span><span class="p">.</span><span class="n">trim</span><span class="p">(</span><span class="s">" eht"</span><span class="p">))</span><span class="w"> </span><span class="c1">#> cake is a li</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>See also: <a href="#_ltrim"><strong>ltrim</strong>(), <strong>ltrim</strong>(chars)</a>, <a href="#_rtrim"><strong>rtrim</strong>(), <strong>rtrim</strong>(chars)</a></p>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_operators"><a class="anchor" href="#_operators"></a>Operators</h2>
<div class="sectionbody">
<div class="sect2">
<h3 id="_plus"><a class="anchor" href="#_plus"></a><strong>+</strong>(other)</h3>
<div class="paragraph">
<p>Concatenate the string with <code>other</code> and return the result of the concatenation.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="s">"the cake is "</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s">"a lie"</span><span class="p">)</span><span class="w"> </span><span class="c1">#> the cake is a lie</span></code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_times"><a class="anchor" href="#_times"></a><strong>*</strong>(count)</h3>
<div class="paragraph">
<p>Concatenate the string <code>count</code> times and return the result of the concatenation.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="s">" the cake is a lie"</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="mi">3</span><span class="p">)</span><span class="err">
</span><span class="c1">#> the cake is a lie the cake is a lie the cake is a lie</span></code></pre>
</div>
</div>
</div>
</div>
</div>
</div>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
messageStyle: "none",
tex2jax: {
inlineMath: [["\\(", "\\)"]],
displayMath: [["\\[", "\\]"]],
ignoreClass: "nostem|nolatexmath"
},
asciimath2jax: {
delimiters: [["\\$", "\\$"]],
ignoreClass: "nostem|noasciimath"
},
TeX: { equationNumbers: { autoNumber: "none" } }
})
MathJax.Hub.Register.StartupHook("AsciiMath Jax Ready", function () {
MathJax.InputJax.AsciiMath.postfilterHooks.Add(function (data, node) {
if ((node = data.script.parentNode) && (node = node.parentNode) && node.classList.contains("stemblock")) {
data.math.root.display = "block"
}
return data
})
})
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.9/MathJax.js?config=TeX-MML-AM_HTMLorMML"></script>
</body>
</html>