码迷,mamicode.com
首页 > 其他好文 > 详细

10. 字符串

时间:2018-10-22 12:58:46      阅读:109      评论:0      收藏:0      [点我收藏+]

标签:其他   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,因为?占了两个字节。

 

字符串就先研究到这里,因为对字符串处理的需求可以说是最多的,后期遇到再做整理。

10. 字符串

标签:其他   dex   nbsp   csharp   字符串   遇到   字符串处理   highlight   研究   

原文地址:https://www.cnblogs.com/hatsusakana/p/9829229.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!