GO string related functions

main Package 

Import ( 
    " FMT " 
    " strings " 
    " StrConv " 
) 

FUNC main () { 
    str: = " Hello Americans "  // Chinese occupy three bytes 
    fmt.Println ( " length of str = len " , len ( STR)) // length. 11 

    for I: = 0 ; I <len (STR); I ++ { 
        fmt.Printf ( " characters% C = \ n- " , STR [I]) // Chinese garbled 
    } 

    RSTR: = [] rune (str) // become a rune slice can be solved Chinese
    fmt.Println ( " length of RSTR = len " , len (RSTR)) // length. 7 
    for I: = 0 ; I <len (RSTR); I ++ { 
        fmt.Printf ( " characters% C = \ n- " , RSTR [I]) // Chinese normal display 
    } 

    // string to integer 
    NSTR, ERR: = strconv.Atoi ( " 123 " ) 
    fmt.Printf ( " NSTR =% V, V ERR =% \ n- " , NSTR , ERR) 

    // can check whether a string of numbers is 
    nstr2, ERR2: = strconv.Atoi ( " abc " )
     iF nil =! ERR2 { 
        fmt.Printf (" Conversion failed V% \ n- ' , ERR2) 
    } the else { 
        fmt.Printf ( " successful conversion V% \ n- ' , nstr2) 
    } 

    // Integer String 
    atostr: = strconv.Itoa ( 12345 ) 
    fmt.Printf ( " V =% NSTR, T ERR =% \ n- " , atostr, atostr) // 12345 string 

    // string rotation [] byte (string) ascii code 
    by: = [] byte ( " Hello, ABC " ) 
    fmt.Println ( " byte = " , by); // each character ascii code 

    //[] byte {11,12,13} to String String ([] {11, 12} byte) 
    bystr: = String ([] byte { 104 , 101 , 108 , 108 , 111 , 44 is , 97 , 98 , 99 }) 
    fmt.Println ( " bystr = " , bystr); // Hello, ABC 

    // decimal converted into a prescribed binary (8, 16) 
    num2: strconv.FormatInt = ( 123 , 2 ) // turn into 2 ary 
    fmt.Println ( " num2 = " , num2); 

    Num8: = strconv.FormatInt ( 123 , 8 ) // turn into octal 
    fmt.Println ( " num2 = " , Num8); 

    // determines whether there is a character string in another string 
    b: = strings.Contains ( " abczxxxxxx " , " Y " ) // returns bool type 
    fmt.Println ( " B = " , B); 

    // a string appears in another available frequency and a string 
    CT: = strings.Count ( " Wahaha " , " HA " ) 
    fmt.Println ( " CT =" , CT); 

    // determines whether the two strings are equal == case sensitive, use strings.EqualFold () does not distinguish 
    SE: = " ABC " == " Abc " 
    SEN: = strings.EqualFold ( " ABC " , " Abc " ) 
    fmt.Println ( " SE = " , SE); // to false 
    fmt.Println ( " SEN = " , SEN); // to true 

    // string is first found to give a string of 1 in position non-existent 
    index: = strings.Index ( " Wahaha " , "h ") //2
    INDEX_1: = strings.Index ( " Wahaha " , " Z " ) // -1 
    fmt.Println ( " index = " , index); // to false 
    fmt.Println ( " INDEX_1 = " , INDEX_1); // to true 

    / / last occurrence of available positions -1 absent 
    the lastIndex: = strings.LastIndex ( " Wahaha " , " HA " ) 
    fmt.Println ( " the lastIndex = " , the lastIndex); // . 4 

    // string replace 
    originstr: = " Wahaha " 
    for newstr: = strings.Replace (originstr, " HA " , " WA " , . 1 ) // . 1 is an alternative, two alternative 2, -1 replace all 
    fmt.Println ( " for newstr = " , for newstr); // wawaha 
    
    // to cut a string array 
    arrstr: = " Ni, HEN, NIU, B " 
    ARR: = strings.Split (arrstr, " , " )
     for I: = 0 ; I < len (ARR); I ++ { 
        FMT.Println(arr[i])
    } 
    Fmt.Printf ( " ARR =% V, type T% \ n- " , ARR, ARR); // wawaha 

    // string conversion sensitive 
    fmt.Println (strings.ToUpper ( " ABC " )) // the ABC 
    FMT .Println (strings.ToLower ( " AbC " )) // ABC 

    // remove space around a string 
    fmt.Printf ( " % Q \ n- " , strings.TrimSpace ( "   XYZ    " )); // "XYZ"% Q He added "" 

    // specify to remove both sides of a character TrimLeft, TrimRight remove left or right
    fmt.Println (strings.Trim ( " !! wahah ahahah !!! " , " ! " )) // wahah ahahah middle will not get rid of! 

    // whether a beginning of the string (HasPrefix); or the end (HasSuffix ) 
    fmt.Println (strings.HasPrefix ( " http://www.wahaha.com " , " HTTP " )) // to true 
    fmt.Println (strings.HasSuffix ( " http://www.wahaha.com/test. AVI " , " AVI " )) // to true 
}

 

Guess you like

Origin www.cnblogs.com/zhangzhiping35/p/11100856.html