{"id":2168,"date":"2024-11-05T20:31:49","date_gmt":"2024-11-05T12:31:49","guid":{"rendered":"https:\/\/blog.xiunian.wang\/?p=2168"},"modified":"2024-11-05T20:32:47","modified_gmt":"2024-11-05T12:32:47","slug":"android%e5%a6%82%e4%bd%95%e6%ad%a3%e7%a1%ae%e7%9a%84%e6%88%aa%e5%8f%96%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%ad%90%e4%b8%b2","status":"publish","type":"post","link":"https:\/\/blog.xiunian.wang\/?p=2168","title":{"rendered":"Android\u5982\u4f55\u6b63\u786e\u7684\u622a\u53d6\u5b57\u7b26\u4e32\/emoji\u5b50\u4e32."},"content":{"rendered":"\n<p>\u8fd9\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4e0d\u80fd\u518d\u7b80\u5355\u7684\u95ee\u9898\uff0c\u7136\u800c\uff0c\u5c31\u662f\u8fd9\u6837\u7b80\u5355\u7684\u5e38\u89c1\u95ee\u9898\uff0c\u5f80\u5f80\u4f1a\u88ab\u5ffd\u89c6\uff0c\u4ece\u800c\u7ed9\u672a\u6765\u7684\u67d0\u4e00\u5929\u57cb\u5751\u3002<\/p>\n\n\n\n<p>\u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\u90fd\u63d0\u4f9b\u4e86built-in \u622a\u53d6\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p>\u6bd4\u5982 Python\u6700\u4e3a\u7b80\u5355\u76f4\u89c2\uff0c\u6bd4\u5982\u4ece&#8221;Hello, World!&#8221; \u4e2d\u63d0\u53d6\u51fa World \u53ef\u4ee5\u901a\u8fc7\u4e0b\u6807\u76f4\u63a5\u83b7\u53d6<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">text = \"Hello, World!\"\nsubstring = text[7:12]\nprint(substring) #print World<\/pre>\n\n\n\n<p>&nbsp;\u5728 JS \u4e2d\uff0cString \u4e5f\u63d0\u4f9b<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">text = \"Hello, World!\"\nsubstring = text.substring(7,12)\nprint(substring) \/\/print 'World'<\/pre>\n\n\n\n<p>\u5728 Android \u4e2d\uff0cKotlin \u548c Java \u7684 String \u540c\u6837\u6709\u7c7b\u4f3c\u7684\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">val text = \"Hello, World!\"\nval substring = text.substring(7,12)\nprint(substring) \/\/print 'World'<\/pre>\n\n\n\n<p>\u76f8\u4fe1\u4f60\u4e5f\u4e00\u5b9a\u5199\u8fc7\u4e0a\u9762\u7684\u4ee3\u7801\uff0c\u4f46\u662f\u771f\u7684\u4f1a\u5982\u4f60\u7684\u9884\u671f\u5f97\u5230\u60f3\u8981\u7684\u7ed3\u679c\u5417\uff1f<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u4e2a\u5b57\u7b26\u4e32\u7684\u4e0b\u6807\uff0c\u5230\u5e95\u8868\u793a\u7684\u662f\u4ec0\u4e48\uff1f<\/h2>\n\n\n\n<!--more-->\n\n\n\n<p>\u4e00\u4e2a\u7b80\u5355\u95ee\u9898\uff0c\u4ec0\u4e48\u662f\u5b57\u7b26\u4e32\uff1f \u82f1\u6587\u6211\u4eec\u53ebString\uff0c\u5176\u672c\u610f\u662f\u7528\u6765\u4e32\u4e1c\u897f\u7684\u7ec6\u7ef3\u5b50\u3002\u725b\u6d25\u8bcd\u5178\u89e3\u91ca\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p><strong>long, thin material used for tying things together, made of several&nbsp;<\/strong><a>threads<\/a><strong>&nbsp;that have been&nbsp;<\/strong><a>twisted<\/a><strong>&nbsp;together; a piece of&nbsp;<\/strong><strong>string<\/strong><strong>&nbsp;used to fasten or pull something or keep something in place.<\/strong><\/p>\n\n\n\n<p>\u6240\u4ee5\u5b57\u7b26\u4e32\u8868\u793a\u5c31\u662f\u4e00\u4e32\u5b57\u7b26\u54af, \u8fd9\u4e00\u70b9 Java\u5b98\u7f51\u4e5f\u6709\u5b9a\u4e49(<a target=\"_blank\" href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/?java\/lang\/String.html\" rel=\"noreferrer noopener\">&nbsp;https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/?java\/lang\/String.html&nbsp;<\/a>)\u3002\u8fdb\u800c\uff0c\u6211\u4eec\u53ef\u4ee5\u7406\u89e3\u4e3a\u662f\u5bf9\u5b57\u7b26 Char(character) \u7684\u7ec4\u5408\u3002\u5373\u6211\u4eec\u7684\u4e0b\u6807\u53ef\u4ee5\u7406\u89e3\u4e3a\u5b57\u7b26\u3002<\/p>\n\n\n\n<p>\u5728 Android \u4e2d\uff0c\u6211\u4eec\u5b57\u7b26\u4e32\u9ed8\u8ba4\u662f\u4f7f\u7528 UTF-8 \u6765\u7f16\u7801\u7684\u3002 \u4f46\u662f\u5b57\u7b26Char\uff0c\u5219\u662f UTF-16\u6765\u7f16\u7801\u7684\u3002 \u5b98\u65b9\u5b9a\u4e49\uff1a<a target=\"_blank\" href=\"https:\/\/docs.oracle.com\/javase\/6\/docs\/api\/java\/lang\/Character.html\" rel=\"noreferrer noopener\">&nbsp;https:\/\/docs.oracle.com\/javase\/6\/docs\/api\/java\/lang\/Character.html<\/a><\/p>\n\n\n\n<p>\u8fd9\u4e5f\u5c31\u662f\u8bf4\u4e3a\u4ec0\u4e48&#8221;abcdefg&#8221; \u5f53 index \u4e3a 0 \u5f97\u5230Char\uff0c a\u3002 1\u5f97\u5230 b\u3002&nbsp;<\/p>\n\n\n\n<p>\u4e00\u4e2a\u82f1\u6587\u5b57\u6bcd UTF-8\u7f16\u7801\u4e0b\u5360\u7528\u4e00\u4e2a\u5b57\u8282\uff0c\u5982\u679c\u662f\u6c49\u5b57\uff0cUTF-8 \u7f16\u7801\u7684\u201c\u6211\u7231\u5317\u4eac\u5929\u5b89\u95e8\u201d \uff0c\u4e0b\u6807\u6307\u4ee3\u7684\u5b57\u7b26\u662f\u4e0d\u662f\u548c\u4e0a\u9762\u82f1\u6587\u4e00\u6837\u5462\uff1f(0\u4ee3\u8868 \u6211\uff0c1 \u4ee3\u8868\u7231\uff09<\/p>\n\n\n\n<p>\u7b54\u6848\u662f\uff1a \u662f\u7684\u3002\u4e3a\u4ec0\u4e48\uff1f\u901a\u8fc7\u4e0a\u9762\u79d1\u666e\u4e86\u89e3\u5230\uff0c\u867d\u7136\u4e00\u4e2a\u6c49\u5b57\u5728 UTF-8 \u4e2d\u5360\u7528\u4e86\u4e09\u4e2a\u5b57\u8282, \u5927\u4e8e\u4e00\u4e2a char \u6240\u5360\u7528\u7684 2 \u4e2a\u5b57\u8282\uff0c\u4f46\u662f\u4e3a\u4ec0\u4e48\u8fd8\u80fd\u548c\u53ea\u5360\u4e00\u4e2a\u5b57\u8282\u7684\u82f1\u6587\u4ea7\u751f\u4e00\u6837\u7684\u6548\u679c\uff1f \u56e0\u4e3a\u8fd9\u4e2achar\u662f&nbsp;UTF-16\u7f16\u7801\u7684\u6587\u672c\uff0c\u5360\u7528\u4e24\u4e2a\u5b57\u8282\uff0c \u5927\u90e8\u5206\u6c49\u5b57\uff0c\u5728 UTF-16 \u7f16\u7801\u7684\u60c5\u51b5\u4e0b\u53ea\u5360\u4e24\u4e2a\u5b57\u8282\uff0c\u56e0\u6b64\u53ef\u4ee5\u4ee5 char \u6765\u8fdb\u884c\u5206\u5272\u5f97\u5230\u4e00\u4e2a\u5b8c\u6574\u7684\u6c49\u5b57\u3002<\/p>\n\n\n\n<p>\u4e0a\u9762\u7684\u6bcf\u4e00\u4e2a\u6c49\u5b57\u548c\u5b57\u6bcd\u5176\u5b9e\u90fd\u5bf9\u5e94\u4e00\u4e2aunicode\u7801\u70b9\uff08code point\uff09\u3002 \u800c\u6211\u4eec\u4ee3\u7801\u4e2d\u7684\u7d22\u5f15\u6307\u7684\u662f\u4e00\u4e2aString \u4e32\u4e0b\u6807 index \u662f char \u7684\u4f4d\u7f6eposition\u3002 \u4e24\u8005\u5e76\u4e0d\u5bf9\u5e94\uff0c\u8fd9\u4e00\u70b9\u4e0d\u80fd\u7406\u89e3\u9519\u4e86\u3002<\/p>\n\n\n\n<p>\u6240\u4ee5\u4e00\u4e2a char \u80fd\u4ee3\u8868\u4e00\u4e2a\u6c49\u5b57\u5417\uff1f\uff0c\u7b54\u6848\u662f\u4e0d\u4e00\u5b9a\uff0c\u4e3e\u4e2a\u4f8b\u5b50<\/p>\n\n\n\n<p>&#8220;\ud880\udf02&#8221;&nbsp; \uff0c\u5b83\u7684\u957f\u5ea6\u5c31\u662f 2\uff0c\u9700\u8981\u4e24\u4e2a char \u624d\u80fd\u4ee3\u8868\u5b57\u7b26<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"> val s=\"\ud880\udf02\"\n print(s.length)\/\/\u8f93\u51fa 2<\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"954\" src=\"https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/11\/image-1024x954.png\" alt=\"\" class=\"wp-image-2172\" srcset=\"https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/11\/image-1024x954.png 1024w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/11\/image-300x279.png 300w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/11\/image-768x715.png 768w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/11\/image.png 1080w\" sizes=\"(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p>\u5982\u4f55\u89e3\u51b3\u5462\uff1f\u8ba9 index \u53d8\u6210\u6211\u4eec\u7406\u89e3\u7684 index \u5462\u3002<\/p>\n\n\n\n<p>\u9488\u5bf9\u4e0a\u9762\u60c5\u51b5\uff0c\u5904\u7406\u81ea\u5df1\u624b\u5199\u4ee3\u7801\u4ee5\u5916\uff0c\u5728 Java\u4e2d String \u7c7b\u6709\u4e00\u4e2a\u53eboffsetByCodePoints \u7684\u65b9\u6cd5\uff0c\u63a5\u6536\u4e00\u4e2a\u4e24\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e00\u4e2a\u662f index\uff0c\u4e5f\u5c31\u662fchar \u7684 index\uff0c\u5982\u679c\u6307\u5b9a\u4e3a 0 \u5219\u4ece\u7b2c\u4e00\u4e2a char \u5f00\u59cb\u3002codePointOffset \u5c31\u662f\u6211\u4eec\u7406\u89e3\u7684\u90a3\u4e2a\u5b57\u7b26\u7684index\u62c9\uff0c\u901a\u8fc7\u5b83\u6211\u4eec\u5c31\u80fd\u62ff\u5230\u6211\u4eec\u9700\u8981\u7684\u5b57\u7b26\u7684\u5bf9\u5e94\u5230 char \u7684 index\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"569\" src=\"https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/11\/image-1-1024x569.png\" alt=\"\" class=\"wp-image-2173\" srcset=\"https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/11\/image-1-1024x569.png 1024w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/11\/image-1-300x167.png 300w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/11\/image-1-768x427.png 768w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/11\/image-1.png 1080w\" sizes=\"(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p>\u4e3e\u4e2a\u4f8b\u5b50\u65b9\u4fbf\u7406\u89e3\uff0c\u6211\u4eec\u901a\u8fc7 offset \u8bbe\u7f6e\u4e3a\u6211\u4eec\u6240\u7406\u89e3\u7684\u5b57\u7b26\u7684 index \u4e3a 2\uff0c\u5219\u8f93\u51fa\u201c\u5403\u201d<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">val s=\"\ud880\udf02\u597d\u5403\"\nprint(\"s:\"+s[s.offsetByCodePoints(0,2)])\/\/ \u8f93\u51fa s:\u5403<\/pre>\n\n\n\n<p>\u5f53\u6211\u4eec\u8981\u4ece&#8221;\ud880\udf02\u597d\u5403&#8221; \u622a\u53d6\u5b50\u4e32&#8221;\u597d\u5403&#8221;\u7684\u65f6\u5019\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8f6c\u6362\u7d22\u5f15\u5f97\u5230\u6b63\u786e\u7684 char \u7684 index\uff0c\u518d\u8fdb\u884c\u622a\u53d6\u3002&nbsp;&nbsp;<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">  val t=\"\ud880\udf02\u597d\u5403\" \n  val key=\"\u597d\u5403\"\n  val index = t.offsetByCodePoints(0,1)\/\/ \u5047\u8bbe\u6211\u4eec\u5df2\u77e5\u597d\u7684 index  \u4e3a 1\n  print(\"s:\"+t.substring(index, index+key.length))\/\/\u8f93\u51fa\uff1a s:\u597d\u5403<\/pre>\n\n\n\n<p>\u770b\u5230\u8fd9\u91cc\uff0c\u4f60\u5e94\u8be5\u660e\u767d\u4e86 char \u7684 index \u548c\u7801\u70b9\uff08\u5b57\u7b26\uff09\u7684 index \u5e94\u8be5\u4e0d\u662f\u540c\u4e00\u4e2a\u6982\u5ff5\u4e86\u5427\u3002<\/p>\n\n\n\n<p>\u9664\u4e86\u6c49\u5b57\u4e4b\u5916\uff0c\u53e6\u4e00\u7c7b\u5e38\u89c1\u7684\u5b57\u7b26\u4e5f\u6709\u53ef\u80fd\u51fa\u73b0\u622a\u65ad\u5931\u8d25\uff0c\u90a3\u5c31\u662f emoji \uff0cemoji\u51fa\u73b0\u622a\u65ad\u5931\u8d25\u7684\u60c5\u51b5\u751a\u81f3\u8fdc\u6bd4\u622a\u65ad\u6c49\u5b57\u66f4\u5e38\u89c1\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">emoji\u8868\u60c5\u5728\u5b57\u7b26\u4e32\u4e2d\u5230\u5e95\u662f\u4ec0\u4e48\uff1f<\/h2>\n\n\n\n<p>\u9996\u5148\uff0cemoji \u662f\u600e\u4e48\u8868\u8fbe\u7684\uff0c\u5f53\u7136\u4e5f\u662f\u901a\u8fc7\u7801\u70b9\uff0c\u4f46\u662f\uff0c\u5b83\u6709\u53ef\u80fd\u662f\u4e00\u4e2acodepoint\uff0c\u4e5f\u6709\u53ef\u80fd\u662f\u591a\u4e2acodepoints\uff0c\u56e0\u4e3a\u5b83\u7684\u5b57\u7b26\u96c6\u662f\u4e0d\u65ad\u5728\u6269\u5145\u7684\u3002\u5230\u73b0V16\u7248\u672c\u5728\u5df2\u7ecf\u6709 3790\u4e2a\u4e86 \u4f20\u9001\u95e8\uff08<a target=\"_blank\" href=\"https:\/\/unicode.org\/emoji\/charts\/emoji-counts.html\" rel=\"noreferrer noopener\">&nbsp;https:\/\/unicode.org\/emoji\/charts\/emoji-counts.html\uff09<\/a><\/p>\n\n\n\n<p>\u4e00\u4e9b\u7b80\u5355\u7684 emoji \u7531\u5355\u4e00\u7684 codepoint \u8868\u793a\ud83d\ude00 (\u7b11\u8138) \u7684 codepoint \u662f U+1F600\u3002<\/p>\n\n\n\n<p>\u4e00\u4e9b\u590d\u6742\u7684 emoji \u7531\u591a\u4e2a codepoint \u7ec4\u5408\u800c\u6210\u3002\u4f8b\u5982\uff1a\ud83d\udc68\u200d\ud83d\udc69\u200d\ud83d\udc67\u200d\ud83d\udc66 (\u5bb6\u5ead) \u662f\u7531\u591a\u4e2a codepoint \u7ec4\u5408\u800c\u6210\u7684\uff1aU+1F468 (\u7537\u4eba) + U+200D (ZWJ) + U+1F469 (\u5973\u4eba) + U+200D (ZWJ) + U+1F467 (\u5973\u5b69) + U+200D (ZWJ) + U+1F466 (\u7537\u5b69)\u3002<\/p>\n\n\n\n<p>\u8fd9\u4e00\u7c7b\u53eb\u505aZWJ Emoji\uff0cZWJ \u662f Zero Width Joiner\uff08\u96f6\u5bbd\u8fde\u63a5\u7b26\uff09\u7684\u7f29\u5199\uff0cUnicode \u4e2d\u7684 codepoint \u662f U+200D\u3002ZWJ \u7528\u4e8e\u5c06\u591a\u4e2a\u5b57\u7b26\u8fde\u63a5\u5728\u4e00\u8d77\uff0c\u5f62\u6210\u4e00\u4e2a\u65b0\u7684\u590d\u5408 emoji\u3002ZWJ emoji \u662f\u901a\u8fc7\u5c06\u591a\u4e2a\u72ec\u7acb\u7684 emoji \u5b57\u7b26\u7528 ZWJ \u8fde\u63a5\u7b26\u8fde\u63a5\u8d77\u6765\uff0c\u5f62\u6210\u4e00\u4e2a\u65b0\u7684\u3001\u66f4\u590d\u6742\u7684\u56fe\u5f62\u3002\u8fd9\u6837\u6211\u4eec\u5c31\u80fd\u4e0d\u65ad\u6269\u5145\u8868\u60c5\u4e86\ud83e\udd23\ud83e\udd23\ud83e\udd23<\/p>\n\n\n\n<p>\u90a3\u8fd9\u6837\u7684\u8bdd\u4e0a\u9762\u4ee3\u7801\u5c31\u770b\u4e0d\u51fa\u6709\u51e0\u4e2a\u201c\u6211\u4eec\u7406\u89e3\u7684\u201dindex \u4e86\uff0c\u56e0\u4e3a\u4f60\u4e0d\u77e5\u9053\u8fd9\u4e2a emoji \u5360\u7528\u4e86\u51e0\u4e2a codepoint\uff0c\u4ece\u800c\u4f7f\u7528 codepoint \u53bb\u5207\u5206 emoji\uff0c\u5982\u679c\u9047\u5230\u7684\u662f ZWJ emoji \u5c31\u4f1a\u5c06 emoji \u622a\u65ad\u4e86\uff0c\u4e0d\u7b26\u5408\u9884\u671f\u554a\u3002<\/p>\n\n\n\n<p>\u800c\u4e14\uff0cemoji \u5360\u7528\u51e0\u4e2a codepoint \u751a\u81f3\u8fde\u7cfb\u7edf\u90fd\u4e0d\u4e00\u5b9a\u77e5\u9053\uff0c\u56e0\u4e3a\u8fd9\u662f\u4e00\u4e2a\u52a8\u6001\u53ef\u62d3\u5c55\u7684\uff0c\u6709\u4e9b emoji \u4f4e\u7248\u672c\u7684\u7cfb\u7edf\u4e0a\u5c31\u662f\u4e0d\u80fd\u6b63\u5e38\u663e\u793a\u7684\u3002\u6211\u4eec\u5728\u4e00\u4e9b\u7f16\u8f91\u5668\u4e2d\u5bf9\u8fd9\u79cd emoji\u8fdb\u884c\u5220\u9664\u64cd\u4f5c\u65f6\u8fd8\u4f1a\u51fa\u73b0\u53d8\u6210\u53e6\u5916\u4e00\u4e2a \u6216\u8005\u591a\u4e2aemoji \u7684\u60c5\u51b5<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"746\" height=\"170\" src=\"https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/11\/a0618cb18b2d4dbe.gif\" alt=\"\" class=\"wp-image-2175\"\/><\/figure>\n\n\n\n<p>\u65e2\u7136\u7cfb\u7edf\u8ba4\u4e0d\u8ba4\u8bc6\uff0c\u53ea\u6709\u7cfb\u7edf\u624d\u77e5\u9053\u3002<\/p>\n\n\n\n<p>\u90a3\u6ca1\u6709\u89e3\u4e86\u4e48\uff1f<\/p>\n\n\n\n<p>\u4e00\u4e2a\u5bb9\u6613\u60f3\u5230\u7684\u65b9\u6cd5\u662f\u901a\u8fc7 emoji \u7684\u89c4\u5f8b\u4f7f\u7528\u6b63\u5219\u5339\u914d\uff0c\u4f46\u662f emoji \u662f\u4e0d\u65ad\u8fed\u4ee3\u7684\uff0c\u8c01\u80fd\u4fdd\u8bc1 V17 V18\u540e\u9762\u4e0d\u4f1a\u65b0\u589e\u65b0\u7684\u7ec4\u5408\u5462.<\/p>\n\n\n\n<p>\u6216\u8005\u5bfb\u6c42\u53e6\u5916\u7684\u89e3\u51b3\u65b9\u6848\u3002\u6bd4\u5982\u672c\u5730\u5c06 emoji \u5217\u8868\u5168\u90e8\u8bb0\u5f55\u4e0b\u6765\u4f7f\u7528\u6620\u5c04\u6765\u5224\u65ad\u5f53\u7136\u4e5f\u662f\u4e00\u79cd\u65b9\u6cd5\uff0c\u4f46\u662f\u8fd9\u6837\u8981\u6c42app \u989d\u5916\u5360\u7528\u4e86\u5185\u5b58\u7a7a\u95f4\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0a\u4e24\u79cd\u65b9\u6cd5\u6548\u7387\u4e0a\u6765\u8bf4\u90fd\u4e0d\u9ad8\u3002<\/p>\n\n\n\n<p>\u770b\u770b\u6709\u6709\u54ea\u4e9b\u73b0\u6210\u7684\u53ef\u4ee5\u7528\u5427\u3002\u67e5\u9605\u4e86\u4e00\u4e9b\u8d44\u6599\u540e\uff0cBreakIterator \u662f\u4e00\u4e2a\u7528\u4e8e\u6587\u672c\u8fb9\u754c\u5206\u6790\u7684\u7c7b\uff0c\u5b83\u53ef\u4ee5\u8bc6\u522b\u6587\u672c\u4e2d\u7684\u8fb9\u754c\u4f4d\u7f6e\uff0c\u4f8b\u5982\u5355\u8bcd\u8fb9\u754c\uff0c\u5e94\u8be5\u53ef\u4ee5\u6b63\u786e\u7684\u5212\u5206 emoji \u5427\u3002<\/p>\n\n\n\n<p>\u901a\u8fc7BreakIterator.getCharacterInstance() \u53ef\u4ee5\u62ff\u5230\u8fed\u4ee3\u5668Iterator\uff0c\u8fdb\u4e00\u6b65\u8fed\u4ee3\u6587\u672c\u3002<\/p>\n\n\n\n<p>\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">fun main() {\n val t= \"\ud880\udf02\ud83d\udc68\u200d\ud83d\udc68\u200d\ud83d\udc67\u200d\ud83d\udc66\"\n count(t)\n}\n\nfun count(s:String){\n        val breakIterator = BreakIterator.getCharacterInstance()\n        breakIterator.setText(s)\n        var currentIndex = breakIterator.first()\n        val lastIndex = breakIterator.last()\n        while (currentIndex &lt; lastIndex) {\n            val next = breakIterator.following(currentIndex)\n            val currentStr = s.subSequence(currentIndex, next)\n            Log.d(TAG, \"current: $currentStr\")\n            currentIndex=next\n\n        }\n    }<\/pre>\n\n\n\n<p>\u5728intellij \u4e2d\u5b9e\u8df5\u53d1\u73b0\u5e76\u4e0d\u597d\u4f7f\u3002\u8f93\u51fa\u5982\u4e0b<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"306\" src=\"https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/11\/image-3-1024x306.png\" alt=\"\" class=\"wp-image-2176\" srcset=\"https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/11\/image-3-1024x306.png 1024w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/11\/image-3-300x90.png 300w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/11\/image-3-768x229.png 768w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/11\/image-3.png 1192w\" sizes=\"(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p>\u6700\u5f00\u59cb\u6000\u7591\u662f&nbsp;getCharacterInstance \u7684\u95ee\u9898\uff0c\u6362\u6210getWordInstance \u4f9d\u65e7\u4e0d\u884c\uff0c\u53ea\u662f\u5ffd\u7565\u6389\u4e86U+200D, \u8be5\u5207\u5272\u7684\u8fd8\u662f<\/p>\n\n\n\n<p>\u4f46\u662f\u5728 Android \u5e73\u53f0\u8bd5\u9a8c\u5374\u80fd\u591f\u6b63\u786e\u7684\u5904\u7406\uff01<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"992\" height=\"100\" src=\"https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/11\/image-4.png\" alt=\"\" class=\"wp-image-2177\" srcset=\"https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/11\/image-4.png 992w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/11\/image-4-300x30.png 300w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/11\/image-4-768x77.png 768w\" sizes=\"(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p>\u67e5\u9605\u5b98\u65b9\u6587\u6863<a href=\"https:\/\/developer.android.com\/reference\/kotlin\/android\/icu\/text\/BreakIterator\" target=\"_blank\" rel=\"noreferrer noopener\">&nbsp;https:\/\/developer.android.com\/reference\/kotlin\/android\/icu\/text\/BreakIterator&nbsp;<\/a>\u53d1\u73b0\uff0cAndroid \u7cfb\u7edf\u4e2d\u4fee\u6539\u4e86BreakIterator \u7684\u5b9e\u73b0\uff0c \u4f7f\u7528\u4e86\u540c\u540d\u7684 android.icu.text.BreakIterator\uff0c\u800c\u4e14\u4f7f\u7528\u7684\u662f\u7eaf Native \u5b9e\u73b0\uff0c\u90a3\u4e0d\u6bd4\u4e0a\u9762\u7684\u65b9\u6cd5\u6548\u7387\u9ad8\uff1f<\/p>\n\n\n\n<p>\u5b98\u65b9\u5e76\u5bf9getCharacterInstance\u7684\u7528\u6cd5\u7ed9\u51fa\u4e86\u8bf4\u660e\uff0c\u6b64 Character \u975e\u5f7cCharacter<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"165\" src=\"https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/11\/image-5-1024x165.png\" alt=\"\" class=\"wp-image-2178\" srcset=\"https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/11\/image-5-1024x165.png 1024w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/11\/image-5-300x48.png 300w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/11\/image-5-768x124.png 768w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/11\/image-5.png 1080w\" sizes=\"(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p>\u96be\u9053 Java \u771f\u7684\u4e0d\u884c\u5417\uff1f \u5176\u5b9e\u662f\u652f\u6301\u7684\uff0c\u4f46\u662f\u56e0\u4e3a\u6211\u4f7f\u7528\u7684\u662fSDK \u7248\u672c\u662f 17\uff0c \u66f4\u6362\u6210 JDK 20+\uff0c\u8868\u73b0\u4e5f\u662f\u4e00\u81f4\u7684\u4e86\u3002\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"94\" src=\"https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/11\/image-6-1024x94.png\" alt=\"\" class=\"wp-image-2179\" srcset=\"https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/11\/image-6-1024x94.png 1024w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/11\/image-6-300x28.png 300w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/11\/image-6-768x71.png 768w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/11\/image-6-1536x141.png 1536w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/11\/image-6.png 1674w\" sizes=\"(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\u603b\u7ed3<\/h2>\n\n\n\n<p>\u6240\u4ee5\u6b63\u786e\u7684\u59ff\u52bf\u622a\u53d6\u53ef\u4ee5\u5982\u4e0b\u53c2\u8003\uff08 Android or JDK20+\uff09\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">fun main() {\n    val t = \"\ud880\udf02\ud83d\udc68\u200d\ud83d\udc68\u200d\ud83d\udc67\u200d\ud83d\udc66abc\"\n    println(cutFirst(t, 4))\/\/\u8f93\u51fa \ud880\udf02\ud83d\udc68\u200d\ud83d\udc68\u200d\ud83d\udc67\u200d\ud83d\udc66ab\n}\n\nfun cutFirst(s: String, count: Int): String {\n    val breakIterator = BreakIterator.getCharacterInstance()\n    breakIterator.setText(s)\n    var currentIndex = breakIterator.first()\n    val sb = StringBuilder()\n    var total = 0\n\n    while (currentIndex != BreakIterator.DONE &amp;&amp; total &lt; count) {\n        val nextIndex = breakIterator.next()\n        if (nextIndex == BreakIterator.DONE) break\n        sb.append(s, currentIndex, nextIndex)\n        currentIndex = nextIndex\n        total++\n    }\n\n    return sb.toString()\n}<\/pre>\n\n\n\n<p>\u4ee5\u540e\u4ea7\u54c1\/\u8bbe\u8ba1\u540c\u5b66\u8bf4\uff0c\u8fd9\u4e2a\u5c55\u793a\u533a\u57df\u8d85\u8fc7 x\u4e2a\u5b57\u7b26\u5219\u622a\u53d6\uff0c\u4f60\u5e94\u8be5\u4e0d\u4f1a\u65e0\u8111\u5199\u4e0b\u5982\u4e0b\u4ee3\u7801\u4e86\u5427<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">val substring = text.substring(0,x)<\/pre>\n\n\n\n<p>\u867d\u7136\u770b\u8d77\u6765\u662f\u4e00\u4e2a\u5f88\u7b80\u5355\u7684\u9700\u6c42\uff0c\u8003\u8651\u5230ugc \u5185\u5bb9\u540e\uff0c\u53ef\u80fd\u6709\u8bb8\u8bb8\u591a\u591a\u610f\u60f3\u4e0d\u5230\u7684\u60c5\u51b5\u51fa\u73b0\u3002\u591a\u8bed\u8a00\u6bd4\u5982\u5370\u5730\u8bed\uff0cRTL\u7b49\u5176\u4ed6\u7684\u5c55\u73b0\u65b9\u5f0f\u90fd\u53ef\u80fd\u53d7\u5230\u5f71\u54cd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4e0d\u80fd\u518d\u7b80\u5355\u7684\u95ee\u9898\uff0c\u7136\u800c\uff0c\u5c31\u662f\u8fd9\u6837\u7b80\u5355\u7684\u5e38\u89c1\u95ee\u9898\uff0c\u5f80\u5f80\u4f1a\u88ab\u5ffd\u89c6\uff0c\u4ece\u800c\u7ed9\u672a\u6765\u7684\u67d0\u4e00\u5929\u57cb\u5751\u3002 \u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\u90fd\u63d0\u4f9b\u4e86built-in \u622a\u53d6\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u3002 \u6bd4\u5982 Python\u6700\u4e3a\u7b80\u5355\u76f4\u89c2\uff0c\u6bd4\u5982\u4ece&#8221;Hello, World!&#8221; \u4e2d\u63d0\u53d6\u51fa World \u53ef\u4ee5\u901a\u8fc7\u4e0b\u6807\u76f4\u63a5\u83b7\u53d6 &nbsp;\u5728 JS \u4e2d\uff0cString \u4e5f\u63d0\u4f9b \u5728 Android \u4e2d\uff0cKotlin \u548c Java \u7684 String \u540c\u6837\u6709\u7c7b\u4f3c\u7684\u65b9\u6cd5\uff1a \u76f8\u4fe1\u4f60\u4e5f\u4e00\u5b9a\u5199\u8fc7\u4e0a\u9762\u7684\u4ee3\u7801\uff0c\u4f46\u662f\u771f\u7684\u4f1a\u5982\u4f60\u7684\u9884\u671f\u5f97\u5230\u60f3\u8981\u7684\u7ed3\u679c\u5417\uff1f \u4e00\u4e2a\u5b57\u7b26\u4e32\u7684\u4e0b\u6807\uff0c\u5230\u5e95\u8868\u793a\u7684\u662f\u4ec0\u4e48\uff1f<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[13],"_links":{"self":[{"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=\/wp\/v2\/posts\/2168"}],"collection":[{"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2168"}],"version-history":[{"count":5,"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=\/wp\/v2\/posts\/2168\/revisions"}],"predecessor-version":[{"id":2181,"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=\/wp\/v2\/posts\/2168\/revisions\/2181"}],"wp:attachment":[{"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2168"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2168"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2168"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}