GO--字符串(常用字符串函数)

GO–字符串(常用字符串函数)

  1. len(str):用于统计字符串的长度

    1. package main
      
      import (
      	"fmt"
      )
      
      func main() {
      	// 用于统计字符串的长度,按字节统计len(str)
      	// golang的编码统一为utf-8 (字母和数字占用一个字节,汉子占用三个字节)
      	str := "hello"
      	fmt.Println("str=", len(str))//5
      }
      
  2. r := []rune(str):用于处理字符串遍历时出现中文的问题

    1. package main
      
      import (
      	"fmt"
      )
      
      func main() {
      	str2 := "hello北京"
      	// 字符串遍历,同时处理有中文的问题 r := []rune(str)
      	r := []rune(str2)
      	for i := 0; i < len(r); i++ {
      		fmt.Printf("字符=%c\n", r[i])
      	}
      }
      
  3. 字符串转整数:n, err := strconv.Atoi(“12”)

    1. package main
      
      import (
      	"fmt"
      )
      
      func main() {
      // 字符串转整数: n, err := strconv.Atoi("12")
      	// 此处接收到的数据为数字时,转换正常,当包含有非数字时将出现错误提示
      	n, err := strconv.Atoi("123")
      	if err != nil {
      		fmt.Println("转换错误", err)
      	} else {
      		fmt.Println("转成的结果是:", n)
      	}
      }
      
  4. 整数转字符串:str = strconv.Itoa(12345)

    1. package main
      
      import (
      	"fmt"
      )
      
      func main() {
      // 整数转字符串 str= strconv.Itoa(1234)
      	str = strconv.Itoa(123)
      	fmt.Printf("str=%v,str=%T", str, str)//str=123,str=string
      }
      
  5. 字符串转 []byte切片:var bytes = []byte(“hello go”)

    1. var bytes = []byte("hello go")
      	fmt.Printf("bytes=%v\n", bytes)//[104 101 108 108 111 32 103 111]
      
  6. []byte 转字符串:str = string([]byte{97,98,99})

    1. var strr = string([]byte{97, 98, 99})
      	fmt.Printf("strr=%v\n", strr)//strr=abc
      
  7. 10进制转 2,8, 16进制:str = strconv.FormatInt(123, 2)

    1. 	str = strconv.FormatInt(123, 2)
      	fmt.Printf("123对应的2进制是%v\n", str)//123对应的2进制是1111011
      	str = strconv.FormatInt(123, 16)
      	fmt.Printf("123对应的16进制是%v\n", str)//123对应的2进制是1111011
      
  8. 查找子串是否在指定的字符串中:strings.Contains(“seafood”, “foo”)

    1. 	b := strings.Contains("seafood", "foo")
      	fmt.Printf("b=%v\n", b)//b=true
      
  9. 统计一个字符串有几个指定的子串:strings.Count(“ceheese”, “e”)

    1. 	num=1num := strings.Count("ceheese", "se")
      	fmt.Printf("num=%v\n", num)//num=1
      
  10. 不区分大小写的字符串比较(==是区分字母大小写的):fint.Println(strings.EqualFold(“abc”,“ABC”))

    1. 	b = strings.EqualFold("abc", "ABC")
      	fmt.Printf("b=%v\n", b)//b=true
      
  11. 返回子串在字符串第一次出现的index值, 如果没有返回-1:strings.Index(“124dsg”,“d”)

    1. 	index := strings.Index("asfdjalk", "d")
      	fmt.Printf("index=%v\n", index)//index=3
      
  12. 返回子串在字符串最后一次出现的index值, 如果没有返回-1:strings.LastIndex(“124dsgdrdrd”,“d”)

    1. 	index11 := strings.LastIndex("124dsgdrdrd", "d")
      	fmt.Printf("index11=%v\n", index11) //index11=10
      
  13. 将指定的子串替换成 另一个子串: strings.Replace(“go go hello”, “go”, “go语言”,n):n可以指定用户希望替换几个,如果n= -1 将表示全部替换

    1. 	index223 := strings.Replace("go go hello", "go", "go语言", 1)
      	fmt.Printf("index223=%v\n", index223)//index223=go语言 go hello
      
  14. 按照指定的某个字符,为分割标识,将一个字符串拆分成字符串数组:strings.Split(“hello,world,ok,no,yes”,",")

    1. 	strArr := strings.Split("hello,world,ok,no,yes", ",")
      	fmt.Printf("strArr=%v\n", strArr)//strArr=[hello world ok no yes]
      
  15. 将字符串的字母进行大小写的转换:strings.ToLower(“Go”)

    1. 	str = strings.ToLower("Go")
      	fmt.Printf("str=%v\n", str)//str=go
      
  16. 将字符串左右两边的空格去除:strings.TrimSpace(" tn a lone good noe ")

    1. 	str123 := strings.TrimSpace("  tn  a  lone  good  noe  ")
      	fmt.Printf("str123=%v\n", str123)//str123=tn  a  lone  good  noe
      
  17. 将字符串左右两边指定的字符去除:strings.Trim("! tn a lone good noe! “,”!")

    1. 	str234 := strings.Trim("!  tn  a  lone  good  noe!  ", "!")
      	fmt.Printf("str234=%v\n", str234) //str234=  tn  a  lone  good  noe! 
      
  18. 将字符串左右两边指定的字符去除:strings.TrimLeft("! tn a lone good noe!!","!")

    1. 	str345 := strings.TrimLeft("!  tn  a  lone  good  noe!!", "!")
      	fmt.Printf("str345=%v\n", str345)//str345=  tn  a  lone  good  noe!!
      
  19. 将字符串左右两边指定的字符去除:strings.TrimRight("! tn a lone good noe!!","!")

    1. 	str456 := strings.TrimRight("!  tn  a  lone  good  noe!!", "!")
      	fmt.Printf("str456=%v\n", str456)//str456=!  tn  a  lone  good  noe
      
  20. 判断字符串是否以指定的字符串开头:strings.HasPrefix(“asdfjalkf”,“as”)

    1. 	str44 := strings.HasPrefix("asdfjalkf", "as")
      	fmt.Printf("str44=%v\n", str44)//str44=true
      
  21. 判断字符串是否以指定的字符串结束:strings.HasSuffix(“asdfjalkf”,“as”)

    1. 	str45 := strings.HasSuffix("asdfjalkf", "as")
      	fmt.Printf("str45=%v\n", str45)//str45=false
      
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Go 语言中,可以使用 strings 包提供的函数来解析字符串。以下是一些常用函数: 1. strings.Split(s, sep):将字符串 s 按照分隔符 sep 进行分割,返回一个字符串数组。 2. strings.Trim(s, cutset):将字符串 s 去掉 cutset 中指定的字符。 3. strings.Replace(s, old, new, n):将字符串 s 中的 old 替换为 new,n 指定替换的次数,-1 表示全部替换。 4. strings.HasPrefix(s, prefix):判断字符串 s 是否以 prefix 开头。 5. strings.HasSuffix(s, suffix):判断字符串 s 是否以 suffix 结尾。 6. strings.Index(s, substr):查找字符串 s 中 substr 的位置,返回第一个匹配的位置,如果没有匹配则返回 -1。 7. strings.LastIndex(s, substr):查找字符串 s 中 substr 的位置,返回最后一个匹配的位置,如果没有匹配则返回 -1。 示例代码: ```go package main import ( "fmt" "strings" ) func main() { // 分割字符串 str := "hello,world" arr := strings.Split(str, ",") fmt.Println(arr) // [hello world] // 去掉指定字符 str2 := "hello,world\n" str2 = strings.Trim(str2, "\n") fmt.Println(str2) // hello,world // 替换字符串 str3 := "hello,world" str3 = strings.Replace(str3, "world", "golang", -1) fmt.Println(str3) // hello,golang // 判断字符串是否以指定前缀开头 str4 := "hello,world" if strings.HasPrefix(str4, "hello") { fmt.Println("str4 starts with hello") } // 判断字符串是否以指定后缀结尾 str5 := "hello,world" if strings.HasSuffix(str5, "world") { fmt.Println("str5 ends with world") } // 查找字符串中指定子串的位置 str6 := "hello,world" index := strings.Index(str6, "world") fmt.Println(index) // 6 } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值