Files
the-way-to-go_ZH_CN/eBook/18.1.md
songleo 7c06912c30 modified: 18.1.md
new file:   18.10.md
	new file:   18.11.md
	new file:   18.2.md
	new file:   18.3.md
	new file:   18.4.md
	modified:   18.5.md
	new file:   18.6.md
	new file:   18.7.md
	new file:   18.8.md
	new file:   18.9.md
	modified:   directory.md
2016-01-03 13:53:23 +08:00

1.1 KiB
Raw Blame History

18.1 字符串

1如何修改字符串中的一个字符

str:="hello"
c:=[]byte(s)
c[0]='c'
s2:= string(c) // s2 == "cello"

2如何获取字符串的子串

substr := str[n:m]

3如何使用for或者for-range遍历一个字符串

// gives only the bytes:
for i:=0; i < len(str); i++ {
 = str[i]
}
// gives the Unicode characters:
for ix, ch := range str {

}

4如何获取一个字符串的字节数len(str)

如何获取一个字符串的字符数:

最快速:
utf8.RuneCountInString(str)
`len([]int(str)) //TBD`

5如何连接字符串

最快速: 
`with a bytes.Buffer`(参考[章节7.2](07.2.md)

`Strings.Join()`(参考[章节4.7](04.7.md)

`+=`
str1 := "Hello " 
str2 := "World!"
str1 += str2 //str1 == "Hello World!"

6如何解析命令行参数使用os或者flag

(参考[例12.4](examples/chapter_12/fileinput.go)

链接