<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>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                字符串是不可變值類型,內部用指針指向 UTF-8 字節數組。 ~~~ ? 默認值是空字符串 ""。 ? 用索引號訪問某字節,如 s[i]。 ? 不能用序號獲取字節元素指針,&s[i] 非法。 ? 不可變類型,無法修改字節數組。 ? 字節數組尾部不包含 NULL。 ~~~ 使用索引號訪問字符 (byte)。 ~~~ package main func main() { s := "abc" println(s[0] == '\x61', s[1] == 'b', s[2] == 0x63) } ~~~ 輸出結果: ~~~ true true true ~~~ 使用 " ` " 定義不做轉義處理的原始字符串,支持跨行。 ~~~ package main func main() { s := `a b\r\n\x00 c` println(s) } ~~~ 輸出結果: ~~~ a b\r\n\x00 c ~~~ 連接跨行字符串時,"+" 必須在上一行末尾,否則導致編譯錯誤。 ~~~ package main import ( "fmt" ) func main() { s := "Hello, " + "World!" // s2 := "Hello, " // +"World!" //./main.go:11:2: invalid operation: + untyped string fmt.Println(s) } ~~~ 支持用兩個索引號 ([]) 返回子串。 串依然指向原字節數組,僅修改了指針和 度屬性。 ~~~ package main import ( "fmt" ) func main() { s := "Hello, World!" s1 := s[:5] // Hello s2 := s[7:] // World! s3 := s[1:5] // ello fmt.Println(s, s1, s2, s3) } ~~~ 輸出結果: ~~~ Hello, World! Hello World! ello ~~~ 單引號字符常量表示 Unicode Code Point, 持 \uFFFF、\U7FFFFFFF、\xFF 格式。 對應 rune 類型,UCS-4。 ~~~ package main import ( "fmt" ) func main() { fmt.Printf("%T\n", 'a') var c1, c2 rune = '\u6211', '們' println(c1 == '我', string(c2) == "\xe4\xbb\xac") } ~~~ 輸出結果: ~~~ int32 // rune 是 int32 的別名 true true ~~~ 要修改字符串,可先將其轉換成 []rune 或 []byte,完成后再轉換為 string。無論哪種轉換,都會重新分配內存,并復制字節數組。 ~~~ package main func main() { s := "abcd" bs := []byte(s) bs[1] = 'B' println(string(bs)) u := "電腦" us := []rune(u) us[1] = '話' println(string(us)) } ~~~ 輸出結果: ~~~ aBcd 電話 ~~~ for 循環遍歷字符串時,也有 byte 和 rune 兩種方式。 ~~~ package main import ( "fmt" ) func main() { s := "abc漢字" for i := 0; i < len(s); i++ { // byte fmt.Printf("%c,", s[i]) } fmt.Println() for _, r := range s { // rune fmt.Printf("%c,", r) } fmt.Println() } ~~~ 輸出結果: ~~~ a,b,c,?,±,?,?,-,?, a,b,c,漢,字, ~~~ string的底層布局 ![](https://box.kancloud.cn/9145ac5758a792600481cd4a1a28e675_586x324.png) 字符串處理: 判斷是不是以某個字符串開頭 ~~~ package main import ( "fmt" "strings" ) func main() { str := "hello world" res0 := strings.HasPrefix(str, "http://") res1 := strings.HasPrefix(str, "hello") fmt.Printf("res0 is %v\n", res0) fmt.Printf("res1 is %v\n", res1) } ~~~ 輸出結果: ~~~ res0 is false res1 is true ~~~ 判斷是不是以某個字符串結尾 ~~~ package main import ( "fmt" "strings" ) func main() { str := "hello world" res0 := strings.HasSuffix(str, "http://") res1 := strings.HasSuffix(str, "world") fmt.Printf("res0 is %v\n", res0) fmt.Printf("res1 is %v\n", res1) } ~~~ 輸出結果: ~~~ res0 is false res1 is true ~~~ 判斷str在s中首次出現的位置,如果沒有返回-1 ~~~ package main import ( "fmt" "strings" ) func main() { str := "hello world" res0 := strings.Index(str, "o") res1 := strings.Index(str, "i") fmt.Printf("res0 is %v\n", res0) fmt.Printf("res1 is %v\n", res1) } ~~~ 輸出結果: ~~~ res0 is 4 res1 is -1 ~~~ 判斷str在s中最后一次出現的位置,如果沒有返回-1 ~~~ package main import ( "fmt" "strings" ) func main() { str := "hello world" res0 := strings.LastIndex(str, "o") res1 := strings.LastIndex(str, "i") fmt.Printf("res0 is %v\n", res0) fmt.Printf("res1 is %v\n", res1) } ~~~ 輸出結果: ~~~ res0 is 7 res1 is -1 ~~~ 字符串替換 ~~~ package main import ( "fmt" "strings" ) func main() { str := "hello world world" res0 := strings.Replace(str, "world", "golang", 2) res1 := strings.Replace(str, "world", "golang", 1) //trings.Replace("原字符串", "被替換的內容", "替換的內容", 替換次數) fmt.Printf("res0 is %v\n", res0) fmt.Printf("res1 is %v\n", res1) } ~~~ 輸出結果: ~~~ res0 is hello golang golang res1 is hello golang world ~~~ 求str含s的次數 ~~~ package main import ( "fmt" "strings" ) func main() { str := "hello world world" countTime0 := strings.Count(str, "o") countTime1 := strings.Count(str, "i") fmt.Printf("countTime0 is %v\n", countTime0) fmt.Printf("countTime1 is %v\n", countTime1) } ~~~ 輸出結果: ~~~ countTime0 is 3 countTime1 is 0 ~~~ 重復 n 次 str ~~~ package main import ( "fmt" "strings" ) func main() { str := "hello world " res0 := strings.Repeat(str, 0) res1 := strings.Repeat(str, 1) res2 := strings.Repeat(str, 2) // strings.Repeat("原字符串", 重復次數) fmt.Printf("res0 is %v\n", res0) fmt.Printf("res1 is %v\n", res1) fmt.Printf("res2 is %v\n", res2) } ~~~ 輸出結果: ~~~ res0 is res1 is hello world res2 is hello world hello world ~~~ str 轉為大寫 ~~~ package main import ( "fmt" "strings" ) func main() { str := "hello world " res := strings.ToUpper(str) fmt.Printf("res is %v\n", res) } ~~~ 輸出結果: ~~~ res is HELLO WORLD ~~~ str 轉為小寫 ~~~ package main import ( "fmt" "strings" ) func main() { str := "HELLO WORLD " res := strings.ToLower(str) fmt.Printf("res is %v\n", res) } ~~~ 輸出結果: ~~~ res is hello world ~~~ 去掉 str 首尾的空格 ~~~ package main import ( "fmt" "strings" ) func main() { str := " hello world " res := strings.TrimSpace(str) fmt.Printf("res is %v\n", res) } ~~~ 去掉字符串首尾指定的字符 ~~~ package main import ( "fmt" "strings" ) func main() { str := "hi , hello world , hi" res := strings.Trim(str, "hi") fmt.Printf("res is %v\n", res) } ~~~ 輸出結果: ~~~ res is , hello world , ~~~ 去掉字符串首指定的字符 ~~~ package main import ( "fmt" "strings" ) func main() { str := "hi , hello world , hi" res := strings.TrimLeft(str, "hi") fmt.Printf("res is %v\n", res) } ~~~ 輸出結果: ~~~ res is , hello world , hi ~~~ 去掉字符串尾指定的字符 ~~~ package main import ( "fmt" "strings" ) func main() { str := "hi , hello world , hi" res := strings.TrimRight(str, "hi") fmt.Printf("res is %v\n", res) } ~~~ 輸出結果: ~~~ res is hi , hello world , ~~~ 返回str空格分隔的所有子串的slice, ~~~ package main import ( "fmt" "strings" ) func main() { str := "hello world ,hello golang" res := strings.Fields(str) fmt.Printf("res is %v\n", res) } ~~~ 輸出結果: ~~~ res is [hello world ,hello golang] ~~~ 返回str 指定字符分隔的所有子串的slice ~~~ package main import ( "fmt" "strings" ) func main() { str := "hello world ,hello golang" res := strings.Split(str, "o") fmt.Printf("res is %v\n", res) } ~~~ 輸出結果: ~~~ res is [hell w rld ,hell g lang] ~~~ 用指定字符將 string 類型的 slice 中所有元素鏈接成一個字符串 ~~~ package main import ( "fmt" "strings" ) func main() { str := []string{"hello", "world", "hello", "golang"} res := strings.Join(str, "++") fmt.Printf("res is %v\n", res) /* num := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 0} res1 := strings.Join(num, "++") // cannot use num (type []int) as type []string in argument to strings.Join fmt.Println(res1) */ } ~~~ 輸出結果: ~~~ res is hello++world++hello++golang ~~~
                  <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>

                              哎呀哎呀视频在线观看