<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智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 4.7 strings 和 strconv 包 - [\[\](https://github.com/Unknwon/the-way-to-go\_ZH\_CN/blob/master/eBook/04.7.md#471-前綴和后綴)4.7.1 前綴和后綴](#httpsgithubcomUnknwonthewaytogo_ZH_CNblobmastereBook047md471471__1) - [\[\](https://github.com/Unknwon/the-way-to-go\_ZH\_CN/blob/master/eBook/04.7.md#472-字符串包含關系)4.7.2 字符串包含關系](#httpsgithubcomUnknwonthewaytogo_ZH_CNblobmastereBook047md472472__2) - [\[\](https://github.com/Unknwon/the-way-to-go\_ZH\_CN/blob/master/eBook/04.7.md#473-判斷子字符串或字符在父字符串中出現的位置索引)4.7.3 判斷子字符串或字符在父字符串中出現的位置(索引)](#httpsgithubcomUnknwonthewaytogo_ZH_CNblobmastereBook047md473473__3) - [\[\](https://github.com/Unknwon/the-way-to-go\_ZH\_CN/blob/master/eBook/04.7.md#474-字符串替換)4.7.4 字符串替換](#httpsgithubcomUnknwonthewaytogo_ZH_CNblobmastereBook047md474474__4) - [\[\](https://github.com/Unknwon/the-way-to-go\_ZH\_CN/blob/master/eBook/04.7.md#475-統計字符串出現次數)4.7.5 統計字符串出現次數](#httpsgithubcomUnknwonthewaytogo_ZH_CNblobmastereBook047md475475__5) - [\[\](https://github.com/Unknwon/the-way-to-go\_ZH\_CN/blob/master/eBook/04.7.md#476-重復字符串)4.7.6 重復字符串](#httpsgithubcomUnknwonthewaytogo_ZH_CNblobmastereBook047md476476__6) - [\[\](https://github.com/Unknwon/the-way-to-go\_ZH\_CN/blob/master/eBook/04.7.md#477-修改字符串大小寫)4.7.7 修改字符串大小寫](#httpsgithubcomUnknwonthewaytogo_ZH_CNblobmastereBook047md477477__7) - [\[\](https://github.com/Unknwon/the-way-to-go\_ZH\_CN/blob/master/eBook/04.7.md#478-修剪字符串)4.7.8 修剪字符串](#httpsgithubcomUnknwonthewaytogo_ZH_CNblobmastereBook047md478478__8) - [\[\](https://github.com/Unknwon/the-way-to-go\_ZH\_CN/blob/master/eBook/04.7.md#479-分割字符串)4.7.9 分割字符串](#httpsgithubcomUnknwonthewaytogo_ZH_CNblobmastereBook047md479479__9) - [\[\](https://github.com/Unknwon/the-way-to-go\_ZH\_CN/blob/master/eBook/04.7.md#4710-拼接-slice-到字符串)4.7.10 拼接 slice 到字符串](#httpsgithubcomUnknwonthewaytogo_ZH_CNblobmastereBook047md4710slice4710__slice__10) - [\[\](https://github.com/Unknwon/the-way-to-go\_ZH\_CN/blob/master/eBook/04.7.md#4711-從字符串中讀取內容)4.7.11 從字符串中讀取內容](#httpsgithubcomUnknwonthewaytogo_ZH_CNblobmastereBook047md47114711__11) - [\[\](https://github.com/Unknwon/the-way-to-go\_ZH\_CN/blob/master/eBook/04.7.md#4712-字符串與其它類型的轉換)4.7.12 字符串與其它類型的轉換](#httpsgithubcomUnknwonthewaytogo_ZH_CNblobmastereBook047md47124712__12) 作為一種基本數據結構,每種語言都有一些對于字符串的預定義處理函數。Go 中使用 `strings` 包來完成對字符串的主要操作。 ## 4.7.1 前綴和后綴 `HasPrefix` 判斷字符串 `s` 是否以 `prefix` 開頭: ``` strings.HasPrefix(s, prefix string) bool ``` `HasSuffix` 判斷字符串 `s` 是否以 `suffix` 結尾: ``` strings.HasSuffix(s, suffix string) bool ``` 示例 4.13 [presuffix.go](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/examples/chapter_4/presuffix.go) ``` package main import ( "fmt" "strings" ) func main() { var str string = "This is an example of a string" fmt.Printf("T/F? Does the string \"%s\" have prefix %s? ", str, "Th") fmt.Printf("%t\n", strings.HasPrefix(str, "Th")) } ``` 輸出: ``` T/F? Does the string "This is an example of a string" have prefix Th? true ``` 這個例子同樣演示了轉義字符 `\` 和格式化字符串的使用。 ## 4.7.2 字符串包含關系 `Contains` 判斷字符串 `s` 是否包含 `substr`: ``` strings.Contains(s, substr string) bool ``` ## 4.7.3 判斷子字符串或字符在父字符串中出現的位置(索引) `Index` 返回字符串 `str` 在字符串 `s` 中的索引(`str` 的第一個字符的索引),-1 表示字符串 `s` 不包含字符串 `str`: ``` strings.Index(s, str string) int ``` `LastIndex` 返回字符串 `str` 在字符串 `s` 中最后出現位置的索引(`str` 的第一個字符的索引),-1 表示字符串 `s` 不包含字符串 `str`: ``` strings.LastIndex(s, str string) int ``` 如果 `ch` 是非 ASCII 編碼的字符,建議使用以下函數來對字符進行定位: ``` strings.IndexRune(s string, ch int) int ``` 示例 4.14 [index\_in\_string.go](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/examples/chapter_4/index_in_string.go) ``` package main import ( "fmt" "strings" ) func main() { var str string = "Hi, I'm Marc, Hi." fmt.Printf("The position of \"Marc\" is: ") fmt.Printf("%d\n", strings.Index(str, "Marc")) fmt.Printf("The position of the first instance of \"Hi\" is: ") fmt.Printf("%d\n", strings.Index(str, "Hi")) fmt.Printf("The position of the last instance of \"Hi\" is: ") fmt.Printf("%d\n", strings.LastIndex(str, "Hi")) fmt.Printf("The position of \"Burger\" is: ") fmt.Printf("%d\n", strings.Index(str, "Burger")) } ``` 輸出: ``` The position of "Marc" is: 8 The position of the first instance of "Hi" is: 0 The position of the last instance of "Hi" is: 14 The position of "Burger" is: -1 ``` ## 4.7.4 字符串替換 `Replace` 用于將字符串 `str` 中的前 `n` 個字符串 `old` 替換為字符串 `new`,并返回一個新的字符串,如果 `n = -1` 則替換所有字符串 `old` 為字符串 `new`: ``` strings.Replace(str, old, new, n) string ``` ## 4.7.5 統計字符串出現次數 `Count` 用于計算字符串 `str` 在字符串 `s` 中出現的非重疊次數: ``` strings.Count(s, str string) int ``` 示例 4.15 [count\_substring.go](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/examples/chapter_4/count_substring.go) ``` package main import ( "fmt" "strings" ) func main() { var str string = "Hello, how is it going, Hugo?" var manyG = "gggggggggg" fmt.Printf("Number of H's in %s is: ", str) fmt.Printf("%d\n", strings.Count(str, "H")) fmt.Printf("Number of double g's in %s is: ", manyG) fmt.Printf("%d\n", strings.Count(manyG, "gg")) } ``` 輸出: ``` Number of H's in Hello, how is it going, Hugo? is: 2 Number of double g’s in gggggggggg is: 5 ``` ## 4.7.6 重復字符串 `Repeat` 用于重復 `count` 次字符串 `s` 并返回一個新的字符串: ``` strings.Repeat(s, count int) string ``` 示例 4.16 [repeat\_string.go](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/examples/chapter_4/repeat_string.go) ``` package main import ( "fmt" "strings" ) func main() { var origS string = "Hi there! " var newS string newS = strings.Repeat(origS, 3) fmt.Printf("The new repeated string is: %s\n", newS) } ``` 輸出: ``` The new repeated string is: Hi there! Hi there! Hi there! ``` ## 4.7.7 修改字符串大小寫 `ToLower` 將字符串中的 Unicode 字符全部轉換為相應的小寫字符: ``` strings.ToLower(s) string ``` `ToUpper` 將字符串中的 Unicode 字符全部轉換為相應的大寫字符: ``` strings.ToUpper(s) string ``` 示例 4.17 [toupper\_lower.go](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/examples/chapter_4/toupper_lower.go) ``` package main import ( "fmt" "strings" ) func main() { var orig string = "Hey, how are you George?" var lower string var upper string fmt.Printf("The original string is: %s\n", orig) lower = strings.ToLower(orig) fmt.Printf("The lowercase string is: %s\n", lower) upper = strings.ToUpper(orig) fmt.Printf("The uppercase string is: %s\n", upper) } ``` 輸出: ``` The original string is: Hey, how are you George? The lowercase string is: hey, how are you george? The uppercase string is: HEY, HOW ARE YOU GEORGE? ``` ## 4.7.8 修剪字符串 你可以使用 `strings.TrimSpace(s)` 來剔除字符串開頭和結尾的空白符號;如果你想要剔除指定字符,則可以使用`strings.Trim(s, "cut")` 來將開頭和結尾的 `cut` 去除掉。該函數的第二個參數可以包含任何字符,如果你只想剔除開頭或者結尾的字符串,則可以使用 `TrimLeft` 或者 `TrimRight` 來實現。 ## 4.7.9 分割字符串 `strings.Fields(s)` 將會利用 1 個或多個空白符號來作為動態長度的分隔符將字符串分割成若干小塊,并返回一個 slice,如果字符串只包含空白符號,則返回一個長度為 0 的 slice。 `strings.Split(s, sep)` 用于自定義分割符號來對指定字符串進行分割,同樣返回 slice。 因為這 2 個函數都會返回 slice,所以習慣使用 for-range 循環來對其進行處理(第 7.3 節)。 ## 4.7.10 拼接 slice 到字符串 `Join` 用于將元素類型為 string 的 slice 使用分割符號來拼接組成一個字符串: ``` Strings.Join(sl []string, sep string) ``` 示例 4.18 [strings\_splitjoin.go](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/examples/chapter_4/strings_splitjoin.go) ``` package main import ( "fmt" "strings" ) func main() { str := "The quick brown fox jumps over the lazy dog" sl := strings.Fields(str) fmt.Printf("Splitted in slice: %v\n", sl) for _, val := range sl { fmt.Printf("%s - ", val) } fmt.Println() str2 := "GO1|The ABC of Go|25" sl2 := strings.Split(str2, "|") fmt.Printf("Splitted in slice: %v\n", sl2) for _, val := range sl2 { fmt.Printf("%s - ", val) } fmt.Println() str3 := strings.Join(sl2,";") fmt.Printf("sl2 joined by ;: %s\n", str3) } ``` 輸出: ``` Splitted in slice: [The quick brown fox jumps over the lazy dog] The - quick - brown - fox - jumps - over - the - lazy - dog - Splitted in slice: [GO1 The ABC of Go 25] GO1 - The ABC of Go - 25 - sl2 joined by ;: GO1;The ABC of Go;25 ``` 其它有關字符串操作的文檔請參閱 [官方文檔](http://golang.org/pkg/strings/)( **譯者注:國內用戶可訪問 [該頁面](http://docs.studygolang.com/pkg/strings/)** )。 ## 4.7.11 從字符串中讀取內容 函數 `strings.NewReader(str)` 用于生成一個 `Reader` 并讀取字符串中的內容,然后返回指向該 `Reader` 的指針,從其它類型讀取內容的函數還有: - `Read()` 從 \[\]byte 中讀取內容。 - `ReadByte()` 和 `ReadRune()` 從字符串中讀取下一個 byte 或者 rune。 ## 4.7.12 字符串與其它類型的轉換 與字符串相關的類型轉換都是通過 `strconv` 包實現的。 該包包含了一些變量用于獲取程序運行的操作系統平臺下 int 類型所占的位數,如:`strconv.IntSize`。 任何類型 **T** 轉換為字符串總是成功的。 針對從數字類型轉換到字符串,Go 提供了以下函數: - `strconv.Itoa(i int) string` 返回數字 i 所表示的字符串類型的十進制數。 - `strconv.FormatFloat(f float64, fmt byte, prec int, bitSize int) string` 將 64 位浮點型的數字轉換為字符串,其中 `fmt` 表示格式(其值可以是 `'b'`、`'e'`、`'f'` 或 `'g'`),`prec` 表示精度,`bitSize` 則使用 32 表示 float32,用 64 表示 float64。 將字符串轉換為其它類型 **tp** 并不總是可能的,可能會在運行時拋出錯誤 `parsing "…": invalid argument`。 針對從字符串類型轉換為數字類型,Go 提供了以下函數: - `strconv.Atoi(s string) (i int, err error)` 將字符串轉換為 int 型。 - `strconv.ParseFloat(s string, bitSize int) (f float64, err error)` 將字符串轉換為 float64 型。 利用多返回值的特性,這些函數會返回 2 個值,第 1 個是轉換后的結果(如果轉換成功),第 2 個是可能出現的錯誤,因此,我們一般使用以下形式來進行從字符串到其它類型的轉換: ``` val, err = strconv.Atoi(s) ``` 在下面這個示例中,我們忽略可能出現的轉換錯誤: 示例 4.19 [string\_conversion.go](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/examples/chapter_4/string_conversion.go) ``` package main import ( "fmt" "strconv" ) func main() { var orig string = "666" var an int var newS string fmt.Printf("The size of ints is: %d\n", strconv.IntSize) an, _ = strconv.Atoi(orig) fmt.Printf("The integer is: %d\n", an) an = an + 5 newS = strconv.Itoa(an) fmt.Printf("The new string is: %s\n", newS) } ``` 輸出: ``` 64 位系統: The size of ints is: 64 32 位系統: The size of ints is: 32 The integer is: 666 The new string is: 671 ``` 在第 5.1 節,我們將會利用 if 語句來對可能出現的錯誤進行分類處理。 更多有關該包的討論,請參閱 [官方文檔](http://golang.org/pkg/strconv/)( **譯者注:國內用戶可訪問 [該頁面](http://docs.studygolang.com/pkg/strconv/)** )。
                  <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>

                              哎呀哎呀视频在线观看