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 (如果是纯英文字符的字符串,可以使用来判断字符串的长度)
golang 字符串统计的更多相关文章
- HDOJ2017字符串统计
字符串统计 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- HDOJ2017_字符串统计
这是一道水题 HDOJ2017_字符串统计 #include<iostream> #include<string> #include<stdio.h> #inclu ...
- GO语言的进阶之路-Golang字符串处理以及文件操作
GO语言的进阶之路-Golang字符串处理以及文件操作 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 我们都知道Golang是一门强类型的语言,相比Python在处理一些并发问题也 ...
- Golang 字符串转URLCode
Golang 字符串转URLCode 最近因调用gitlab API,在生成某些字符串的时候直接请求 gitlab API 失败, url如下: keysURL := "http://192 ...
- 给出一个string字符串,统计里面出现的字符个数
给出一个string字符串,统计里面出现的字符个数 解决方案: 使用algorithm里面的count函数,使用方法是count(begin,end,'c'),其中begin指的是起始地址,end指的 ...
- hdu2017 字符串统计【C++】
字符串统计 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- 【Java例题】8.2 手工编写字符串统计的可视化程序
2. 手工编写字符串统计的可视化程序. 一个Frame窗体容器,布局为null,两个TextField组件,一个Button组件. Button组件上添加ActionEvent事件监听器Actio ...
- [转] golang 字符串比较是否相等
1 前言 strings.EqualFold不区分大小写,"==" 区分且直观. 2 代码 golang字符串比较的三种常见方法 fmt.Println("go" ...
- sh_15_字符串统计操作
sh_15_字符串统计操作 hello_str = "hello hello" # 1. 统计字符串长度 print(len(hello_str)) # 2. 统计某一个小(子)字 ...
随机推荐
- MySQL数据库优化的方式
1.选取最适用的字段属性 MySQL可以很好的支持大数据量的存取,但是一般说来,数据库中的表越小,在它上面执行的查询也就会越快.因此,在创建表的时候,为了获得更好的性能,我们可以将表中字段的宽度设得尽 ...
- eact native生成APP报错:You have not accepted the license agreements of the following SDK components:
一.报错信息 * What went wrong: A problem occurred configuring project ':app'. > You have not accepted ...
- [LeetCode&Python] Problem 543. Diameter of Binary Tree
Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a b ...
- java-this和super的区别
1.this和super都代表什么: - this:代表当前对象的引用,谁来调用我,我就代表谁 - super:代表当前对象父类的引用 - super(...)或者this(...)必须放在构造方法的 ...
- 3D打印材料PLA,ABS对比
- hdu6440 Dream(费马小定理)
保证 当 n^p=n(mod p) 是成立 只要保证n*m=n*m(mod p); #include<bits/stdc++.h> using namespace std; int ma ...
- dijksta 模板
#include<bits/stdc++.h> using namespace std; #define INF 0x3f3f3f3f ]; ]; ][]; void dijkstra(i ...
- PyCharm下载安装
PyCharm 是一款功能强大的 Python 编辑器,具有跨平台性,鉴于目前最新版 PyCharm 使用教程较少,为了节约时间,来介绍一下 PyCharm 在 Windows下是如何安装的. 这是 ...
- LNMP环境包安装IonCube教程
ioncube是业内优秀的php加密解密解决方案.和zend guard相比,ioncube具有如下优势: 1. 安全:zend guard的版本不是非常安全,网络上有破解使用zend,下面我们来看I ...
- python文件处理复习
1.创建文件 >>> file('test.txt','w') -->在当前路径下创建文件 test.txt是文件名 w是打开文件的方式,r是读,w是写,a是追加,如果当前 ...