当前位置:网站首页>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
}
边栏推荐
- CF:A. The Third Three Number Problem【关于我是位运算垃圾这个事情】
- Lb10s-asemi rectifier bridge lb10s
- The Research Report "2022 RPA supplier strength matrix analysis of China's banking industry" was officially launched
- 关于 Notion-Like 工具的反思和畅想
- 峰会回顾|保旺达-合规和安全双驱动的数据安全整体防护体系
- SAP ui5 objectpagelayout control usage sharing
- Association modeling method in SAP segw transaction code
- How to choose note taking software? Comparison and evaluation of notion, flowus and WOLAI
- 爱可生SQLe审核工具顺利完成信通院‘SQL质量管理平台分级能力’评测
- Rocky基础命令3
猜你喜欢

Sorry, we can't open xxxxx Docx, because there is a problem with the content (repackaging problem)

Hundred days to complete the open source task of the domestic database opengauss -- openguass minimalist version 3.0.0 installation tutorial

PyCharm安装第三方库图解

Setting up sqli lab environment

Discussion on error messages and API versions of SAP ui5 getsaplogonlanguage is not a function

SAP UI5 FlexibleColumnLayout 控件介绍

逆波兰表达式

Pycharm installation third party library diagram

OpenHarmony应用开发之Navigation组件详解

MySQL giant pit: update updates should be judged with caution by affecting the number of rows!!!
随机推荐
MSTP and eth trunk
It's too convenient. You can complete the code release and approval by nailing it!
Shu tianmeng map × Weiyan technology - Dream map database circle of friends + 1
Yyds dry goods inventory # solve the real problem of famous enterprises: move the round table
《2022年中国银行业RPA供应商实力矩阵分析》研究报告正式启动
I'm doing open source in Didi
mysql econnreset_Nodejs 套接字报错处理 Error: read ECONNRESET
Although the volume and price fall, why are the structural deposits of commercial banks favored by listed companies?
函数传递参数小案例
LeetCode20.有效的括号
Talking about fake demand from takeout order
自然语言处理系列(一)入门概述
RHCAS6
Didi open source Delta: AI developers can easily train natural language models
Halcon 模板匹配实战代码(一)
946. Verify stack sequence
Association modeling method in SAP segw transaction code
使用Dom4j解析XML
Setting up sqli lab environment
Concurrent performance test of SAP Spartacus with JMeter