Commonly used functions and string interception

  • You may be used len (string variables) Get the byte length of the string, wherein a length of 1 byte in English, Chinese occupies 3 bytes in length

  • You can use the variable name [n] obtained string of byte n + 1, returns the Unicode code values (uint8 type) corresponding to this byte. Note that n is the range [0, length)

  • You can use the variable name [n: m] is greater than or equal to n character sequence extracted smaller than m

    • n and m may be omitted, omit that when n is 0, m is the length

    • Because the Chinese occupation of three bytes, if not removed intact the Chinese, will be garbled

  • Can be obtained by slicing the string into a length, and which acquires content may be directly used for binding loop acquiring range
main FUNC () { 
    S: = " smallming Zhang " 
    A: = S [ 0 ] 
    fmt.Println (A)         // Output: 115 
    fmt.Printf ( " % T \ n- " , A) // output uint8 
    B: = fmt.Sprintf ( " % C " , A) 
    fmt.Printf ( " % T \ n- " , B) // output: String 
    fmt.Println (B)         // output S 
        fmt.Println (len (S)) // output: 12, the byte length 
    fmt.Println (S [ . 1 : . 4 ]) //Output: Mal 
    fmt.Println (S [: 2 ])   // Output: SM 
    fmt.Println (S [ . 5 :])   // Output: ming sheets 

        S1: = [] Rune (S) 
    fmt.Println (len (S1 ))     // output: 10 
    fmt.Println (S1 [ . 9 ])       // output 24352 
    fmt.Printf ( " % C " , S1 [ . 9 ]) // output: Double 

    // traversal string content 
    for I, n- : = Range S { 
        fmt.Println (I, n-) 
    }           
}    

Common Functions

  • Strings in the string provides a common function of the package

  • Commonly used functions are summarized as follows

main Package 

Import ( 
    " FMT " 
    " strings " 
) 

FUNC main () { 
    S: = " smallming " 
    // first time index 
    fmt.Println (strings.Index (S, " L " ))
     // last occurrence the index 
    fmt.Println (strings.LastIndex (S, " L " ))
     // whether to specify the content beginning 
    fmt.Println (strings.HasPrefix (S, " Small " ))
     // if end with the specified content 
    fmt.Println ( strings.HasSuffix (S, "ming"))
     // if the specified character string 
    fmt.Println (strings.Contains (S, " mi The " ))
     // whole becomes lowercase 
    fmt.Println (strings.ToLower (S))
     // all uppercase 
    fmt.Println (strings. the ToUpper (S))
     // replace the old string in the first n new string into sub-strings, if n is less than 0 represents the total replacement.
     @ If the number n is greater than said old replace all 
    fmt.Println (strings. the Replace (S, " m " , " K " , - . 1 ))
     // string count times 
    fmt.Println (strings.Repeat (S, 2 ))
     // before and after the specified character string is removed 
    fmt.Println (strings .Trim (s,"  " )) // to spaces may be used strings.TrimSpace (S)
     // specified to split the character string into a slice 
    fmt.Println (strings.Split (S, " m " ))
     // Use the specified delimiter incorporated into the string sections 
    ARR: = [] string { " Small " , " Ming " } 
    fmt.Println (strings.Join (ARR, "" )) 
}

Guess you like

Origin www.cnblogs.com/miaoweiye/p/12085055.html