GO中字符串常用的系统函数有哪些


在平时字符串的使用频率是非常高的,那么有哪些函数式我们经常要用到的呢,在这里为大家整理以下。

统计字符串的长度,len

    var s string = "hello world"
	var s2 string = "你好"
	fmt.Println(len(s))// 11
	fmt.Println(len(s2))//6 中文字符在unicode下占2个字节,在utf-8编码下占3个字节,而golang默认编码正好是utf-8

注意: len是按字节数计算的

遍历字符,并处理中文 r := []rune(s)

    var s3 string = "hello 你好"
	runes := []rune(s3)

	for i := 0; i < len(runes); i++{
		fmt.Printf("字符=%c\n",runes[i])
	}

在这里插入图片描述

字符串转整数

    var s4 string = "123"
	num, err := strconv.Atoi(s4)
	if err != nil {
		panic(err)
	}
	fmt.Printf("num type is %T, value = %v\n",num,num)
	// num type is int, value = 123

整数转字符串

    var num1 int = 66
	str := strconv.Itoa(num1)
	fmt.Printf("str type is %T, value = %q\n",str,str)
	// str type is string, value = "66"

字符串转 []byte

    var s5 string = "hello"
	bytes := []byte(s5)
	fmt.Printf("bytes = %v ",bytes)

在这里插入图片描述

这里把字符转为相对应的ASCII码

[]byte转字符串

    s6 := string([]byte{97,98,99})
	fmt.Printf("s6 = %v\n",s6) // s6 = abc

十进制转其它进制数字

    var num3 int64 = 10
    // 想转换成 xx进制,就把第二个参数写成几,例如二进制2,八进制8,十六进制16
	formatInt := strconv.FormatInt(num3, 2)
	fmt.Printf("对应的2进制数字: %v\n",formatInt)
	// 对应的2进制数字: 1010

字符串是否包含某子串

    boo := strings.Contains("hello 你好", "你好")
	fmt.Printf("是否包含: %v\n",boo)// true

统计在字符串中出现子串的次数

    count := strings.Count("hello", "l")
	fmt.Printf("出现次数: %v\n",count) // 2

如果没有出现返回 0,否则 返回 大于0的数

不区分大小写比较

fmt.Println(strings.EqualFold("abc","Abc")) // true

== 是区分大小写的比较

子串在母串第一次出现的索引位置(从0开始)

没有出现就返回 -1

    fmt.Println(strings.Index("first blood","ood")) // 8
	fmt.Println(strings.Index("first blood","oodm")) // -1

子串在母串最后一次出现的位置索引

没有则返回 -1

    fmt.Println(strings.LastIndex("go golang","go")) // 3
	fmt.Println(strings.LastIndex("go golang","gog")) // -1

字符串替换 replace

func Replace(s, old, new string, n int) string
返回将s中前n个不重叠old子串都替换为new的新字符串,如果n<0会替换所有old子串。

    fmt.Println(strings.Replace("hello,world,hello,world","world","go",1)) // hello,go,hello,world
	fmt.Println(strings.Replace("hello,world,hello,world","world","go",-1)) // hello,go,hello,go

字符串切割

func Split(s, sep string) []string
用去掉s中出现的sep的方式进行分割,会分割到结尾,并返回生成的所有片段组成的切片(每一个sep都会进行一次切割,即使两个sep相邻,也会进行两次切割)。如果sep为空字符,Split会将s切分成每一个unicode码值一个字符串。

    var s7 string = "hello,world"
	split := strings.Split(s7, ",")
	fmt.Println(split) // [hello world]

大小写转换

大写转小写 ToLower
小写转大写 ToUpper

    fmt.Println(strings.ToLower("GO")) // go
	fmt.Println(strings.ToUpper("go")) // GO

去除左右空格 TrimSpace

    s8 := strings.TrimSpace("hello ")
	fmt.Printf("%q\n",s8) // "hello"

去除左右两端指定的字符 strings.Trim

func Trim(s string, cutset string) string
返回将s前后端所有cutset包含的utf-8码值都去掉的字符串。

    s9 := strings.Trim(" ! hello world   123  !", "! ")// 去除左右两端的 空格 和 !
	fmt.Printf("s9 = %q\n",s9) //s9 = "hello world   123"
	
    s10 := strings.Trim("你好鸭你", "你")
	fmt.Printf("s10 = %q\n",s10) //s10 = "好鸭"

去除左端指定字符 strings.TrimLeft

    s11 := strings.TrimLeft("你好鸭你", "你")
	fmt.Printf("s11 = %q\n",s11)  //  s11 = "好鸭你"

去除右端指定字符 strings.TrimRight

    s12 := strings.TrimRight("你好鸭你", "你")
	fmt.Printf("s12 = %q\n",s12) // s12 = "你好鸭"

字符串是否已指定字符串开头 strings.HasPrefix

    b := strings.HasPrefix("https://www.baidu.com","https")
	fmt.Printf("b = %v\n",b) // b = true

	b2 := strings.HasPrefix("ftp://192.168.0.12","https")
	fmt.Printf("b2 = %v\n",b2) // b2 = false

字符串是否以指定字符串结尾 strings.HasSuffix

    b3 := strings.HasSuffix("main.go","go")
	fmt.Printf("b3 = %v\n",b3) // b3 = true

	b4 := strings.HasSuffix("main.java","go")
	fmt.Printf("b4 = %v\n",b4) // b4 = false
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值