当前位置:网站首页>Go string operation
Go string operation
2022-07-05 13:12:00 【UPythonFish】
List of articles
go String manipulation
Go A string in a language is a byte slice . Put the content in double quotation marks ”” Between , also Go The string in is compatible Unicode Coded , And use UTF-8 Encoding . Because the string is a byte slice , So we can get every byte of the string .
func main() {
s := "hello world"
for i:=0;i<len(s);i++{
fmt.Println(string(s[i])) // No addition string What is printed out is the corresponding character Unicode code
}
}
rune
for Loop string , The default is a byte by byte cycle , But our Chinese is 3 Bytes correspond to a Chinese character , therefore , Direct circular Chinese , It will cause garbled code
func main() {
s1 := " China "
for i:=0;i<len(s1);i++{
fmt.Print(string(s1[i])) // ä¸å›½
}
}
therefore , When Chinese appears in the string , To prevent garbled code , You can make strings rune(int32) Slice of for loop
func main() {
s := " in countries "
r := []rune(s)
for i:=0;i<len(r);i++{
fmt.Print(string(r[i])) // in countries
}
}
byte Slicing and rune Slice build string
since go The underlying storage string is stored byte, So can we directly create a slice , Put bytes in the slice .
The answer is obviously yes .
"hello world" Decimal bytes are 104 101 108 108 111 32 119 111 114 108 100 You can also put 16 Base number
func main() {
byteSlice := []byte{
104,101,108,108,111,32,119,111,114,108,100}
str := string(byteSlice)
fmt.Println(str) // hello world
}
Again , You can also create rune Slice to build Chinese characters
" in countries " Decimal bytes are 20013 32 22269
func main() {
runeSlice := []rune{
20013,32,22269 } // 32 Refers to the space
str := string(runeSlice)
fmt.Println(str) // in countries
}
byte(uint8, A byte ) and rune(int32, Four bytes ) These two types , To represent bytes and strings
String length
String length statistics , stay python Can be used in the len Methods to count , Also in go in , There is also a built-in function len(), But this len() stay go What is counted in is the number of bytes of the string , The reason is also go The string in is a byte stored . The statistics of the length of characters need to be used go Another built-in function in utf8.RuneCountInString(), The case is as follows :
func main() {
s := "a China "
fmt.Println(len(s)) // The statistics are byte length
fmt.Println(utf8.RuneCountInString(s)) // Count character length
}
How to modify the string
We mentioned that earlier , The string is in go Is a value type , Although it can be retrieved by index , However, it is forbidden to modify the value according to the index
func main(){
s := "hello"
fmt.Println(string(s[0])) // h
s[0] = 'a' // Report errors ,' ' Is to get this character Unicode code
}
So if we want to change the string , It can also be achieved by slicing , But the original string is still unchanged !
func main() {
s := "hello"
r := []rune(s)
r[1] = 'A'
fmt.Println(string(r)) // hAllo
fmt.Println(s) // hello
}
边栏推荐
- SAP UI5 视图里的 OverflowToolbar 控件
- 数据湖(七):Iceberg概念及回顾什么是数据湖
- 《2022年中國銀行業RPA供應商實力矩陣分析》研究報告正式啟動
- Asemi rectifier bridge hd06 parameters, hd06 pictures, hd06 applications
- Rocky基础知识1
- Get to know linkerd project for the first time
- Difference between avc1 and H264
- 关于 Notion-Like 工具的反思和畅想
- C# 对象存储
- 同事半个月都没搞懂selenium,我半个小时就给他整明白!顺手秀了一波爬淘宝的操作[通俗易懂]
猜你喜欢

SAP SEGW 事物码里的导航属性(Navigation Property) 和 EntitySet 使用方法

Small case of function transfer parameters

逆波兰表达式

Overflow toolbar control in SAP ui5 view

C# 对象存储

Simple page request and parsing cases

Leetcode20. Valid parentheses

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

Principle and configuration of RSTP protocol

精彩速递|腾讯云数据库6月刊
随机推荐
MSTP and eth trunk
Rocky基础知识1
关于 Notion-Like 工具的反思和畅想
使用Dom4j解析XML
从外卖点单浅谈伪需求
Run, open circuit
函数的默认参数&函数参数的多种方法
Shu tianmeng map × Weiyan technology - Dream map database circle of friends + 1
JXL notes
I'm doing open source in Didi
[Nacos cloud native] the first step of reading the source code is to start Nacos locally
LB10S-ASEMI整流桥LB10S
What is the difference between Bi software in the domestic market
APICloud Studio3 API管理与调试使用教程
Detailed explanation of navigation component of openharmony application development
Concurrent performance test of SAP Spartacus with JMeter
js判断数组中是否存在某个元素(四种方法)
ASEMI整流桥HD06参数,HD06图片,HD06应用
解决uni-app配置页面、tabBar无效问题
There is no monitoring and no operation and maintenance. The following is the commonly used script monitoring in monitoring