介绍

使用golang怎么对字符串的长度进行获取?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

1。不同字符与获取字符串长度

获取字符串长度,是字符串操作的重要方法。理论来说,获取字符串长度,只要从头到尾查找一遍就可以了。但遗憾的是,不同字符具有不同的编码格式。拉丁字母一个字符只要一个字节就行,而中文则可能需要两道三个字节;UNICODE把所有字符设置为2个字节,utf - 8格式则把所有字符设置为1——3个字节。

因此,字符串长度的获得,不等于按字节数查找,而要根据不同字符编码查找。

2. golang中获取字符串长度的方法

对于中文开发者来说,经常需要对字符串进行长度判断.golang有自己的默认判断长度函数len();但遗憾的是,len()函数判断字符串长度的时候,是判断字符的字节数而不是字符长度,因此,在中文字符下,应该采用如下方法:

1)使用bytes.Count()统计

2)使用strings.Count()统计

3)将字符串转换为[]符文后调用len函数进行统计

4)使用utf8.RuneCountInString()统计

3。样例展示

s :=,“欢迎学习的len()去函数“   r :=,[]符文(开始于)   fmt.Println (len (r))   fmt.Println (len (s))   fmt.Println (bytes.Count([]字节(s), nil),安康;1)   fmt.Println (strings.Count(年代,““),安康;1)   fmt.Println (utf8.RuneCountInString (s))

<强>补充:去语言获取中英文混和字符串的长度以及子字符串的方法

1。纯英文字符串

使用len()函数。

testString1 :=,“中国!“   length2 :=, len (testString1)   fmt.Printf (“testString1 字符串的长度是:% d",, length2)

长度是6 .

2。中英文混合字符串

2.1先使用len()函数。

testString2 :=,“我爱你中国,我爱你中国!“   length3 :=, len (testString2)   fmt.Printf(“字符串的长度是:% d",, length3)   fmt.Printf (“testString2字符串的长度是:% d \ n",, length3)   fmt.Printf (“testString2中的最后一个字符是:% s \ n",, testString2 [length3-1])   fmt.Printf (“testString2中的最后一个字符是:% c \ n",, testString2 [length3-1])   fmt.Printf (“testString2中的下标6 -末尾的子字符串是:% s \ n", testString2 [15]):   fmt.Printf (“testString2中的下标6 -末尾的子字符串是:% s \ n", testString2 [16]):

这种方法的到的是字节数.Go语言中,中文字符按utf - 8编码,占3字节,故长度是31。故此方法不适用统计中英文混合或者中文字符串长度。

2.2使用utf8.RuneCountInString()方法。

testString2 :=,“我爱你中国,我爱你中国!“   length4 :=, utf8.RuneCountInString (testString2)   fmt.Printf(“使用utf8中的方法统计的字符串长度是:% d \ n",, length4)

此方法可统计字符数、输出结果是15。

2.3转成[]符文类型,再对此类型进行操作

testString2 :=,“我爱你中国,我爱你中国!“   temp :=,[]符文(testString2)   length5 :=, len(临时)   fmt.Printf(“使用符文统计的字符串的长度是:% d \ n",, length5)//获取字符串中最后一个字符   lastChar :=,字符串(临时[length5-1])//获取下标从0到3(不包括3)的子串   subString1 :=,临时(0:3)   subString2 :=,临时(完)   fmt.Printf (“testString2中的最后一个字符是:% s \ n",, lastChar)   fmt.Printf (“testString2中的下标0 - 2的子字符串是:% s \ n",字符串(subString1))   fmt.Printf (“testString2中的下标6 - 8的子字符串是:% s \ n",字符串(subString2))

此方法也可输出字符个数15。但是此方法能获取指定下标范围的子字符串,也能获取指定下标位置的字符。比第二种方法方便。

3。示例代码

package 主要   import  (   ,“fmt"   ,“unicode/utf8"   )   func  main (), {   ,//纯英文   ,testString1 :=,“中国!“   ,length2 :=, len (testString1)   ,fmt.Printf (“testString1字符串的长度是:% d \ n",, length2)   ,lastCharA :=, testString1 [length2-1]   ,//此处用% s格式输出最后一个字符会出错,只能用% c   ,fmt.Printf (“testString1字符串中最后一个字符是:% s \ n",, lastCharA)   ,fmt.Printf (“testString1字符串中最后一个字符是:% c \ n",, lastCharA)   ,fmt.Printf (“testString1中的下标0 - 2的子字符串是:% s \ n", testString1 [0:3])   ,fmt.Printf (“testString1中的下标3 -末尾的子字符串是:% s \ n", testString1 [3:])   ,fmt.Println ()   ,//中英文加一起15个字符   ,testString2 :=,“我爱你中国,我爱你中国!“   ,//此处长度是输出字节数,去语言中文字符是utf - 8编码,长度3字节,故此处应该是15 + 1 + 9 + 6=31   ,length3 :=, len (testString2)   ,fmt.Printf (“testString2字符串的长度是:% d \ n",, length3)   ,fmt.Printf (“testString2中的最后一个字符是:% s \ n",, testString2 [length3-1])   ,fmt.Printf (“testString2中的最后一个字符是:% c \ n",, testString2 [length3-1])   ,fmt.Printf (“testString2中的下标6 -末尾的子字符串是:% s \ n", testString2 [15]):   ,fmt.Printf (“testString2中的下标6 -末尾的子字符串是:% s \ n", testString2 [16]):   ,fmt.Println ()   ,//此处就是统计字符数   ,length4 :=, utf8.RuneCountInString (testString2)   ,fmt.Printf(“使用utf8中的方法统计的字符串长度是:% d \ n",, length4)   ,fmt.Println ()   ,//转成符文类型,再统计字符数   ,temp :=,[]符文(testString2)   ,//获取中英文混合字符串长度   ,length5 :=, len(临时)   ,fmt.Printf(“使用符文统计的字符串的长度是:% d \ n",, length5)   ,//获取字符串中最后一个字符   ,lastCharB :=,字符串(临时[length5-1])   ,//获取下标从0到3(不包括3)的子串   :=,temp, subString1  (0:3)   :=,temp, subString2  [6:]   ,fmt.Printf (“testString2中的最后一个字符是:% s \ n",, lastCharB)   ,fmt.Printf (“testString2中的下标0 - 2的子字符串是:% s \ n",字符串(subString1))   ,fmt.Printf (“testString2中的下标6 -末尾的子字符串是:% s \ n",字符串(subString2))   }