You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
golang-tutorial/demo/string/rune.go

43 lines
615 B

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")
// 特殊字符
name = "Señor"
printBytes(name)
fmt.Printf("\n")
printChars(name)
// 打印汉字
fmt.Printf("\n")
name = "萬少波"
printBytes(name)
fmt.Printf("\n")
printChars(name)
}