十四.golang的字符串
package main import ( "fmt" ) func main() { name := "Hello World" fmt.Println(name) }
上面的程序将会输出 Hello World。
package main import ( "fmt" ) func printBytes(s string) { for i:= 0; i < len(s); i++ { fmt.Printf("%x ", s[i]) } } func main() { name := "Hello World" printBytes(name) }
让我们稍微修改一下上面的程序,让它打印字符串的每一个字符。
package main import ( "fmt" ) func printBytes(s string) { for i:= 0; i < len(s); i++ { fmt.Printf("%x ", s[i]) } } func printChars(s string) { for i:= 0; i < len(s); i++ { fmt.Printf("%c ",s[i]) } } func main() { name := "Hello World" printBytes(name) fmt.Printf("\n") printChars(name) }
48 65 6c 6c 6f 20 57 6f 72 6c 64
H e l l o W o r l d
上面的程序获取字符串的每一个字符,虽然看起来是合法的,但却有一个严重的 bug。让我拆解这个代码来看看我们做错了什么。
package main import ( "fmt" ) func printBytes(s string) { for i:= 0; i < len(s); i++ { fmt.Printf("%x ", s[i]) } } func printChars(s string) { for i:= 0; i < len(s); i++ { fmt.Printf("%c ",s[i]) } } func main() { name := "Hello World" printBytes(name) fmt.Printf("\n") printChars(name) fmt.Printf("\n") name = "Señor" printBytes(name) fmt.Printf("\n") printChars(name) }
上面代码输出的结果是:
48 65 6c 6c 6f 20 57 6f 72 6c 64 H e l l o W o r l d 53 65 c3 b1 6f 72 S e à ± o r
package main import ( "fmt" ) func printBytes(s string) { for i:= 0; i < len(s); i++ { fmt.Printf("%x ", s[i]) } } func printChars(s string) { runes := []rune(s) for i:= 0; i < len(runes); i++ { fmt.Printf("%c ",runes[i]) } } func main() { name := "Hello World" printBytes(name) fmt.Printf("\n") printChars(name) fmt.Printf("\n\n") name = "Señor" printBytes(name) fmt.Printf("\n") printChars(name) }
在上面代码的第 14 行,字符串被转化为一个 rune 切片。然后我们循环打印字符。程序的输出结果是
48 65 6c 6c 6f 20 57 6f 72 6c 64 H e l l o W o r l d 53 65 c3 b1 6f 72 S e ñ o r
package main import ( "fmt" ) func printCharsAndBytes(s string) { for index, rune := range s { fmt.Printf("%c starts at byte %d\n", rune, index) } } func main() { name := "Señor" printCharsAndBytes(name) }
S starts at byte 0 e starts at byte 1 ñ starts at byte 2 o starts at byte 4 r starts at byte 5
package main import ( "fmt" ) func main() { byteSlice := []byte{0x43, 0x61, 0x66, 0xC3, 0xA9} str := string(byteSlice) fmt.Println(str) }
上面的程序中 byteSlice 包含字符串 Café 用 UTF-8 编码后的 16 进制字节。程序输出结果是
如果我们把 16 进制换成对应的 10 进制值会怎么样呢?上面的程序还能工作吗?让我们来试一试:
package main import ( "fmt" ) func main() { byteSlice := []byte{67, 97, 102, 195, 169}//decimal equivalent of {'\x43', '\x61', '\x66', '\xC3', '\xA9'} str := string(byteSlice) fmt.Println(str) }
package main import ( "fmt" ) func main() { runeSlice := []rune{0x0053, 0x0065, 0x00f1, 0x006f, 0x0072} str := string(runeSlice) fmt.Println(str) }
package main import ( "fmt" "unicode/utf8" ) func length(s string) { fmt.Printf("length of %s is %d\n", s, utf8.RuneCountInString(s)) } func main() { word1 := "Señor" length(word1) word2 := "Pets" length(word2) }
上面程序的输出结果是:
length of Señor is 5 length of Pets is 4
package main import ( "fmt" ) func mutate(s string)string { s[0] = 'a'//any valid unicode character within single quote is a rune return s } func main() { h := "hello" fmt.Println(mutate(h)) }
为了修改字符串,可以把字符串转化为一个 rune 切片。然后这个切片可以进行任何想要的改变,然后再转化为一个字符串。
package main import ( "fmt" ) func mutate(s []rune) string { s[0] = 'a' return string(s) } func main() { h := "hello" fmt.Println(mutate([]rune(h))) }