golang string common system functions

1. The length of the string statistics, bytes len (str)

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

2. String traversal problem while processing a Chinese r: = [] rune (str)

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

3. String to Integer: n, err: = strconv.Atoi ( "12")

   n-, ERR: = strconv.Atoi ( " 5656 " )
     IF ERR =! nil { 
        fmt.Println ( " conversion errors " , ERR) 
    } the else { 
        fmt.Println ( " turn into a result " , n-) 
    }

4. Integer String str = strconv.Itoa (12345)

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

The string rotation [] byte: var bytes = [] byte ( "hello go")

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

6 [] byte string transfer:. Str = string ([] byte {97, 98, 99})

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

7.10, 8, 16 turn hex hex: str = strconv.FormatInt (123, 2)

    STR: strconv.FormatInt = (123, 2 ) 
    fmt.Printf ( " 123% corresponding to binary is V = \ n- " , STR) 
    STR = strconv.FormatInt (123, 16 ) 
    fmt.Printf ( " 16 corresponding to the intake 123 % V system is = \ n- " , STR)

8. Find substring in the specified string: strings.Contains ( "seafood", "foo")

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

There are several statistical 9. A string specified substring: strings.Count ( "ceheese", "e")

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

10. The case-insensitive string comparison (== is case sensitive): fmt.Println (strings.EqualFold ( "abc", "Abc"))

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

11. Return substring index value for the first occurrence of the string, if none -1: strings.Index ( "NLT_abc", "abc")

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

12. Return the last substring in the string that appears in the index, if not returned -1: strings.LastIndex ( "go golang", "go")

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

13. The specified substring replace another substring: strings.Replace ( "go lgo hello", "go", "python", n) n can specify that you want to replace some, if n = -1 represent all replace

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

14. a character specified for the segmentation identification, the string will be split into a string array, 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. The case-letter string conversion: strings.ToLower ( "Go") // strings.ToUpper ( "Go")

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

16. The string of right and left spaces removed: strings.TrimSpace ( "tn a long gopher ntrn")

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

17. The left and right sides of the specified character string is removed: strings.Trim ( "hello world!!", "!") // strings.TrimLeft // strings.TrimRight

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

18. determines whether the string starts with the specified string: strings.HasPrefix ( "ftp://192.168.10.1", "ftp")

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

19 determines whether the end of the string specified string: strings.HasSuffix ( "NLT_abc.jpg", "abc")

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

 

 

Guess you like

Origin www.cnblogs.com/xiangxiaolin/p/11795896.html