加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Golang(Go语言)代码技巧之字符串(string)

发布时间:2020-12-16 18:33:50 所属栏目:大数据 来源:网络整理
导读:改变字符串中的某些字符 str := "hello roc" bytes [] byte ( str ) bytes [ 1 ] = 'a' string //str == "hallo roc" 截取子串 substr str [ n : m //截取从索引n到m-1的子串 遍历字符串 //for遍历,此方式只能遍历存放ASCII //编码的字符串,比如中文就不行
  • 改变字符串中的某些字符
  
  
str := "hello roc"bytes []byte(str)bytes[1]='a' string //str == "hallo roc"
  • 截取子串
   
   
substr str[n:m//截取从索引n到m-1的子串
  • 遍历字符串
    
    
//for遍历,此方式只能遍历存放ASCII//编码的字符串,比如中文就不行for i 0;< len); i++{//... = str[i]}//for-range遍历,此方式可以遍历Unicode//编码的字符串,不担心乱码 index,char range str fmt.Printf("%d %cn"indexchar)}
  • 计算字符串长度
  
    
//字符串中字符全为ASCII中的字符len)//字符串中含非ASCII的Unicode字符utf8.RuneCountInString)
  • 连接字符串
速度最快的写法:
var buf bytes.Bufferbuf.WriteString"hello ""roc"fmt.Println.String())//hello roc
还有如下两种方式:
strings.Join([]{"hello"" world"},68)">"")
  
     
"hello"+="roc"

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读