当前位置:网站首页>go 字符串操作
go 字符串操作
2022-07-05 13:00:00 【UPythonFish】
go 字符串操作
Go 语言中的字符串是一个字节切片。把内容放在双引号””之间,并且Go 中的字符串是兼容 Unicode 编码的,并且使用 UTF-8 进行编码。由于字符串是一个字节切片,所以我们可以获取字符串的每一个字节。
func main() {
s := "hello world"
for i:=0;i<len(s);i++{
fmt.Println(string(s[i])) // 不加 string 打印出来的是字符对应的 Unicode 编码
}
}
rune
for 循环字符串,默认是一个字节一个字节循环,但我们的中文是采用3个字节对应一个汉字,因此,直接循环中文,就会导致显示乱码
func main() {
s1 := "中国"
for i:=0;i<len(s1);i++{
fmt.Print(string(s1[i])) // ä¸å›½
}
}
因此,当字符串中出现中文时,为防止乱码现象,可以将字符串做成 rune(int32) 的切片去 for 循环
func main() {
s := "中 国"
r := []rune(s)
for i:=0;i<len(r);i++{
fmt.Print(string(r[i])) // 中 国
}
}
byte切片和rune切片构建字符串
既然 go 底层存储字符串就是存储的byte,那么我们是不是可以直接建立一个切片,切片里面放入一个个字节呢。
答案显而易见是可以的。
"hello world" 十进制字节为 104 101 108 108 111 32 119 111 114 108 100 也可以放入16进制
func main() {
byteSlice := []byte{
104,101,108,108,111,32,119,111,114,108,100}
str := string(byteSlice)
fmt.Println(str) // hello world
}
同样,也可以建立rune切片来构建中文字符
"中 国" 十进制字节为 20013 32 22269
func main() {
runeSlice := []rune{
20013,32,22269 } // 32指的空格
str := string(runeSlice)
fmt.Println(str) // 中 国
}
byte(uint8, 一个字节) 和 rune(int32, 四个字节) 这两种类型,来代表字节和字符串
字符串长度
字符串长度统计,在python中可以用len方法来统计,同样在go中,也有一个内置函数len(),但是这个len()在go中统计的是字符串的字节数,原因也在 go 中字符串是存储的一个个字节。而统计字符穿长度就需要用到go中另一个内置函数 utf8.RuneCountInString(),案例如下:
func main() {
s := "a中国"
fmt.Println(len(s)) // 统计的是字节长度
fmt.Println(utf8.RuneCountInString(s)) // 统计字符长度
}
修改字符串的方法
前面我们提到过,字符串在go中是一个值类型,虽然可以通过索引来取值,但是禁止根据索引修改值
func main(){
s := "hello"
fmt.Println(string(s[0])) // h
s[0] = 'a' // 报错,' ' 就是获取该字符的Unicode编码
}
那么如果我们想要对字符串进行更改,也是可以通过切片的方法去实现的, 但原字符串还是未改变的!
func main() {
s := "hello"
r := []rune(s)
r[1] = 'A'
fmt.Println(string(r)) // hAllo
fmt.Println(s) // hello
}
边栏推荐
- 自然语言处理系列(一)入门概述
- 事务的基本特性和隔离级别
- 解决 UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xa2 in position 107
- 手把手带你入门Apache伪静态的配置
- Install rhel8.2 virtual machine
- APICloud Studio3 WiFi真机同步和WiFi真机预览使用说明
- AVC1与H264的区别
- 逆波兰表达式
- Hiengine: comparable to the local cloud native memory database engine
- [cloud native] event publishing and subscription in Nacos -- observer mode
猜你喜欢

Binder通信过程及ServiceManager创建过程
![leetcode:221. Maximum square [essence of DP state transition]](/img/ea/158e8659657984794c52a0449e0ee5.png)
leetcode:221. Maximum square [essence of DP state transition]

解决uni-app配置页面、tabBar无效问题

逆波兰表达式
![[cloud native] event publishing and subscription in Nacos -- observer mode](/img/0f/34ab42b7fb0085f58f36eb67b6f107.png)
[cloud native] event publishing and subscription in Nacos -- observer mode

蜀天梦图×微言科技丨达梦图数据库朋友圈+1

leetcode:221. 最大正方形【dp状态转移的精髓】

Le rapport de recherche sur l'analyse matricielle de la Force des fournisseurs de RPA dans le secteur bancaire chinois en 2022 a été officiellement lancé.

百日完成国产数据库opengausss的开源任务--openGuass极简版3.0.0安装教程

爱可生SQLe审核工具顺利完成信通院‘SQL质量管理平台分级能力’评测
随机推荐
实现 1~number 之间,所有数字的加和
DataPipeline双料入选中国信通院2022数智化图谱、数据库发展报告
How to realize batch sending when fishing
碎片化知识管理工具Memos
leetcode:221. 最大正方形【dp状态转移的精髓】
Detailed explanation of navigation component of openharmony application development
What is the difference between Bi software in the domestic market
百日完成国产数据库opengausss的开源任务--openGuass极简版3.0.0安装教程
A small talk caused by the increase of sweeping
程序员成长第八篇:做好测试工作
Put functions in modules
A specific example of ABAP type and EDM type mapping in SAP segw transaction code
Rocky基础知识1
RHCSA8
自然语言处理系列(一)入门概述
Alipay transfer system background or API interface to avoid pitfalls
前缀、中缀、后缀表达式「建议收藏」
AVC1与H264的区别
使用Dom4j解析XML
数据泄露怎么办?'华生·K'7招消灭安全威胁