当前位置:网站首页>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
}
边栏推荐
- 程序员成长第八篇:做好测试工作
- 从外卖点单浅谈伪需求
- Small case of function transfer parameters
- Halcon template matching actual code (I)
- 【服务器数据恢复】某品牌服务器存储raid5数据恢复案例
- 946. 验证栈序列
- 实现 1~number 之间,所有数字的加和
- SAP SEGW 事物码里的 Association 建模方式
- Hundred days to complete the open source task of the domestic database opengauss -- openguass minimalist version 3.0.0 installation tutorial
- A small talk caused by the increase of sweeping
猜你喜欢
I'm doing open source in Didi
Cf:a. the third three number problem
LeetCode20.有效的括号
[深度学习论文笔记]UCTransNet:从transformer的通道角度重新思考U-Net中的跳跃连接
Android本地Sqlite数据库的备份和还原
精彩速递|腾讯云数据库6月刊
Detailed explanation of navigation component of openharmony application development
leetcode:221. Maximum square [essence of DP state transition]
Introduction to sap ui5 flexiblecolumnlayout control
[cloud native] event publishing and subscription in Nacos -- observer mode
随机推荐
函数的默认参数&函数参数的多种方法
946. Verify stack sequence
ASEMI整流桥HD06参数,HD06图片,HD06应用
[cloud native] use of Nacos taskmanager task management
手把手带你入门Apache伪静态的配置
量价虽降,商业银行结构性存款为何受上市公司所偏爱?
Overflow toolbar control in SAP ui5 view
Asemi rectifier bridge hd06 parameters, hd06 pictures, hd06 applications
将函数放在模块中
The solution of outputting 64 bits from printf format%lld of cross platform (32bit and 64bit)
RHCSA8
Shi Zhenzhen's 2021 summary and 2022 outlook | colorful eggs at the end of the article
Navigation property and entityset usage in SAP segw transaction code
Datapipeline was selected into the 2022 digital intelligence atlas and database development report of China Academy of communications and communications
go 指针
爱可生SQLe审核工具顺利完成信通院‘SQL质量管理平台分级能力’评测
“百度杯”CTF比赛 九月场,Web:Upload
Natural language processing from Xiaobai to proficient (4): using machine learning to classify Chinese email content
SAP UI5 DynamicPage 控件介紹
#yyds干货盘点# 解决名企真题:搬圆桌