标签:其他 dex nbsp csharp 字符串 遇到 字符串处理 highlight 研究
【遍历字符串】
str := "test string" for i:= 0; i < len(str); i++ { fmt.Printf("%c ",str[i]) }
这样写似乎没什么问题,但是在Go语言中我们一般使用utf-8编码,在遇到中文或其他特殊字符时,就会出现问题。我们可以使用,rune + range 循环
str := "Se?or" for index, rune := range str { fmt.Printf("str[%d] ==> %c\n", index, rune) }
str[0] ==> S
str[1] ==> e
str[2] ==> ?
str[4] ==> o
str[5] ==> r
可以看到输出,2之后直接就是4,因为?占了两个字节。
字符串就先研究到这里,因为对字符串处理的需求可以说是最多的,后期遇到再做整理。
标签:其他 dex nbsp csharp 字符串 遇到 字符串处理 highlight 研究
原文地址:https://www.cnblogs.com/hatsusakana/p/9829229.html