Tweaks to other samples to accommodate strings-and-runes
Add a link to the range sample, and remove partial mentions of this topic in string-functions since it's now covered well in strings-and-runes
This commit is contained in:
parent
b2057ccfd2
commit
e604254e38
@ -42,6 +42,8 @@ func main() {
|
||||
// `range` on strings iterates over Unicode code
|
||||
// points. The first value is the starting byte index
|
||||
// of the `rune` and the second the `rune` itself.
|
||||
// See [Strings and Runes](strings-and-runes) for more
|
||||
// details.
|
||||
for i, c := range "go" {
|
||||
fmt.Println(i, c)
|
||||
}
|
||||
|
@ -1,2 +1,2 @@
|
||||
c7d9ae9ed081fb4bbf27ef45242fbb39bbae3d4c
|
||||
pdZOtv4g-7J
|
||||
c8da490660d234fc420f39d9b8a4aba27f8aba46
|
||||
kRsyWNmLFLz
|
||||
|
@ -35,17 +35,4 @@ func main() {
|
||||
p("ToLower: ", s.ToLower("TEST"))
|
||||
p("ToUpper: ", s.ToUpper("test"))
|
||||
p()
|
||||
|
||||
// Not part of `strings`, but worth mentioning here, are
|
||||
// the mechanisms for getting the length of a string in
|
||||
// bytes and getting a byte by index.
|
||||
p("Len: ", len("hello"))
|
||||
p("Char:", "hello"[1])
|
||||
}
|
||||
|
||||
// Note that `len` and indexing above work at the byte level.
|
||||
// Go uses UTF-8 encoded strings, so this is often useful
|
||||
// as-is. If you're working with potentially multi-byte
|
||||
// characters you'll want to use encoding-aware operations.
|
||||
// See [strings, bytes, runes and characters in Go](https://blog.golang.org/strings)
|
||||
// for more information.
|
||||
|
@ -1,2 +1,2 @@
|
||||
33b15b8c999ba65564b965b96cbfeadac0d1637d
|
||||
fZ_FqN5WlSz
|
||||
2201eba7e3b9339a6e700a4faa9b085c43dd1ed2
|
||||
pE_KNt12Zv9
|
||||
|
@ -11,6 +11,3 @@ Replace: f0o
|
||||
Split: [a b c d e]
|
||||
ToLower: test
|
||||
ToUpper: TEST
|
||||
|
||||
Len: 5
|
||||
Char: 101
|
||||
|
6
public/range
generated
6
public/range
generated
@ -43,7 +43,7 @@ of the data structures we’ve already learned.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
<a href="http://play.golang.org/p/pdZOtv4g-7J"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<a href="http://play.golang.org/p/kRsyWNmLFLz"><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>
|
||||
@ -146,7 +146,9 @@ the indexes though.</p>
|
||||
<td class="docs">
|
||||
<p><code>range</code> on strings iterates over Unicode code
|
||||
points. The first value is the starting byte index
|
||||
of the <code>rune</code> and the second the <code>rune</code> itself.</p>
|
||||
of the <code>rune</code> and the second the <code>rune</code> itself.
|
||||
See <a href="strings-and-runes">Strings and Runes</a> for more
|
||||
details.</p>
|
||||
|
||||
</td>
|
||||
<td class="code">
|
||||
|
51
public/string-functions
generated
51
public/string-functions
generated
@ -43,7 +43,7 @@ to give you a sense of the package.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
<a href="http://play.golang.org/p/fZ_FqN5WlSz"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<a href="http://play.golang.org/p/pE_KNt12Zv9"><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>
|
||||
@ -99,7 +99,7 @@ functions in the <a href="http://golang.org/pkg/strings/"><code>strings</code></
|
||||
package docs.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
<td class="code">
|
||||
|
||||
<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>
|
||||
@ -115,43 +115,11 @@ package docs.</p>
|
||||
<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>Not part of <code>strings</code>, but worth mentioning here, are
|
||||
the mechanisms for getting the length of a string in
|
||||
bytes and getting a byte by index.</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>Note that <code>len</code> and indexing above work at the byte level.
|
||||
Go uses UTF-8 encoded strings, so this is often useful
|
||||
as-is. If you’re working with potentially multi-byte
|
||||
characters you’ll want to use encoding-aware operations.
|
||||
See <a href="https://blog.golang.org/strings">strings, bytes, runes and characters in Go</a>
|
||||
for more information.</p>
|
||||
|
||||
</td>
|
||||
<td class="code empty">
|
||||
|
||||
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
</table>
|
||||
|
||||
<table>
|
||||
@ -160,7 +128,7 @@ for more information.</p>
|
||||
<td class="docs">
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
<td class="code">
|
||||
|
||||
<pre class="chroma"><span class="gp">$</span> go run string-functions.go
|
||||
<span class="go">Contains: true
|
||||
@ -178,17 +146,6 @@ for more information.</p>
|
||||
</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>
|
||||
|
||||
|
||||
@ -204,7 +161,7 @@ for more information.</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('');
|
||||
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}\u000A');codeLines.push('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
Loading…
x
Reference in New Issue
Block a user