go语中字符串常用的系统函数

1.按字节统计字符串长度
var str string = “hello world”
len(str)

2.字符串的遍历,并解决中文问题
var str string = “hello world”
r:=rune(str)
for i:=0;i<len®;i++{
fmt.Printf("%c\n",r[i])
}

3.字符串转整数
n,err:=strconv.Atoi(“hello world”)
if err!=nil{
fmt.Println(“转换错误”,err)
}
else{
fmt.Pringln(“转换成功”,n)
}

4.整数转字符串
str = strconv.Itoa(12345)
fmt.Pringln("%s",str)

5.字符串转 []byte
var bytes = []byte(“hello world”)
fmt.Pringln(“bytes=%v/n”,bytes)

6.byte[] 转字符串
str= string(byte{1,2.3})
fmt.Pringln(“str=%s/n”,str)

7.10进制转2,8,16进制,并返回对应字符串
str = strconv.FormatInt(123,2) //第二个参数是对应的进制
str = strconv.FormatInt(123,8)
str = strconv.FormatInt(123,16)

8.查找子串是包含在指定字符串中
str = strings.Contains(“helloworld”,“hello”) //第二个参数是字串

9.统计一个字符串有几个指定字符串
num: = strings.Count(“helloworld”,“hello”) //结果1
fmt.Printf("%v\n",num)

10.不区分大小写的字符串比较
//区分大小写时用==
a = strings.EqualFold(“abc”,“ABC”)
fmt.Printf("%v\n",a) //true

11.返回字串第一次出现的index值,没有返回-1
index = strings.Index(“cabbbbs”,“ab”)
fmt.Printf("%v\n",index) //1

12.返回字串最后一次出现的index值,没有返回-1
index = strings.LastIndex(“cabbbabs”,“ab”)
fmt.Printf("%v\n",index) //5

13.将指定的字符串转换为另一个字符串
str:=“hello go go go”
str1 = strings.Replace(str,“go”,-1) //-1表示全部替换
fmt.Printf(“str=%v\n str1=%v\n”)

14.将某个字符作为指定标识,将字符串分割为字符数组
strArr:=strings.Split(“Hello,World”,",")
for i:=0;i<len(strArr);i++{
fmt.Printf(“str[%v]=%v”,i,strArr[])
}

15.将字符串进行大小写替换
str:=“hello world”
str= strings.ToUpper(str) //转换成大写
str= strings.ToLower(str) //转换成小写

16.将字符串左右两面空格去掉
str = strings.TrimSpace(" hello world ")

17.将字符串两边指定字符去掉
str = strings.Trim("!hello world!","!")

18.将字符串左侧,或右侧指定字符去掉
str =strings.TrimLeft("!hello world!","!") //左
str =strings.TrimRight("!hello world!","!") //右

19.判断字符串时否以指定字符串开头
a = strings.HasPrefix("!hello world","!") //true

20.判断字符串时否以指定字符串结尾
a = strings.HasSuffix("!hello world","!") //false

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值