golang 字符串统计

golang内建只认utf8

如果传递的字符串里含有汉字什么的,最好使用 utf8.RuneCountInString() 统计

字符串统计几种方法:

- 使用 bytes.Count() 统计
- 使用 strings.Count() 统计
- 将字符串转换为 []rune 后调用 len 函数进行统计
- 使用 utf8.RuneCountInString() 统计

str:="HelloWord"
l1:=len([]rune(str))
l2:=bytes.Count([]byte(str),nil)-1)
l3:=strings.Count(str,"")-1
l4:=utf8.RuneCountInString(str)
fmt.Println(l1)
fmt.Println(l2)
fmt.Println(l3)
fmt.Println(l4)
打印结果:都是 9

  

注:在 Golang 中,如果字符串中出现中文字符不能直接调用 len 函数来统计字符串字符长度,这是因为在 Go 中,字符串是以 UTF-8 为格式进行存储的,在字符串上调用 len 函数,取得的是字符串包含的 byte 的个数。

str:="HelloWorld"

str1 := "Hello, 世界"
fmt.Println(len(str1)) // 打印结果:13
fmt.Println(len(str))  //打印结果:9  (如果是纯英文字符的字符串,可以使用来判断字符串的长度)
原文地址:https://www.cnblogs.com/si812cn/p/9638547.html