{"id":1732,"date":"2023-05-12T08:47:57","date_gmt":"2023-05-12T08:47:57","guid":{"rendered":"https:\/\/www.liutianfeng.com\/?p=1732"},"modified":"2023-05-17T01:24:57","modified_gmt":"2023-05-17T01:24:57","slug":"%e5%ad%97%e7%ac%a6%e4%b8%b2-string","status":"publish","type":"post","link":"https:\/\/www.liutianfeng.com\/?p=1732","title":{"rendered":"\u5b57\u7b26\u4e32 &#8211; String"},"content":{"rendered":"\n<p><\/p>\n\n\n<p><strong>\u65b9\u6cd5\uff1a<\/strong><br \/>Strings\u5305\u63d0\u4f9b\u4e86\u5e38\u7528\u548c\u65b9\u6cd5\u3002<br \/>Compare: \u6bd4\u8f83\u5b57\u7b26\u4e32<br \/>Contains: \u662f\u5426\u5305\u542b\u5b50\u5b57\u7b26\u4e32<br \/>Count: \u5b50\u5b57\u7b26\u4e32\u51fa\u73b0\u6b21\u6570<br \/>EqualFold: \u4e0d\u533a\u5206\u5927\u5c0f\u5199\u6bd4\u8f83<br \/>HasPrefix: \u662f\u5426\u6709\u524d\u7f00<br \/>HasSuffix: \u662f\u5426\u6709\u5c3e\u7f00<br \/>Index: \u83b7\u53d6\u5b50\u5b57\u7b26\u7684Index<br \/>Fields: \u6309\u7a7a\u767d\u5b57\u7b26\u5206\u5272\u5b57\u7b26\u4e32<br \/>Split: \u6307\u5b9a\u5206\u5272\u7b26\u5206\u5272<br \/>Join: \u5b57\u7b26\u4e32\u5408\u5e76<br \/>LastIndex: \u83b7\u53d6\u5b50\u5b57\u7b26\u5728\u5b57\u7b26\u4e32\u4e2d\u7684\u6700\u540e\u4e00\u4e2a\u7d22\u5f15<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">fmt.Println(strings.Compare(\"abc\", \"acb\"))                                   \/\/ -1\nfmt.Println(strings.Contains(\"abc\", \"ab\"))                                   \/\/ true\nfmt.Println(strings.ContainsAny(\"abc\", \"ac\"))                                \/\/ true\nfmt.Println(strings.Count(\"abca\", \"a\"))                                      \/\/ 2\nfmt.Println(strings.Fields(\"abc def ghi\"))                                   \/\/ [abc def ghi]\nfmt.Println(strings.HasPrefix(\"abc\", \"ab\"))                                  \/\/ true\nfmt.Println(strings.HasSuffix(\"abc\", \"ac\"))                                  \/\/ false\nfmt.Println(strings.Index(\"abc\", \"b\"))                                       \/\/ 1\nfmt.Println(strings.LastIndex(\"abcab\", \"a\"))                                 \/\/ 3\nfmt.Println(strings.Split(\"hello;my;name;is;tom\", \";\"))                      \/\/ [hello my name is tom]\nfmt.Println(strings.Join([]string{\"hello\", \"my\", \"name\", \"is\", \"tom\"}, \"_\")) \/\/ hello_my_name_is_tom\nfmt.Println(strings.Repeat(\"abc\", 3))                                        \/\/ abcabcabc\nfmt.Println(strings.Replace(\"abcdefabcabc\", \"ab\", \"xy\", 2))                  \/\/ xycdefxycabc, \u66ff\u6362\u524dn\u4e2a\nfmt.Println(strings.Replace(\"abcdefabcabc\", \"ab\", \"xy\", -1))                 \/\/ xycdefxycxyc, -1\u66ff\u6362\u5168\u90e8, =ReplaceAll\nfmt.Println(strings.ReplaceAll(\"abcdefabcabc\", \"ab\", \"xy\"))                  \/\/ xycdefxycxyc\nfmt.Println(strings.ToLower(\"abcABC\"))                                       \/\/ \u8f6c\u6362\u5c0f\u5199\nfmt.Println(strings.ToUpper(\"abcABC\"))                                       \/\/ \u8f6c\u6362\u5927\u5199\nfmt.Println(strings.Title(\"abcABC\"))                                         \/\/ \u9996\u5b57\u6bcd\u5927\u5199\nfmt.Println(strings.Trim(\"zzxyzabcxyzabcyxzxx\", \"xz\"))                       \/\/ yzabcxyzabcy, \u5de6\u53f3\u5168\u90e8\u5220\u51cfxz, \u76f4\u5230\u4e0d\u662f\u5b83\u4fe9\u4e3a\u6b62\nfmt.Println(strings.TrimLeft(\"zzxyzabcxyzabcyxzxx\", \"xz\"))                   \/\/ yzabcxyzabcyxzxx, \u4ec5\u5de6\u4fa7\nfmt.Println(strings.TrimRight(\"zzxyzabcxyzabcyxzxx\", \"xz\"))                  \/\/ zzxyzabcxyzabcy, \u4ec5\u53f3\u4fa7\nfmt.Println(strings.TrimSpace(\"   hello dkkd  dkkdi    \"))                   \/\/ hello dkkd  dkkdi\nfmt.Print(strings.TrimPrefix(\"\", \"\"))                                        \/\/ \u53bb\u9664\u524d\u7f00\nfmt.Print(strings.TrimSuffix(\"\", \"\"))                                        \/\/ \u53bb\u9664\u5c3e\u7f00<\/code><\/pre>\n<p><strong>[]byte\u7684\u65b9\u6cd5\u548cstrings\u7684\u65b9\u6cd5\u662f\u4e00\u6837\u7684\u3002<\/strong><\/p>\n<p>\u957f\u5ea6\u8ba1\u7b97\uff1a<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">import (\n\t\"fmt\"\n\t\"unicode\/utf8\"\n)\n\nfunc main() {\n\ts := \"\u6211\u7231\u4f60\uff0c\u4e2d\u534e\u4eba\u6c11\u5171\u548c\u56fd\uff01\"\n\tfmt.Println(len(s))                    \/\/ 36 byte\u7684\u5b57\u7b26\u6570\n\tfmt.Println(utf8.RuneCountInString(s)) \/\/ 12 UTF-8\u7684\u5b57\u7b26\u6570\n}<\/code><\/pre>\n<p><strong>strconv\u5305\uff1a<\/strong><\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">\/\/ bool: strconv.ParseBool, \u89e3\u6790\u5b57\u7b26\u4e32\u662f\u4e0d\u662fbool\u7c7b\u578b\nv, err := strconv.ParseBool(\"true\")\nfmt.Println(v, err) \/\/ true\ns_Bool := strconv.FormatBool(true)  \/\/ bool\u7c7b\u578b\u8f6c\u6362\u6210\u5b57\u7b26\u4e32\nfmt.Printf(\"Type: %T, Value: %v\\n\", s_Bool, s_Bool) \/\/ Type: string, Value: true\n\n\n\/\/ int: Atoi\u628a\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aint\nif v, err := strconv.Atoi(\"123\"); err == nil {\n\tfmt.Println(v)\n} else {\n\tfmt.Println(err)\n}\n\n\/\/ int: Itoa\u628aInt\u8f6c\u6362\u4e3astring\nstr1 := strconv.Itoa(123)\nfmt.Println(str1) \/\/ 123\n\n\/\/ 10\u8fdb\u5236\u6570\u5b57\u8f6c\u6362\u621016\u8fdb\u5236\u4e4b\u540e\u518d\u8f6c\u6362\u6210\u5b57\u7b26\u4e32\ni_Str := strconv.FormatInt(10, 16)\nfmt.Printf(\"%T, %v\\n\", i_Str, i_Str) \/\/ string, a\n\n\/\/ \u89e3\u679016\u8fdb\u5236\u7684\u5b57\u7b26\u4e32, 16\u8868\u793a16\u8fdb\u5236, 64\u8868\u793aint64\nif v, err := strconv.ParseInt(\"64\", 16, 64); err == nil {\n\tfmt.Printf(\"%T, %v\\n\", v, v) \/\/ int64, 100\n\n}\n\n\/\/ \u89e3\u6790Float\u5b57\u7b26\u4e32, 64\u8868\u793afloat64\nif v, err := strconv.ParseFloat(\"3.14\", 64); err == nil {\n\tfmt.Printf(\"%T, %v\\n\", v, v) \/\/ float64, 3.14\n\n}<\/code><\/pre>\n<p><strong>Sprintf: \u8f6c\u6362\u5b57\u7b26\u4e32<\/strong><\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">sd := fmt.Sprintf(\"%d\", 12)\nsf := fmt.Sprintf(\"%.2f\", 12.3333)\nfmt.Printf(\"sd: %T-%v; sf: %T-%v\\n\", sd, sd, sf, sf)  \/\/ sd: string-12; sf: string-12.33<\/code><\/pre>\n<p><strong>\u5192\u6ce1\u6392\u5e8f\uff1aExp<\/strong><\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">func main() {\n\ts1 := []int{5, 1, 4, 2, 3}\n\tfor j := 0; j &lt; len(s1)-1; j++ {\n\t\tfmt.Println(\"\u7b2c\", j, \"\u8f6e\u6bd4\u8f83...\")\n\t\tfor i := j + 1; i &lt; len(s1); i++ {\n\t\t\tleft := s1[j]\n\t\t\tright := s1[i]\n\t\t\ttmp := 0\n\t\t\tif right &lt;= left {\n\t\t\t\ttmp = s1[j]\n\t\t\t\ts1[j] = s1[i]\n\t\t\t\ts1[i] = tmp\n\t\t\t}\n\t\t}\n\t\tfmt.Println(\"\u7b2c\", j, \"\u8f6e\u6bd4\u8f83\u7ed3\u679c: \", s1)\n\t}\n}\n\n# go run main.go\n\u7b2c 0 \u8f6e\u6bd4\u8f83...\n\u7b2c 0 \u8f6e\u6bd4\u8f83\u7ed3\u679c:  [1 5 4 2 3]\n\u7b2c 1 \u8f6e\u6bd4\u8f83...\n\u7b2c 1 \u8f6e\u6bd4\u8f83\u7ed3\u679c:  [1 2 5 4 3]\n\u7b2c 2 \u8f6e\u6bd4\u8f83...\n\u7b2c 2 \u8f6e\u6bd4\u8f83\u7ed3\u679c:  [1 2 3 5 4]\n\u7b2c 3 \u8f6e\u6bd4\u8f83...\n\u7b2c 3 \u8f6e\u6bd4\u8f83\u7ed3\u679c:  [1 2 3 4 5]<\/code><\/pre>\n<p>\u00a0<\/p>\n<p>\u00a0<\/p><p>\u8f6c\u8f7d\u8bf7\u6ce8\u660e\uff1a<a href=\"https:\/\/www.liutianfeng.com\">liutianfeng.com<\/a> &raquo; <a href=\"https:\/\/www.liutianfeng.com\/?p=1732\">\u5b57\u7b26\u4e32 &#8211; String<\/a><\/p>","protected":false},"excerpt":{"rendered":"<p>\u65b9\u6cd5\uff1aStrings\u5305\u63d0\u4f9b\u4e86\u5e38\u7528\u548c\u65b9\u6cd5\u3002Compare: \u6bd4\u8f83\u5b57\u7b26\u4e32Contains: \u662f\u5426\u5305\u542b\u5b50\u5b57\u7b26\u4e32Cou [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[70],"tags":[],"_links":{"self":[{"href":"https:\/\/www.liutianfeng.com\/index.php?rest_route=\/wp\/v2\/posts\/1732"}],"collection":[{"href":"https:\/\/www.liutianfeng.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.liutianfeng.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.liutianfeng.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.liutianfeng.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1732"}],"version-history":[{"count":2,"href":"https:\/\/www.liutianfeng.com\/index.php?rest_route=\/wp\/v2\/posts\/1732\/revisions"}],"predecessor-version":[{"id":1745,"href":"https:\/\/www.liutianfeng.com\/index.php?rest_route=\/wp\/v2\/posts\/1732\/revisions\/1745"}],"wp:attachment":[{"href":"https:\/\/www.liutianfeng.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1732"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.liutianfeng.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1732"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.liutianfeng.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1732"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}