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
|
||||
|
||||
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。
|
||||
|
||||
|
146
eBook/04.7.md
146
eBook/04.7.md
@@ -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 (
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
)
|
||||
|
||||
func main() {
|
||||
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,26 +49,33 @@ 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
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
)
|
||||
|
||||
func main() {
|
||||
func main() {
|
||||
var str string = "Hi, I'm Marc, Hi."
|
||||
|
||||
fmt.Printf("The position of \"Marc\" is: ")
|
||||
@@ -75,7 +88,8 @@ Example 4.14 [index_in_string.go](examples/chapter_4/index_in_string.go)
|
||||
|
||||
fmt.Printf("The position of \"Burger\" is: ")
|
||||
fmt.Printf("%d\n", strings.Index(str, "Burger"))
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
输出:
|
||||
|
||||
@@ -88,24 +102,29 @@ 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
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
)
|
||||
|
||||
func main() {
|
||||
func main() {
|
||||
var str string = "Hello, how is it going, Hugo?"
|
||||
var manyG = "gggggggggg"
|
||||
|
||||
@@ -114,7 +133,8 @@ 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("%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
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
)
|
||||
|
||||
func main() {
|
||||
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,22 +176,27 @@ 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
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
)
|
||||
|
||||
func main() {
|
||||
func main() {
|
||||
var orig string = "Hey, how are you George?"
|
||||
var lower string
|
||||
var upper string
|
||||
@@ -177,7 +206,8 @@ Example 4.17 [toupper_lower.go](examples/chapter_4/toupper_lower.go)
|
||||
fmt.Printf("The lowercase string is: %s\n", lower)
|
||||
upper = strings.ToUpper(orig)
|
||||
fmt.Printf("The uppercase string is: %s\n", upper)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
输出:
|
||||
|
||||
@@ -201,18 +231,21 @@ 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
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
)
|
||||
|
||||
func main() {
|
||||
func main() {
|
||||
str := "The quick brown fox jumps over the lazy dog"
|
||||
sl := strings.Fields(str)
|
||||
fmt.Printf("Splitted in slice: %v\n", sl)
|
||||
@@ -229,7 +262,8 @@ Example 4.18 [strings_splitjoin.go](examples/chapter_4/strings_splitjoin.go)
|
||||
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,16 +308,17 @@ 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 (
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
)
|
||||
|
||||
func main() {
|
||||
func main() {
|
||||
var orig string = "666"
|
||||
var an int
|
||||
var newS string
|
||||
@@ -295,7 +330,8 @@ Example 4.19 [string_conversion.go](examples/chapter_4/string_conversion.go)
|
||||
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/)** )。
|
||||
|
||||
## 链接
|
||||
|
||||
|
Reference in New Issue
Block a user