golang字符串常用的系统函数

wklken的笔记 2019-11-05

1.统计字符串的长度,按字节len(str)

  str := "hello北京"
  fmt.Println("str len=", len(str))

2.字符串遍历,同时处理有中文的问题 r := []rune(str)

str2 := "hello北京"
    r := []rune(str2)
    for i := 1; i < len(r); i++ {
        fmt.Printf("字符=%c\n", r[i])
    }

3.字符串转整数: n, err := strconv.Atoi("12")

   n, err := strconv.Atoi("5656")
    if err != nil{
        fmt.Println("转换错误", err)
    }else {
        fmt.Println("转成的结果是", n)
    }

4.整数转字符串 str = strconv.Itoa(12345)

str = strconv.Itoa(123456)
    fmt.Printf("str=%v, str=%T", str, str)

5.字符串转 []byte: var bytes = []byte("hello go")

var bytes = []byte("hello go")
    fmt.Printf("bytes=%v\n", bytes)

6.[]byte 转字符串: str = string([]byte{97, 98, 99})

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

7.10进制转2,8,16进制: str = strconv.FormatInt(123, 2)

str := strconv.FormatInt(123, 2)
    fmt.Printf("123对应的二进制是=%v\n", str)
    str = strconv.FormatInt(123, 16)
    fmt.Printf("123对应的16进制是=%v\n", str)

8.查找子串是否在指定的字符串中:strings.Contains("seafood", "foo")

b := strings.Contains("seafood", "abc")
    fmt.Printf("b=%v\n", b)

9.统计一个字符串有几个指定的子串: strings.Count("ceheese", "e")

num := strings.Count("ceheese", "e")
    fmt.Printf("num=%v\n", num)

10.不区分大小写的字符串比较(==是区分字母大小写的):fmt.Println(strings.EqualFold("abc", "Abc"))

b = strings.EqualFold("abc", "Abc")
    fmt.Printf("b=%v\n", b)

11.返回子串在字符串第一次出现的index值,如果没有返回-1:strings.Index("NLT_abc", "abc")

  index = strings.Index("NLT_abcabcabc", "abc")  fmt.Printf("index=%v\n", index)

12.返回子串在字符串最后一次出现的index,如果没有返回-1:strings.LastIndex("go golang", "go")

index := strings.LastIndex("go golang", "go")
    fmt.Printf("index=%v\n", index)

13.将指定的子串替换成另外一个子串:strings.Replace("go lgo hello", "go",  "python", n)n可以指定你希望替换几个,如果n= -1表示全部替换

str2 := "go go hello"
    str := strings.Replace(str2, "go", "python", -1)
    fmt.Printf("str=%v str2=%v\n", str, str2)

14.按照指定的某个字符,为分割标识,将一个字符串拆分成字符串数组,strings.Split("hello, wrold, ok", ",")

strArr := strings.Split("hello,world,ok", ",")
    for i := 0; i < len(strArr); i++ {
        fmt.Printf("str[%v]=%v\n", i, strArr[i])
    }
    fmt.Printf("strArr=%v\n", strArr)

15.将字符串的字母进行大小写的转换:strings.ToLower("Go") // strings.ToUpper("Go")

str := "goLange Hello"
    //str = strings.ToLower(str)
    str = strings.ToUpper(str)
    fmt.Printf("str=%v\n", str)

16.将字符串左右两边的空格去掉:strings.TrimSpace("        tn  a long gopher ntrn           ")

str = strings.TrimSpace("        tn  a long gopher ntrn           ")
    fmt.Printf("str=%v\n", str)

17.将字符串左右两边指定的字符去掉:strings.Trim("!hello world!", "!") // strings.TrimLeft //  strings.TrimRight

str = strings.Trim("! he!llo!", " !") 
    fmt.Printf("str=%q\n", str)

18.判断字符串是否以指定的字符串开头:strings.HasPrefix("ftp://192.168.10.1", "ftp")

b := strings.HasPrefix("ftp://192.168.10.1", "ftp")
    fmt.Printf("b=%v\n", b)

19.判断字符串是否以指定的字符串结束:strings.HasSuffix("NLT_abc.jpg", "abc")

b = strings.HasSuffix("NLT_abcd", "abc")
    fmt.Printf("b=%v\n",b)

相关推荐