精校: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
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。

View File

@@ -6,26 +6,32 @@
`HasPrefix` 判断字符串 `s` 是否以 `prefix` 开头:
strings.HasPrefix(s, prefix string) bool
```go
strings.HasPrefix(s, prefix string) bool
```
`HasSuffix` 判断字符串 `s` 是否以 `suffix` 结尾:
strings.HasSuffix(s, suffix string) bool
```go
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)
package main
```go
package main
import (
"fmt"
"strings"
)
import (
"fmt"
"strings"
)
func main() {
var str string = "This is an example of a string"
fmt.Printf("T/F? Does the string \"%s\" have prefix %s? ", str, "Th")
fmt.Printf("%t\n", strings.HasPrefix(str, "Th"))
}
func main() {
var str string = "This is an example of a string"
fmt.Printf("T/F? Does the string \"%s\" have prefix %s? ", str, "Th")
fmt.Printf("%t\n", strings.HasPrefix(str, "Th"))
}
```
输出:
@@ -43,39 +49,47 @@ Example 4.13 [presuffix.go](examples/chapter_4/presuffix.go)
`Index` 返回字符串 `str` 在字符串 `s` 中的索引(`str` 的第一个字符的索引),-1 表示字符串 `s` 不包含字符串 `str`
strings.Index(s, str string) int
```go
strings.Index(s, str string) int
```
`LastIndex` 返回字符串 `str` 在字符串 `s` 中最后出现位置的索引(`str` 的第一个字符的索引),-1 表示字符串 `s` 不包含字符串 `str`
strings.LastIndex(s, str string) int
```go
strings.LastIndex(s, str string) int
```
如果 `ch` 是非 ASCII 编码的字符,建议使用以下函数来对字符进行定位:
strings.IndexRune(s string, ch int) int
```go
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)
package main
import (
"fmt"
"strings"
)
func main() {
var str string = "Hi, I'm Marc, Hi."
fmt.Printf("The position of \"Marc\" is: ")
fmt.Printf("%d\n", strings.Index(str, "Marc"))
fmt.Printf("The position of the first instance of \"Hi\" is: ")
fmt.Printf("%d\n", strings.Index(str, "Hi"))
fmt.Printf("The position of the last instance of \"Hi\" is: ")
fmt.Printf("%d\n", strings.LastIndex(str, "Hi"))
fmt.Printf("The position of \"Burger\" is: ")
fmt.Printf("%d\n", strings.Index(str, "Burger"))
}
```go
package main
import (
"fmt"
"strings"
)
func main() {
var str string = "Hi, I'm Marc, Hi."
fmt.Printf("The position of \"Marc\" is: ")
fmt.Printf("%d\n", strings.Index(str, "Marc"))
fmt.Printf("The position of the first instance of \"Hi\" is: ")
fmt.Printf("%d\n", strings.Index(str, "Hi"))
fmt.Printf("The position of the last instance of \"Hi\" is: ")
fmt.Printf("%d\n", strings.LastIndex(str, "Hi"))
fmt.Printf("The position of \"Burger\" is: ")
fmt.Printf("%d\n", strings.Index(str, "Burger"))
}
```
输出:
@@ -88,33 +102,39 @@ Example 4.14 [index_in_string.go](examples/chapter_4/index_in_string.go)
`Replace` 用于将字符串 `str` 中的前 `n` 个字符串 `old` 替换为字符串 `new`,并返回一个新的字符串,如果 `n = -1` 则替换所有字符串 `old` 为字符串 `new`
strings.Replace(str, old, new, n) string
```go
strings.Replace(str, old, new, n) string
```
## 4.7.5 统计字符串出现次数
`Count` 用于计算字符串 `str` 在字符串 `s` 中出现的非重叠次数:
strings.Count(s, str string) int
```go
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)
package main
import (
"fmt"
"strings"
)
func main() {
var str string = "Hello, how is it going, Hugo?"
var manyG = "gggggggggg"
fmt.Printf("Number of H's in %s is: ", str)
fmt.Printf("%d\n", strings.Count(str, "H"))
fmt.Printf("Number of double g's in %s is: ", manyG)
fmt.Printf("%d\n", strings.Count(manyG, "gg"))
}
```go
package main
import (
"fmt"
"strings"
)
func main() {
var str string = "Hello, how is it going, Hugo?"
var manyG = "gggggggggg"
fmt.Printf("Number of H's in %s is: ", str)
fmt.Printf("%d\n", strings.Count(str, "H"))
fmt.Printf("Number of double g's in %s is: ", manyG)
fmt.Printf("%d\n", strings.Count(manyG, "gg"))
}
```
输出:
@@ -125,24 +145,28 @@ Example 4.15 [count_substring.go](examples/chapter_4/count_substring.go)
`Repeat` 用于重复 `count` 次字符串 `s` 并返回一个新的字符串:
strings.Repeat(s, count int) string
```go
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)
package main
import (
"fmt"
"strings"
)
func main() {
var origS string = "Hi there! "
var newS string
newS = strings.Repeat(origS, 3)
fmt.Printf("The new repeated string is: %s\n", newS)
}
```go
package main
import (
"fmt"
"strings"
)
func main() {
var origS string = "Hi there! "
var newS string
newS = strings.Repeat(origS, 3)
fmt.Printf("The new repeated string is: %s\n", newS)
}
```
输出:
@@ -152,32 +176,38 @@ Example 4.16 [repeat_string.go](examples/chapter_4/repeat_string.go)
`ToLower` 将字符串中的 Unicode 字符全部转换为相应的小写字符:
strings.ToLower(s) string
```go
strings.ToLower(s) string
```
`ToUpper` 将字符串中的 Unicode 字符全部转换为相应的大写字符:
strings.ToUpper(s) string
```go
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)
package main
import (
"fmt"
"strings"
)
func main() {
var orig string = "Hey, how are you George?"
var lower string
var upper string
fmt.Printf("The original string is: %s\n", orig)
lower = strings.ToLower(orig)
fmt.Printf("The lowercase string is: %s\n", lower)
upper = strings.ToUpper(orig)
fmt.Printf("The uppercase string is: %s\n", upper)
}
```go
package main
import (
"fmt"
"strings"
)
func main() {
var orig string = "Hey, how are you George?"
var lower string
var upper string
fmt.Printf("The original string is: %s\n", orig)
lower = strings.ToLower(orig)
fmt.Printf("The lowercase string is: %s\n", lower)
upper = strings.ToUpper(orig)
fmt.Printf("The uppercase string is: %s\n", upper)
}
```
输出:
@@ -201,35 +231,39 @@ Example 4.17 [toupper_lower.go](examples/chapter_4/toupper_lower.go)
`Join` 用于将元素类型为 string 的 slice 使用分割符号来拼接组成一个字符串:
Strings.Join(sl []string, sep string)
```go
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)
package main
import (
"fmt"
"strings"
)
func main() {
str := "The quick brown fox jumps over the lazy dog"
sl := strings.Fields(str)
fmt.Printf("Splitted in slice: %v\n", sl)
for _, val := range sl {
fmt.Printf("%s - ", val)
}
fmt.Println()
str2 := "GO1|The ABC of Go|25"
sl2 := strings.Split(str2, "|")
fmt.Printf("Splitted in slice: %v\n", sl2)
for _, val := range sl2 {
fmt.Printf("%s - ", val)
}
fmt.Println()
str3 := strings.Join(sl2,";")
fmt.Printf("sl2 joined by ;: %s\n", str3)
```go
package main
import (
"fmt"
"strings"
)
func main() {
str := "The quick brown fox jumps over the lazy dog"
sl := strings.Fields(str)
fmt.Printf("Splitted in slice: %v\n", sl)
for _, val := range sl {
fmt.Printf("%s - ", val)
}
fmt.Println()
str2 := "GO1|The ABC of Go|25"
sl2 := strings.Split(str2, "|")
fmt.Printf("Splitted in slice: %v\n", sl2)
for _, val := range sl2 {
fmt.Printf("%s - ", val)
}
fmt.Println()
str3 := strings.Join(sl2,";")
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 -
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 从字符串中读取内容
@@ -274,28 +308,30 @@ 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)
package main
import (
"fmt"
"strconv"
)
func main() {
var orig string = "666"
var an int
var newS string
fmt.Printf("The size of ints is: %d\n", strconv.IntSize)
an, _ = strconv.Atoi(orig)
fmt.Printf("The integer is: %d\n", an)
an = an + 5
newS = strconv.Itoa(an)
fmt.Printf("The new string is: %s\n", newS)
}
```go
package main
import (
"fmt"
"strconv"
)
func main() {
var orig string = "666"
var an int
var newS string
fmt.Printf("The size of ints is: %d\n", strconv.IntSize)
an, _ = strconv.Atoi(orig)
fmt.Printf("The integer is: %d\n", an)
an = an + 5
newS = strconv.Itoa(an)
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 语句来对可能出现的错误进行分类处理。
更多有关该包的讨论,请参阅官方文档 [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/)** )。
## 链接