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

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                [TOC] go語言的string是一種數據類型。這個數據類型占用16字節空間,前面8字節是一個指針,指向字符串的地址。后面八個字節是一個整數,標識字符串長度 ## 字符串的存儲原理 ![](https://img.kancloud.cn/d3/ac/d3ac0bff3fd68c459dd266fe7b8297b0_1125x475.png) string 數據結構,源碼包 `src/runtime/string.go:stringStruct` 定義了string的數據結構: ```go type stringStruct struct { str unsafe.Pointer len int } ``` >[info] > - stringStruct.str:字符串的首地址 > - stringStruct.len: 字符串的長度 ## Unicode 轉換 utf8 ```go var s rune s = '曾' fmt.Printf("字符'曾'對應的Unicode的十進制: %d\n", s) fmt.Printf("字符'曾'對應的Unicode的十六進制: %x\n", s) fmt.Printf("字符'曾'對應的Unicode的二進制: %b\n", s) // 運行結果: // 字符'曾'對應的Unicode的十進制: 26366 // 字符'曾'對應的Unicode的十六進制: 66fe // 字符'曾'對應的Unicode的二進制: 110011011111110 ``` Unicode二進制轉換utf8二進制規則:`1110xxxx 10xxxxxx 10xxxxxx` 將Unicode最后六位補上utf8第三個字符的xxxxxx。將Unicode倒數7到12個字符補上utf第二個字符的xxxxxx。將Unicode剩下的數字補上utf8第一個字符(不夠前面補0) 示例如下: '曾'對應的Unicode二進制:110011011111110 '曾'對應的utf8二進制:11100110 10011011 10111110 驗證 ```go s1 := "曾" for i := 0; i < len(s1); i++ { fmt.Printf("s1[%d]: %d, %b\n", i, s1[i], s1[i]) } // 運行結果: // s1[0]: 230, 11100110 // s1[1]: 155, 10011011 // s1[2]: 190, 10111110 ``` ## 字符串與字節串相互轉換 ```go // 字符串與字節串相互轉換 // (1) 字符串轉字節串,有兩種方式轉換方式。 s4 := "abc,您好" fmt.Println([]byte(s4)) // utf8 編碼 【常用】 fmt.Println([]rune(s4)) // Unicode 編碼 // (2) 字節串轉字符串 b1 := []byte{97, 98, 99, 44, 230, 130, 168, 229, 165, 189} b2 := []rune{97, 98, 99, 44, 24744, 22909} fmt.Println(string(b1)) fmt.Println(string(b2)) // 運行結果: // [97 98 99 44 230 130 168 229 165 189] // [97 98 99 44 24744 22909] // abc,您好 // abc,您好 ``` ## 統計字符串個數 ```go s5 := "abc,您好" fmt.Printf("len: %v\n", len([]rune(s5))) // 運行結果: // len: 6 ``` ## for遍歷字符串 ```go s3 := "您好呀!abc" for i, v := range s3 { fmt.Println(i, string(v)) } // 運行結果: // 0 您 // 3 好 // 6 呀 // 9 ! // 12 a // 13 b // 14 c ```
                  <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>

                              哎呀哎呀视频在线观看