当前位置:网站首页>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
}
边栏推荐
- Taobao short video, why the worse the effect
- Concurrent performance test of SAP Spartacus with JMeter
- SAP SEGW 事物码里的 Association 建模方式
- 数据湖(七):Iceberg概念及回顾什么是数据湖
- Natural language processing series (I) introduction overview
- Datapipeline was selected into the 2022 digital intelligence atlas and database development report of China Academy of communications and communications
- JXL notes
- 解决uni-app配置页面、tabBar无效问题
- leetcode:221. 最大正方形【dp状态转移的精髓】
- Cf:a. the third three number problem
猜你喜欢

RHCSA10
![[cloud native] use of Nacos taskmanager task management](/img/ad/24bdd4572ef9990238913cb7cd16f8.png)
[cloud native] use of Nacos taskmanager task management

无密码身份验证如何保障用户隐私安全?

Leetcode20. Valid parentheses

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

A specific example of ABAP type and EDM type mapping in SAP segw transaction code

Actual combat simulation │ JWT login authentication

Lepton 无损压缩原理及性能分析
![[Nacos cloud native] the first step of reading the source code is to start Nacos locally](/img/f8/d9b848593cf7380a6c99ee0a8158f8.png)
[Nacos cloud native] the first step of reading the source code is to start Nacos locally

阿里云SLB负载均衡产品基本概念与购买流程
随机推荐
手把手带你入门Apache伪静态的配置
A small talk caused by the increase of sweeping
Introduction to the principle of DNS
Hundred days to complete the open source task of the domestic database opengauss -- openguass minimalist version 3.0.0 installation tutorial
Lepton 无损压缩原理及性能分析
Pandora IOT development board learning (HAL Library) - Experiment 7 window watchdog experiment (learning notes)
SAP UI5 FlexibleColumnLayout 控件介绍
Rocky基础知识1
CF:A. The Third Three Number Problem【关于我是位运算垃圾这个事情】
Hiengine: comparable to the local cloud native memory database engine
量价虽降,商业银行结构性存款为何受上市公司所偏爱?
Yyds dry goods inventory # solve the real problem of famous enterprises: move the round table
I'm doing open source in Didi
Leetcode20. Valid parentheses
##无监控,不运维,以下是监控里常用的脚本监控
Notion 类笔记软件如何选择?Notion 、FlowUs 、Wolai 对比评测
RHCSA9
946. Verify stack sequence
Changing JS code has no effect
从外卖点单浅谈伪需求