<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                字符串在我們平常的Web開發中經常用到,包括用戶的輸入,數據庫讀取的數據等,我們經常需要對字符串進行分割、連接、轉換等操作,本小節將通過Go標準庫中的strings和strconv兩個包中的函數來講解如何進行有效快速的操作。 ## [](https://github.com/astaxie/build-web-application-with-golang/blob/master/zh/07.6.md#字符串操作)字符串操作 下面這些函數來自于strings包,這里介紹一些我平常經常用到的函數,更詳細的請參考官方的文檔。 * 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通過sep鏈接起來 ~~~ 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 重復s字符串count次,最后返回重復的字符串 ~~~ fmt.Println("ba" + strings.Repeat("na", 2)) //Output:banana ~~~ * func Replace(s, old, new string, n int) string 在s字符串中,把old字符串替換為new字符串,n表示替換的次數,小于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"] ~~~ ## [](https://github.com/astaxie/build-web-application-with-golang/blob/master/zh/07.6.md#字符串轉換)字符串轉換 字符串轉化的函數在strconv中,如下也只是列表一些常用的: * Append 系列函數將整數等轉換為字符串后,添加到現有的字節數組中。 ~~~ 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 系列函數把其他類型的轉換為字符串 ~~~ 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 系列函數把字符串轉換為其他類型 ~~~ 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) } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看