Merge remote-tracking branch 'origin'

This commit is contained in:
Unknown
2013-06-22 21:30:18 +08:00
2 changed files with 19 additions and 19 deletions

View File

@@ -7,7 +7,7 @@ Go 支持以下 2 种形式的字面值:
- 解释字符串:
该类字符串使用双引号括起来,其中的相关的转字符将被替换,这些转字符包括:
该类字符串使用双引号括起来,其中的相关的转字符将被替换,这些转字符包括:
- `\n`:换行符
- `\r`:回车符
@@ -67,4 +67,4 @@ Go 支持以下 2 种形式的字面值:
##链接
- [目录](directory.md)
- 上一节:[基本类型和运算符](04.5.md)
- 下一节:[strings 和 strconv 包](04.7.md)
- 下一节:[strings 和 strconv 包](04.7.md)

View File

@@ -15,21 +15,21 @@ Example 4.13 [presuffix.go](examples/chapter_4/presuffix.go)
package main
import (
fmt
strings
"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))
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"))
}
输出:
T/F? Does the string This is an example of a string have prefix Th? True
T/F? Does the string "This is an example of a string" have prefix Th? true
这个例子同样演示了转字符 `\` 和格式化字符串的使用。
这个例子同样演示了转字符 `\` 和格式化字符串的使用。
##4.7.2 字符串包含关系
`Contains` 判断字符串 `s` 是否包含 `substr`
@@ -75,10 +75,10 @@ Example 4.14 [index_in_string.go](examples/chapter_4/index_in_string.go)
输出:
The position of Marc is: 8
The position of the first instance of Hi is: 0
The position of the last instance of Hi is: 14
The position of Burger is: -1
The position of "Marc" is: 8
The position of the first instance of "Hi" is: 0
The position of the last instance of "Hi" is: 14
The position of "Burger" is: -1
##4.7.4 字符串替换
`Replace` 用于将字符串 `str` 中的前 `n` 个字符串 `old` 替换为字符串 `new`,并返回一个新的字符串,如果 `n = -1` 则替换所有字符串 `old` 为字符串 `new`
@@ -112,7 +112,7 @@ Example 4.15 [count_substring.go](examples/chapter_4/count_substring.go)
输出:
Number of Hs in Hello, how is it going, Hugo? is: 2
Number of H's in Hello, how is it going, Hugo? is: 2
Number of double gs in gggggggggg is: 5
##4.7.6 重复字符串
@@ -178,7 +178,7 @@ Example 4.17 [toupper_lower.go](examples/chapter_4/toupper_lower.go)
The uppercase string is: HEY, HOW ARE YOU GEORGE?
##4.7.8 修剪字符串
你可以使用 `strings.TrimSpace(s)` 来剔除字符串开头和结尾的空白符号;如果你想要剔除指定字符,则可以使用 `strings.Trim(s, cut)` 来将开头和结尾的 `cut` 去除掉。该函数的第二个参数可以包含任何字符,如果你只想剔除开头或者结尾的字符串,则可以使用 `TrimLeft` 或者 `TrimRight` 来实现。
你可以使用 `strings.TrimSpace(s)` 来剔除字符串开头和结尾的空白符号;如果你想要剔除指定字符,则可以使用 `strings.Trim(s, "cut")` 来将开头和结尾的 `cut` 去除掉。该函数的第二个参数可以包含任何字符,如果你只想剔除开头或者结尾的字符串,则可以使用 `TrimLeft` 或者 `TrimRight` 来实现。
##4.7.9 分割字符串
`strings.Fields(s)` 将会利用 1 个或多个空白符号来作为动态长度的分隔符将字符串分割成若干小块,并返回一个 slice如果字符串只包含空白符号则返回一个长度为 0 的 slice。
@@ -241,14 +241,14 @@ Example 4.18 [strings_splitjoin.go](examples/chapter_4/strings_splitjoin.go)
该包包含了一些变量用于获取程序运行的操作系统平台下 int 类型所占的位数,如:`strconv.IntSize`
任何类型 T 转换为字符串总是成功的。
任何类型 **T** 转换为字符串总是成功的。
针对从数字类型转换到字符串Go 提供了以下函数:
- `strconv.Itoa(i int) string` 返回数字 i 所表示的字符串类型的十进制数。
- `strconv.FormatFloat(f float64, fmt byte, prec int, bitSize int) string` 将 64 位浮点型的数字转换为字符串,其中 `fmt` 表示格式(其值可以是 `b``e``f``g``prec` 表示精度,`bitSize` 则使用 32 表示 float32用 64 表示 float64。
- `strconv.FormatFloat(f float64, fmt byte, prec int, bitSize int) string` 将 64 位浮点型的数字转换为字符串,其中 `fmt` 表示格式(其值可以是 `'b'``'e'``'f'``'g'``prec` 表示精度,`bitSize` 则使用 32 表示 float32用 64 表示 float64。
将字符串转换为其它类型 tp 并不总是可能的,可能会在运行时抛出错误 `parsing “…”: invalid argument`
将字符串转换为其它类型 **tp** 并不总是可能的,可能会在运行时抛出错误 `parsing "…": invalid argument`
针对从字符串类型转换为数字类型Go 提供了以下函数:
@@ -297,4 +297,4 @@ Example 4.19 [string_conversion.go](examples/chapter_4/string_conversion.go)
##链接
- [目录](directory.md)
- 上一节:[字符串](04.6.md)
- 下一节:[时间和日期](04.8.md)
- 下一节:[时间和日期](04.8.md)