当前位置:网站首页>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
}
边栏推荐
- [cloud native] event publishing and subscription in Nacos -- observer mode
- 数据泄露怎么办?'华生·K'7招消灭安全威胁
- 946. Verify stack sequence
- MySQL splits strings for conditional queries
- 国际自动机工程师学会(SAE International)战略投资几何伙伴
- DataPipeline双料入选中国信通院2022数智化图谱、数据库发展报告
- Association modeling method in SAP segw transaction code
- 时钟周期
- Overflow toolbar control in SAP ui5 view
- Navigation property and entityset usage in SAP segw transaction code
猜你喜欢

Laravel document reading notes -mews/captcha use (verification code function)

Although the volume and price fall, why are the structural deposits of commercial banks favored by listed companies?

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

Android本地Sqlite数据库的备份和还原

Introduction aux contrôles de la page dynamique SAP ui5

Flutter 绘制波浪移动动画效果,曲线和折线图

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é.

Overflow toolbar control in SAP ui5 view

Introduction to the principle of DNS

946. 验证栈序列
随机推荐
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é.
ASEMI整流桥HD06参数,HD06图片,HD06应用
[深度学习论文笔记]UCTransNet:从transformer的通道角度重新思考U-Net中的跳跃连接
The solution of outputting 64 bits from printf format%lld of cross platform (32bit and 64bit)
How to protect user privacy without password authentication?
Flutter InkWell & Ink组件
leetcode:221. Maximum square [essence of DP state transition]
CAN和CAN FD
How can non-technical departments participate in Devops?
程序员成长第八篇:做好测试工作
RHCSA8
使用 jMeter 对 SAP Spartacus 进行并发性能测试
How to realize batch sending when fishing
《2022年中國銀行業RPA供應商實力矩陣分析》研究報告正式啟動
Rocky basic command 3
Asemi rectifier bridge hd06 parameters, hd06 pictures, hd06 applications
mysql econnreset_Nodejs 套接字报错处理 Error: read ECONNRESET
Talking about fake demand from takeout order
Association modeling method in SAP segw transaction code
AVC1与H264的区别