mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-11 19:41:43 +08:00
精校:4.7
This commit is contained in:
@@ -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。
|
||||||
|
|
||||||
|
324
eBook/04.7.md
324
eBook/04.7.md
@@ -6,26 +6,32 @@
|
|||||||
|
|
||||||
`HasPrefix` 判断字符串 `s` 是否以 `prefix` 开头:
|
`HasPrefix` 判断字符串 `s` 是否以 `prefix` 开头:
|
||||||
|
|
||||||
strings.HasPrefix(s, prefix string) bool
|
```go
|
||||||
|
strings.HasPrefix(s, prefix string) bool
|
||||||
|
```
|
||||||
|
|
||||||
`HasSuffix` 判断字符串 `s` 是否以 `suffix` 结尾:
|
`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 (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var str string = "This is an example of a string"
|
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/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,39 +49,47 @@ Example 4.13 [presuffix.go](examples/chapter_4/presuffix.go)
|
|||||||
|
|
||||||
`Index` 返回字符串 `str` 在字符串 `s` 中的索引(`str` 的第一个字符的索引),-1 表示字符串 `s` 不包含字符串 `str`:
|
`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`:
|
`LastIndex` 返回字符串 `str` 在字符串 `s` 中最后出现位置的索引(`str` 的第一个字符的索引),-1 表示字符串 `s` 不包含字符串 `str`:
|
||||||
|
|
||||||
strings.LastIndex(s, str string) int
|
```go
|
||||||
|
strings.LastIndex(s, str string) int
|
||||||
|
```
|
||||||
|
|
||||||
如果 `ch` 是非 ASCII 编码的字符,建议使用以下函数来对字符进行定位:
|
如果 `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
|
```go
|
||||||
|
package main
|
||||||
import (
|
|
||||||
"fmt"
|
import (
|
||||||
"strings"
|
"fmt"
|
||||||
)
|
"strings"
|
||||||
|
)
|
||||||
func main() {
|
|
||||||
var str string = "Hi, I'm Marc, Hi."
|
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 \"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 first instance of \"Hi\" is: ")
|
||||||
fmt.Printf("The position of the last instance of \"Hi\" is: ")
|
fmt.Printf("%d\n", strings.Index(str, "Hi"))
|
||||||
fmt.Printf("%d\n", strings.LastIndex(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"))
|
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`:
|
`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 统计字符串出现次数
|
## 4.7.5 统计字符串出现次数
|
||||||
|
|
||||||
`Count` 用于计算字符串 `str` 在字符串 `s` 中出现的非重叠次数:
|
`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
|
```go
|
||||||
|
package main
|
||||||
import (
|
|
||||||
"fmt"
|
import (
|
||||||
"strings"
|
"fmt"
|
||||||
)
|
"strings"
|
||||||
|
)
|
||||||
func main() {
|
|
||||||
var str string = "Hello, how is it going, Hugo?"
|
func main() {
|
||||||
var manyG = "gggggggggg"
|
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 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"))
|
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` 并返回一个新的字符串:
|
`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
|
```go
|
||||||
|
package main
|
||||||
import (
|
|
||||||
"fmt"
|
import (
|
||||||
"strings"
|
"fmt"
|
||||||
)
|
"strings"
|
||||||
|
)
|
||||||
func main() {
|
|
||||||
var origS string = "Hi there! "
|
func main() {
|
||||||
var newS string
|
var origS string = "Hi there! "
|
||||||
|
var newS string
|
||||||
newS = strings.Repeat(origS, 3)
|
|
||||||
fmt.Printf("The new repeated string is: %s\n", newS)
|
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 字符全部转换为相应的小写字符:
|
`ToLower` 将字符串中的 Unicode 字符全部转换为相应的小写字符:
|
||||||
|
|
||||||
strings.ToLower(s) string
|
```go
|
||||||
|
strings.ToLower(s) string
|
||||||
|
```
|
||||||
|
|
||||||
`ToUpper` 将字符串中的 Unicode 字符全部转换为相应的大写字符:
|
`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
|
```go
|
||||||
|
package main
|
||||||
import (
|
|
||||||
"fmt"
|
import (
|
||||||
"strings"
|
"fmt"
|
||||||
)
|
"strings"
|
||||||
|
)
|
||||||
func main() {
|
|
||||||
var orig string = "Hey, how are you George?"
|
func main() {
|
||||||
var lower string
|
var orig string = "Hey, how are you George?"
|
||||||
var upper string
|
var lower string
|
||||||
|
var upper string
|
||||||
fmt.Printf("The original string is: %s\n", orig)
|
|
||||||
lower = strings.ToLower(orig)
|
fmt.Printf("The original string is: %s\n", orig)
|
||||||
fmt.Printf("The lowercase string is: %s\n", lower)
|
lower = strings.ToLower(orig)
|
||||||
upper = strings.ToUpper(orig)
|
fmt.Printf("The lowercase string is: %s\n", lower)
|
||||||
fmt.Printf("The uppercase string is: %s\n", upper)
|
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 使用分割符号来拼接组成一个字符串:
|
`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
|
```go
|
||||||
|
package main
|
||||||
import (
|
|
||||||
"fmt"
|
import (
|
||||||
"strings"
|
"fmt"
|
||||||
)
|
"strings"
|
||||||
|
)
|
||||||
func main() {
|
|
||||||
str := "The quick brown fox jumps over the lazy dog"
|
func main() {
|
||||||
sl := strings.Fields(str)
|
str := "The quick brown fox jumps over the lazy dog"
|
||||||
fmt.Printf("Splitted in slice: %v\n", sl)
|
sl := strings.Fields(str)
|
||||||
for _, val := range sl {
|
fmt.Printf("Splitted in slice: %v\n", sl)
|
||||||
fmt.Printf("%s - ", val)
|
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)
|
|
||||||
}
|
}
|
||||||
|
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 -
|
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,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
|
```go
|
||||||
|
package main
|
||||||
import (
|
|
||||||
"fmt"
|
import (
|
||||||
"strconv"
|
"fmt"
|
||||||
)
|
"strconv"
|
||||||
|
)
|
||||||
func main() {
|
|
||||||
var orig string = "666"
|
func main() {
|
||||||
var an int
|
var orig string = "666"
|
||||||
var newS string
|
var an int
|
||||||
|
var newS string
|
||||||
fmt.Printf("The size of ints is: %d\n", strconv.IntSize)
|
|
||||||
|
fmt.Printf("The size of ints is: %d\n", strconv.IntSize)
|
||||||
an, _ = strconv.Atoi(orig)
|
|
||||||
fmt.Printf("The integer is: %d\n", an)
|
an, _ = strconv.Atoi(orig)
|
||||||
an = an + 5
|
fmt.Printf("The integer is: %d\n", an)
|
||||||
newS = strconv.Itoa(an)
|
an = an + 5
|
||||||
fmt.Printf("The new string is: %s\n", newS)
|
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 语句来对可能出现的错误进行分类处理。
|
在第 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/)** )。
|
||||||
|
|
||||||
## 链接
|
## 链接
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user