go語言string遍歷細節
阿新 • • 發佈:2019-01-04
/** * Created by Administrator on 13-12-18. */ package main import ( "fmt" "os" ) func main() { str := "hello, 世界" n := len(str) for i := 0; i < n; i++ { fmt.Fprintf(os.Stdout, "%d %v %c\n", i, str[i], str[i]) } fmt.Fprintf(os.Stdout, "---------------\n") for i , v := range str { fmt.Fprintf(os.Stdout, "%d %v %c\n", i, v, v) } fmt.Fprintf(os.Stdout, "---------------\n") rs := []rune(str) n = len(rs) for i := 0; i < n; i++ { fmt.Fprintf(os.Stdout, "%d %v %c\n", i, rs[i], rs[i]) } }
0 104 h
1 101 e
2 108 l
3 108 l
4 111 o
5 44 ,
6 32
7 228 ä
8 184 ¸
9 150
10 231 ç
11 149
12 140
---------------
0 104 h
1 101 e
2 108 l
3 108 l
4 111 o
5 44 ,
6 32
7 19990 世
10 30028 界
---------------
0 104 h
1 101 e
2 108 l
3 108 l
4 111 o
5 44 ,
6 32
7 19990 世
8 30028 界