99re热这里只有精品视频,7777色鬼xxxx欧美色妇,国产成人精品一区二三区在线观看,内射爽无广熟女亚洲,精品人妻av一区二区三区

Go 字符串處理

2022-05-13 17:43 更新

字符串在我們平常的Web開(kāi)發(fā)中經(jīng)常用到,包括用戶(hù)的輸入,數(shù)據(jù)庫(kù)讀取的數(shù)據(jù)等,我們經(jīng)常需要對(duì)字符串進(jìn)行分割、連接、轉(zhuǎn)換等操作,本小節(jié)將通過(guò)Go標(biāo)準(zhǔn)庫(kù)中的strings和strconv兩個(gè)包中的函數(shù)來(lái)講解如何進(jìn)行有效快速的操作。

字符串操作

下面這些函數(shù)來(lái)自于strings包,這里介紹一些我平常經(jīng)常用到的函數(shù),更詳細(xì)的請(qǐng)參考官方的文檔。

  • func Contains(s, substr string) bool

    字符串s中是否包含substr,返回bool值

    fmt.Println(strings.Contains("seafood", "foo"))
    fmt.Println(strings.Contains("seafood", "bar"))
    fmt.Println(strings.Contains("seafood", ""))
    fmt.Println(strings.Contains("", ""))
    //Output:
    //true
    //false
    //true
    //true
  • func Join(a []string, sep string) string

    字符串鏈接,把slice a通過(guò)sep鏈接起來(lái)

    s := []string{"foo", "bar", "baz"}
    fmt.Println(strings.Join(s, ", "))
    //Output:foo, bar, baz      
  • func Index(s, sep string) int

    在字符串s中查找sep所在的位置,返回位置值,找不到返回-1

    fmt.Println(strings.Index("chicken", "ken"))
    fmt.Println(strings.Index("chicken", "dmr"))
    //Output:4
    //-1
  • func Repeat(s string, count int) string

    重復(fù)s字符串count次,最后返回重復(fù)的字符串

    fmt.Println("ba" + strings.Repeat("na", 2))
    //Output:banana
  • func Replace(s, old, new string, n int) string

    在s字符串中,把old字符串替換為new字符串,n表示替換的次數(shù),小于0表示全部替換

    fmt.Println(strings.Replace("oink oink oink", "k", "ky", 2))
    fmt.Println(strings.Replace("oink oink oink", "oink", "moo", -1))
    //Output:oinky oinky oink
    //moo moo moo
  • func Split(s, sep string) []string

    把s字符串按照sep分割,返回slice

    fmt.Printf("%q\n", strings.Split("a,b,c", ","))
    fmt.Printf("%q\n", strings.Split("a man a plan a canal panama", "a "))
    fmt.Printf("%q\n", strings.Split(" xyz ", ""))
    fmt.Printf("%q\n", strings.Split("", "Bernardo O'Higgins"))
    //Output:["a" "b" "c"]
    //["" "man " "plan " "canal panama"]
    //[" " "x" "y" "z" " "]
    //[""]
  • func Trim(s string, cutset string) string

    在s字符串的頭部和尾部去除cutset指定的字符串

    fmt.Printf("[%q]", strings.Trim(" !!! Achtung !!! ", "! "))
    //Output:["Achtung"]
  • func Fields(s string) []string

    去除s字符串的空格符,并且按照空格分割返回slice

    fmt.Printf("Fields are: %q", strings.Fields("  foo bar  baz   "))
    //Output:Fields are: ["foo" "bar" "baz"]

字符串轉(zhuǎn)換

字符串轉(zhuǎn)化的函數(shù)在strconv中,如下也只是列表一些常用的:

  • Append 系列函數(shù)將整數(shù)等轉(zhuǎn)換為字符串后,添加到現(xiàn)有的字節(jié)數(shù)組中。

    package main
    
    import (
        "fmt"
        "strconv"
    )
    
    func main() {
        str := make([]byte, 0, 100)
        str = strconv.AppendInt(str, 4567, 10)
        str = strconv.AppendBool(str, false)
        str = strconv.AppendQuote(str, "abcdefg")
        str = strconv.AppendQuoteRune(str, '單')
        fmt.Println(string(str))
    }
  • Format 系列函數(shù)把其他類(lèi)型的轉(zhuǎn)換為字符串

    package main
    
    import (
        "fmt"
        "strconv"
    )
    
    func main() {
        a := strconv.FormatBool(false)
        b := strconv.FormatFloat(123.23, 'g', 12, 64)
        c := strconv.FormatInt(1234, 10)
        d := strconv.FormatUint(12345, 10)
        e := strconv.Itoa(1023)
        fmt.Println(a, b, c, d, e)
    }
  • Parse 系列函數(shù)把字符串轉(zhuǎn)換為其他類(lèi)型

    package main
    
    import (
        "fmt"
        "strconv"
    )
    func checkError(e error){
        if e != nil{
            fmt.Println(e)
        }
    }
    func main() {
        a, err := strconv.ParseBool("false")
        checkError(err)
        b, err := strconv.ParseFloat("123.23", 64)
        checkError(err)
        c, err := strconv.ParseInt("1234", 10, 64)
        checkError(err)
        d, err := strconv.ParseUint("12345", 10, 64)
        checkError(err)
        e, err := strconv.Atoi("1023")
        checkError(err)
        fmt.Println(a, b, c, d, e)
    }
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)