{"id":207925,"date":"2025-07-08T11:45:28","date_gmt":"2025-07-08T03:45:28","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207925\/"},"modified":"2025-07-08T11:45:28","modified_gmt":"2025-07-08T03:45:28","slug":"%e5%9c%a8%e5%ad%97%e7%ac%a6%e4%b8%b2%e6%90%9c%e7%b4%a2%e4%b8%ad%e5%a4%84%e7%90%86-unicode","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207925\/","title":{"rendered":"\u5728\u5b57\u7b26\u4e32\u641c\u7d22\u4e2d\u5904\u7406 Unicode"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>\u5728\u5b57\u7b26\u4e32\u641c\u7d22\u4e2d\u5904\u7406 Unicode<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-26 20:57:33<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u4eca\u5929\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5728\u5b57\u7b26\u4e32\u641c\u7d22\u4e2d\u5904\u7406 Unicode\u300b<\/span>\uff0c\u5176\u4e2d\u6d89\u53ca\u5230\u7684\u77e5\u8bc6\u70b9\u5305\u62ec<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u65e0\u8bba\u4f60\u662f\u5c0f\u767d\u8fd8\u662f\u8001\u624b\uff0c\u90fd\u9002\u5408\u770b\u4e00\u770b\u54e6~\u6709\u597d\u7684\u5efa\u8bae\u4e5f\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u7559\u8a00\uff0c\u82e5\u662f\u770b\u5b8c\u6709\u6240\u6536\u83b7\uff0c\u4e5f\u5e0c\u671b\u5927\u5bb6\u80fd\u591a\u591a\u70b9\u8d5e\u652f\u6301\u5440\uff01\u4e00\u8d77\u52a0\u6cb9\u5b66\u4e60~<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u5047\u8bbe\u6211\u6709\u4e00\u4e2a\u5305\u542b unicode \u5b57\u7b26\u7684\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>s := \"foo \u65e5\u672c foo!\"\n<\/pre>\n<p>\u6211\u6b63\u5728\u5c1d\u8bd5\u67e5\u627e\u5b57\u7b26\u4e32\u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684 <code>foo<\/code>\uff1a<\/p>\n<pre>index := strings.LastIndex(s, \"foo\")\n<\/pre>\n<p>\u8fd9\u91cc\u7684\u9884\u671f\u7ed3\u679c\u662f 7\uff0c\u4f46\u7531\u4e8e\u5b57\u7b26\u4e32\u4e2d\u7684 unicode\uff0c\u8fd9\u5c06\u8fd4\u56de 11 \u4f5c\u4e3a\u7d22\u5f15\u3002<\/p>\n<p>\u6709\u6ca1\u6709\u529e\u6cd5\u4f7f\u7528\u6807\u51c6\u5e93\u51fd\u6570\u6765\u5904\u7406\u8fd9\u4e2a\u95ee\u9898\uff1f<\/p>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u60a8\u9047\u5230\u4e86 go \u548c\u5b57\u8282\u4e2d\u7684<strong>\u7b26\u6587<\/strong>\u4e4b\u95f4\u7684\u5dee\u5f02\u3002\u5b57\u7b26\u4e32\u7531\u5b57\u8282\u7ec4\u6210\uff0c\u800c\u4e0d\u662f\u7b26\u6587\u3002\u5982\u679c\u60a8\u8fd8\u6ca1\u6709\u4e86\u89e3\u8fd9\u4e00\u70b9\uff0c\u60a8\u5e94\u8be5\u9605\u8bfb \u3002<\/p>\n<p>\u8fd9\u662f\u6211\u7684\u5feb\u901f\u51fd\u6570\u7248\u672c\uff0c\u7528\u4e8e\u8ba1\u7b97\u5b57\u7b26\u4e32\u4e2d\u6700\u540e\u4e00\u4e2a\u5b50\u5b57\u7b26\u4e32\u5339\u914d\u4e4b\u524d\u7684\u7b26\u6587\u6570\u91cf\u3002\u57fa\u672c\u65b9\u6cd5\u662f\u627e\u5230\u5b57\u8282\u7d22\u5f15\uff0c\u7136\u540e\u5bf9\u5b57\u7b26\u4e32\u7b26\u6587\u8fdb\u884c\u8fed\u4ee3\/\u8ba1\u6570\uff0c\u76f4\u5230\u6d88\u8017\u5b8c\u8be5\u6570\u91cf\u7684\u5b57\u8282\u3002<\/p>\n<p>\u6211\u4e0d\u77e5\u9053\u53ef\u4ee5\u76f4\u63a5\u6267\u884c\u6b64\u64cd\u4f5c\u7684\u6807\u51c6\u5e93\u65b9\u6cd5\u3002<\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n    \"strings\"\n)\n\nfunc lastruneindex(s, substr string) (int, error) {\n    byteindex := strings.lastindex(s, substr)\n    if byteindex &lt; 0 {\n        return byteindex, nil\n    }\n    reader := strings.newreader(s)\n    count := 0\n    for byteindex &gt; 0 {\n        _, bytes, err := reader.readrune()\n        if err != nil {\n            return 0, err\n        }\n        byteindex = byteindex - bytes\n        count += 1\n    }\n    return count, nil\n}\n\nfunc main() {\n    s := \"foo \u65e5\u672c foo!\"\n    count, err := lastruneindex(s, \"foo\")\n    fmt.println(count, err)\n    \/\/ outputs:\n    \/\/ 7 &lt;nil&gt;\n}\n<\/pre>\n<p>\u8fd9\u975e\u5e38\u63a5\u8fd1\uff1a<\/p>\n<pre>package main\n\nimport (\n   \"golang.org\/x\/text\/language\"\n   \"golang.org\/x\/text\/search\"\n)\n\nfunc main() {\n   m := search.new(language.english)\n   start, end := m.indexstring(\"foo \u65e5\u672c foo!\", \"foo\")\n   println(start == 0, end == 3)\n}<\/pre>\n<p>\u4f46\u5b83\u6b63\u5728\u5411\u524d\u641c\u7d22\u3002\u6211\u8bd5\u8fc7\u8fd9\u4e2a\uff1a<\/p>\n<pre>m.indexstring(\"foo \u65e5\u672c foo!\", \"foo\", search.backwards)<\/pre>\n<p>\u4f46\u6211\u5f97\u5230\u8fd9\u4e2a\u7ed3\u679c\uff1a<\/p>\n<pre>panic: TODO: implement<\/pre>\n<ul>\n<li><\/li>\n<li><\/li>\n<\/ul>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300a\u5728\u5b57\u7b26\u4e32\u641c\u7d22\u4e2d\u5904\u7406 Unicode\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207925","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207925","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=207925"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207925\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207925"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207925"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207925"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}