精校:4.7

This commit is contained in:
Unknwon
2015-07-10 11:55:00 +08:00
parent 29ad231d45
commit 0227814eff
2 changed files with 181 additions and 145 deletions

View File

@@ -91,7 +91,7 @@ F || F -> false
### 4.5.2.1 整型 int 和浮点型 float ### 4.5.2.1 整型 int 和浮点型 float
Go 语言支持整型和浮点型数字,并且原生支持复数,其中位的运算采用补码(二的补码,详情参见[http://en.wikipedia.org/wiki/Two's_complement](http://en.wikipedia.org/wiki/Two's_complement))。 Go 语言支持整型和浮点型数字,并且原生支持复数,其中位的运算采用补码(详情参见 [二的补码](http://en.wikipedia.org/wiki/Two's_complement) 页面)。
Go 也有基于架构的类型例如int、uint 和 uintptr。 Go 也有基于架构的类型例如int、uint 和 uintptr。

View File

@@ -6,14 +6,19 @@
`HasPrefix` 判断字符串 `s` 是否以 `prefix` 开头: `HasPrefix` 判断字符串 `s` 是否以 `prefix` 开头:
```go
strings.HasPrefix(s, prefix string) bool strings.HasPrefix(s, prefix string) bool
```
`HasSuffix` 判断字符串 `s` 是否以 `suffix` 结尾: `HasSuffix` 判断字符串 `s` 是否以 `suffix` 结尾:
```go
strings.HasSuffix(s, suffix string) bool strings.HasSuffix(s, suffix string) bool
```
Example 4.13 [presuffix.go](examples/chapter_4/presuffix.go) 示例 4.13 [presuffix.go](examples/chapter_4/presuffix.go)
```go
package main package main
import ( import (
@@ -26,6 +31,7 @@ Example 4.13 [presuffix.go](examples/chapter_4/presuffix.go)
fmt.Printf("T/F? Does the string \"%s\" have prefix %s? ", str, "Th") fmt.Printf("T/F? Does the string \"%s\" have prefix %s? ", str, "Th")
fmt.Printf("%t\n", strings.HasPrefix(str, "Th")) fmt.Printf("%t\n", strings.HasPrefix(str, "Th"))
} }
```
输出: 输出:
@@ -43,18 +49,25 @@ Example 4.13 [presuffix.go](examples/chapter_4/presuffix.go)
`Index` 返回字符串 `str` 在字符串 `s` 中的索引(`str` 的第一个字符的索引),-1 表示字符串 `s` 不包含字符串 `str` `Index` 返回字符串 `str` 在字符串 `s` 中的索引(`str` 的第一个字符的索引),-1 表示字符串 `s` 不包含字符串 `str`
```go
strings.Index(s, str string) int strings.Index(s, str string) int
```
`LastIndex` 返回字符串 `str` 在字符串 `s` 中最后出现位置的索引(`str` 的第一个字符的索引),-1 表示字符串 `s` 不包含字符串 `str` `LastIndex` 返回字符串 `str` 在字符串 `s` 中最后出现位置的索引(`str` 的第一个字符的索引),-1 表示字符串 `s` 不包含字符串 `str`
```go
strings.LastIndex(s, str string) int strings.LastIndex(s, str string) int
```
如果 `ch` 是非 ASCII 编码的字符,建议使用以下函数来对字符进行定位: 如果 `ch` 是非 ASCII 编码的字符,建议使用以下函数来对字符进行定位:
```go
strings.IndexRune(s string, ch int) int strings.IndexRune(s string, ch int) int
```
Example 4.14 [index_in_string.go](examples/chapter_4/index_in_string.go) 示例 4.14 [index_in_string.go](examples/chapter_4/index_in_string.go)
```go
package main package main
import ( import (
@@ -76,6 +89,7 @@ Example 4.14 [index_in_string.go](examples/chapter_4/index_in_string.go)
fmt.Printf("The position of \"Burger\" is: ") fmt.Printf("The position of \"Burger\" is: ")
fmt.Printf("%d\n", strings.Index(str, "Burger")) fmt.Printf("%d\n", strings.Index(str, "Burger"))
} }
```
输出: 输出:
@@ -88,16 +102,21 @@ Example 4.14 [index_in_string.go](examples/chapter_4/index_in_string.go)
`Replace` 用于将字符串 `str` 中的前 `n` 个字符串 `old` 替换为字符串 `new`,并返回一个新的字符串,如果 `n = -1` 则替换所有字符串 `old` 为字符串 `new` `Replace` 用于将字符串 `str` 中的前 `n` 个字符串 `old` 替换为字符串 `new`,并返回一个新的字符串,如果 `n = -1` 则替换所有字符串 `old` 为字符串 `new`
```go
strings.Replace(str, old, new, n) string strings.Replace(str, old, new, n) string
```
## 4.7.5 统计字符串出现次数 ## 4.7.5 统计字符串出现次数
`Count` 用于计算字符串 `str` 在字符串 `s` 中出现的非重叠次数: `Count` 用于计算字符串 `str` 在字符串 `s` 中出现的非重叠次数:
```go
strings.Count(s, str string) int strings.Count(s, str string) int
```
Example 4.15 [count_substring.go](examples/chapter_4/count_substring.go) 示例 4.15 [count_substring.go](examples/chapter_4/count_substring.go)
```go
package main package main
import ( import (
@@ -115,6 +134,7 @@ Example 4.15 [count_substring.go](examples/chapter_4/count_substring.go)
fmt.Printf("Number of double g's in %s is: ", manyG) fmt.Printf("Number of double g's in %s is: ", manyG)
fmt.Printf("%d\n", strings.Count(manyG, "gg")) fmt.Printf("%d\n", strings.Count(manyG, "gg"))
} }
```
输出: 输出:
@@ -125,10 +145,13 @@ Example 4.15 [count_substring.go](examples/chapter_4/count_substring.go)
`Repeat` 用于重复 `count` 次字符串 `s` 并返回一个新的字符串: `Repeat` 用于重复 `count` 次字符串 `s` 并返回一个新的字符串:
```go
strings.Repeat(s, count int) string strings.Repeat(s, count int) string
```
Example 4.16 [repeat_string.go](examples/chapter_4/repeat_string.go) 示例 4.16 [repeat_string.go](examples/chapter_4/repeat_string.go)
```go
package main package main
import ( import (
@@ -143,6 +166,7 @@ Example 4.16 [repeat_string.go](examples/chapter_4/repeat_string.go)
newS = strings.Repeat(origS, 3) newS = strings.Repeat(origS, 3)
fmt.Printf("The new repeated string is: %s\n", newS) fmt.Printf("The new repeated string is: %s\n", newS)
} }
```
输出: 输出:
@@ -152,14 +176,19 @@ Example 4.16 [repeat_string.go](examples/chapter_4/repeat_string.go)
`ToLower` 将字符串中的 Unicode 字符全部转换为相应的小写字符: `ToLower` 将字符串中的 Unicode 字符全部转换为相应的小写字符:
```go
strings.ToLower(s) string strings.ToLower(s) string
```
`ToUpper` 将字符串中的 Unicode 字符全部转换为相应的大写字符: `ToUpper` 将字符串中的 Unicode 字符全部转换为相应的大写字符:
```go
strings.ToUpper(s) string strings.ToUpper(s) string
```
Example 4.17 [toupper_lower.go](examples/chapter_4/toupper_lower.go) 示例 4.17 [toupper_lower.go](examples/chapter_4/toupper_lower.go)
```go
package main package main
import ( import (
@@ -178,6 +207,7 @@ Example 4.17 [toupper_lower.go](examples/chapter_4/toupper_lower.go)
upper = strings.ToUpper(orig) upper = strings.ToUpper(orig)
fmt.Printf("The uppercase string is: %s\n", upper) fmt.Printf("The uppercase string is: %s\n", upper)
} }
```
输出: 输出:
@@ -201,10 +231,13 @@ Example 4.17 [toupper_lower.go](examples/chapter_4/toupper_lower.go)
`Join` 用于将元素类型为 string 的 slice 使用分割符号来拼接组成一个字符串: `Join` 用于将元素类型为 string 的 slice 使用分割符号来拼接组成一个字符串:
```go
Strings.Join(sl []string, sep string) Strings.Join(sl []string, sep string)
```
Example 4.18 [strings_splitjoin.go](examples/chapter_4/strings_splitjoin.go) 示例 4.18 [strings_splitjoin.go](examples/chapter_4/strings_splitjoin.go)
```go
package main package main
import ( import (
@@ -230,6 +263,7 @@ Example 4.18 [strings_splitjoin.go](examples/chapter_4/strings_splitjoin.go)
str3 := strings.Join(sl2,";") str3 := strings.Join(sl2,";")
fmt.Printf("sl2 joined by ;: %s\n", str3) fmt.Printf("sl2 joined by ;: %s\n", str3)
} }
```
输出: 输出:
@@ -239,7 +273,7 @@ Example 4.18 [strings_splitjoin.go](examples/chapter_4/strings_splitjoin.go)
GO1 - The ABC of Go - 25 - GO1 - The ABC of Go - 25 -
sl2 joined by ;: GO1;The ABC of Go;25 sl2 joined by ;: GO1;The ABC of Go;25
其它有关字符串操作的文档请参阅官方文档 [http://golang.org/pkg/strings/](http://golang.org/pkg/strings/) ***译者注:国内用户可访问 [http://docs.studygolang.com/pkg/strings/](http://docs.studygolang.com/pkg/strings/)*** )。 其它有关字符串操作的文档请参阅官方文档 [http://golang.org/pkg/strings/](http://golang.org/pkg/strings/) **译者注:国内用户可访问 [该页面](http://docs.studygolang.com/pkg/strings/)** )。
## 4.7.11 从字符串中读取内容 ## 4.7.11 从字符串中读取内容
@@ -274,8 +308,9 @@ Example 4.18 [strings_splitjoin.go](examples/chapter_4/strings_splitjoin.go)
在下面这个示例中,我们忽略可能出现的转换错误: 在下面这个示例中,我们忽略可能出现的转换错误:
Example 4.19 [string_conversion.go](examples/chapter_4/string_conversion.go) 示例 4.19 [string_conversion.go](examples/chapter_4/string_conversion.go)
```go
package main package main
import ( import (
@@ -296,6 +331,7 @@ Example 4.19 [string_conversion.go](examples/chapter_4/string_conversion.go)
newS = strconv.Itoa(an) newS = strconv.Itoa(an)
fmt.Printf("The new string is: %s\n", newS) fmt.Printf("The new string is: %s\n", newS)
} }
```
输出: 输出:
@@ -305,7 +341,7 @@ Example 4.19 [string_conversion.go](examples/chapter_4/string_conversion.go)
在第 5.1 节,我们将会利用 if 语句来对可能出现的错误进行分类处理。 在第 5.1 节,我们将会利用 if 语句来对可能出现的错误进行分类处理。
更多有关该包的讨论,请参阅官方文档 [http://golang.org/pkg/strconv/](http://golang.org/pkg/strconv/) ***译者注:国内用户可访问 [http://docs.studygolang.com/pkg/strconv/](http://docs.studygolang.com/pkg/strconv/)*** )。 更多有关该包的讨论,请参阅官方文档 [http://golang.org/pkg/strconv/](http://golang.org/pkg/strconv/) **译者注:国内用户可访问 [该页面](http://docs.studygolang.com/pkg/strconv/)** )。
## 链接 ## 链接