This commit is contained in:
Unknown
2014-04-09 00:04:59 -04:00
parent f66ca15956
commit 480ec9f46a
10 changed files with 114 additions and 62 deletions

View File

@@ -1,7 +1,9 @@
#4.7 strings 和 strconv 包
# 4.7 strings 和 strconv 包
作为一种基本数据结构每种语言都有一些对于字符串的预定义处理函数。Go 中使用 `strings` 包来完成对字符串的主要操作。
##4.7.1 前缀和后缀
## 4.7.1 前缀和后缀
`HasPrefix` 判断字符串 `s` 是否以 `prefix` 开头:
strings.HasPrefix(s, prefix string) bool
@@ -31,12 +33,14 @@ Example 4.13 [presuffix.go](examples/chapter_4/presuffix.go)
这个例子同样演示了转义字符 `\` 和格式化字符串的使用。
##4.7.2 字符串包含关系
## 4.7.2 字符串包含关系
`Contains` 判断字符串 `s` 是否包含 `substr`
strings.Contains(s, substr string) bool
##4.7.3 判断子字符串或字符在父字符串中出现的位置(索引)
## 4.7.3 判断子字符串或字符在父字符串中出现的位置(索引)
`Index` 返回字符串 `str` 在字符串 `s` 中的索引(`str` 的第一个字符的索引),-1 表示字符串 `s` 不包含字符串 `str`
strings.Index(s, str string) int
@@ -80,12 +84,14 @@ Example 4.14 [index_in_string.go](examples/chapter_4/index_in_string.go)
The position of the last instance of "Hi" is: 14
The position of "Burger" is: -1
##4.7.4 字符串替换
## 4.7.4 字符串替换
`Replace` 用于将字符串 `str` 中的前 `n` 个字符串 `old` 替换为字符串 `new`,并返回一个新的字符串,如果 `n = -1` 则替换所有字符串 `old` 为字符串 `new`
strings.Replace(str, old, new, n) string
##4.7.5 统计字符串出现次数
## 4.7.5 统计字符串出现次数
`Count` 用于计算字符串 `str` 在字符串 `s` 中出现的非重叠次数:
strings.Count(s, str string) int
@@ -115,7 +121,8 @@ Example 4.15 [count_substring.go](examples/chapter_4/count_substring.go)
Number of H's in Hello, how is it going, Hugo? is: 2
Number of double gs in gggggggggg is: 5
##4.7.6 重复字符串
## 4.7.6 重复字符串
`Repeat` 用于重复 `count` 次字符串 `s` 并返回一个新的字符串:
strings.Repeat(s, count int) string
@@ -141,7 +148,8 @@ Example 4.16 [repeat_string.go](examples/chapter_4/repeat_string.go)
The new repeated string is: Hi there! Hi there! Hi there!
##4.7.7 修改字符串大小写
## 4.7.7 修改字符串大小写
`ToLower` 将字符串中的 Unicode 字符全部转换为相应的小写字符:
strings.ToLower(s) string
@@ -177,17 +185,20 @@ Example 4.17 [toupper_lower.go](examples/chapter_4/toupper_lower.go)
The lowercase string is: hey, how are you george?
The uppercase string is: HEY, HOW ARE YOU GEORGE?
##4.7.8 修剪字符串
## 4.7.8 修剪字符串
你可以使用 `strings.TrimSpace(s)` 来剔除字符串开头和结尾的空白符号;如果你想要剔除指定字符,则可以使用 `strings.Trim(s, "cut")` 来将开头和结尾的 `cut` 去除掉。该函数的第二个参数可以包含任何字符,如果你只想剔除开头或者结尾的字符串,则可以使用 `TrimLeft` 或者 `TrimRight` 来实现。
##4.7.9 分割字符串
## 4.7.9 分割字符串
`strings.Fields(s)` 将会利用 1 个或多个空白符号来作为动态长度的分隔符将字符串分割成若干小块,并返回一个 slice如果字符串只包含空白符号则返回一个长度为 0 的 slice。
`strings.Split(s, sep)` 用于自定义分割符号来对指定字符串进行分割,同样返回 slice。
因为这 2 个函数都会返回 slice所以习惯使用 for-range 循环来对其进行处理(第 7.3 节)。
##4.7.10 拼接 slice 到字符串
## 4.7.10 拼接 slice 到字符串
`Join` 用于将元素类型为 string 的 slice 使用分割符号来拼接组成一个字符串:
Strings.Join(sl []string, sep string)
@@ -230,13 +241,15 @@ Example 4.18 [strings_splitjoin.go](examples/chapter_4/strings_splitjoin.go)
其它有关字符串操作的文档请参阅官方文档 [http://golang.org/pkg/strings/](http://golang.org/pkg/strings/) ***译者注:国内用户可访问 [http://docs.studygolang.com/pkg/strings/](http://docs.studygolang.com/pkg/strings/)*** )。
##4.7.11 从字符串中读取内容
## 4.7.11 从字符串中读取内容
函数 `strings.NewReader(str)` 用于生成一个 `Reader` 并读取字符串中的内容,然后返回指向该 `Reader` 的指针,从其它类型读取内容的函数还有:
- `Read()` 从 []byte 中读取内容。
- `ReadByte()``ReadRune()` 从字符串中读取下一个 byte 或者 rune。
##4.7.12 字符串与其它类型的转换
## 4.7.12 字符串与其它类型的转换
与字符串相关的类型转换都是通过 `strconv` 包实现的。
该包包含了一些变量用于获取程序运行的操作系统平台下 int 类型所占的位数,如:`strconv.IntSize`
@@ -294,7 +307,8 @@ Example 4.19 [string_conversion.go](examples/chapter_4/string_conversion.go)
更多有关该包的讨论,请参阅官方文档 [http://golang.org/pkg/strconv/](http://golang.org/pkg/strconv/) ***译者注:国内用户可访问 [http://docs.studygolang.com/pkg/strconv/](http://docs.studygolang.com/pkg/strconv/)*** )。
##链接
## 链接
- [目录](directory.md)
- 上一节:[字符串](04.6.md)
- 下一节:[时间和日期](04.8.md)