{"id":208511,"date":"2021-01-09T08:14:50","date_gmt":"2021-01-09T00:14:50","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=208511"},"modified":"2021-01-03T14:30:45","modified_gmt":"2021-01-03T06:30:45","slug":"ten-javascript-string-tips","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/ten-javascript-string-tips.html","title":{"rendered":"\u9001\u4f60\u5341\u6761 JavaScript \u5b57\u7b26\u4e32\u6280\u5de7"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u5b57\u7b26\u4e32\u662f\u51e0\u4e4e\u6240\u6709\u7f16\u7a0b\u8bed\u8a00\u4e2d\u6700\u57fa\u672c\u7684\u6570\u636e\u7c7b\u578b\u4e4b\u4e00\u3002\u540c\u65f6\uff0c\u5b83\u4e5f\u662f\u65b0\u5f00\u53d1\u4eba\u5458\u5b66\u4e60\u7684\u6700\u65e9\u7684\u6570\u636e\u7c7b\u578b\u4e4b\u4e00\u3002\u7136\u800c\uff0c\u5c24\u5176\u662f\u5728JavaScript\u4e2d\uff0c\u8bb8\u591a\u5f00\u53d1\u4eba\u5458\u5e76\u4e0d\u77e5\u9053\u5173\u4e8e\u5b57\u7b26\u4e32\u7684\u4e00\u4e9b\u6709\u8da3\u7684\u7ec6\u8282\u3002\u5e0c\u671b\u6b64\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"
\n\u6211\u4eec\u79f0\u4e00\u4e2a\u5b57\u7b26\u5e8f\u5217\u4e3a\u5b57\u7b26\u4e32\u3002\u8fd9\u51e0\u4e4e\u662f\u6240\u6709\u7f16\u7a0b\u8bed\u8a00\u4e2d\u90fd\u6709\u7684\u57fa\u672c\u7c7b\u578b\u4e4b\u4e00\u3002\u8fd9\u91cc\u8ddf\u5927\u5bb6\u5c55\u793a\u5173\u4e8e JS \u5b57\u7b26\u4e32\u768410\u4e2a\u5f88\u68d2\u7684\u6280\u5de7\uff0c\u4f60\u53ef\u80fd\u8fd8\u4e0d\u77e5\u9053\u54e6?<\/p>\n

1.\u5982\u4f55\u591a\u6b21\u590d\u5236\u4e00\u4e2a\u5b57\u7b26\u4e32<\/strong><\/div>\n

JS \u5b57\u7b26\u4e32\u5141\u8bb8\u7b80\u5355\u7684\u91cd\u590d\uff0c\u4e0e\u7eaf\u624b\u5de5\u590d\u5236\u5b57\u7b26\u4e32\u4e0d\u540c\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684repeat\u65b9\u6cd5\u3002<\/p>\n

\r\nconst laughing = '\u5c0f\u667a'.repeat(3) \r\nconsol.log(laughing) \/\/ \"\u5c0f\u667a\u5c0f\u667a\u5c0f\u667a\" \r\n \r\nconst eightBits = '1'.repeat(8) \r\nconsole.log(eightBits) \/\/ \"11111111\" \r\n<\/pre>\n
2. \u5982\u4f55\u586b\u5145\u4e00\u4e2a\u5b57\u7b26\u4e32\u5230\u6307\u5b9a\u7684\u957f\u5ea6<\/strong><\/div>\n

\u6709\u65f6\uff0c\u6211\u4eec\u5e0c\u671b\u5b57\u7b26\u4e32\u5177\u6709\u7279\u5b9a\u957f\u5ea6\u3002\u5982\u679c\u5b57\u7b26\u4e32\u592a\u77ed\uff0c\u5219\u9700\u8981\u586b\u5145\u5269\u4f59\u7a7a\u95f4\uff0c\u76f4\u5230\u8fbe\u5230\u6307\u5b9a\u7684\u957f\u5ea6\u4e3a\u6b62\u3002<\/p>\n

\u8fc7\u53bb\uff0c\u4e3b\u8981\u8fd8\u662f\u4f7f\u7528\u5e93 left-pad\u3002\u4f46\u662f\uff0c\u4eca\u5929\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528padStart\u548cSpadEnd\u65b9\u6cd5\uff0c\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u662f\u5728\u5b57\u7b26\u4e32\u7684\u5f00\u5934\u8fd8\u662f\u7ed3\u5c3e\u586b\u5145\u5b57\u7b26\u4e32\u3002<\/p>\n

\/\/ \u5728\u5f00\u5934\u6dfb\u52a0 \"0\"\uff0c\u76f4\u5230\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u4e3a 8\u3002<\/p>\n

\r\n\/\/ \u5728\u5f00\u5934\u6dfb\u52a0 \"0\"\uff0c\u76f4\u5230\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u4e3a 8\u3002 \r\nconst eightBits = '001'.padStart(8, '0') \r\nconsole.log(eightBits) \/\/ \"00000001\" \r\n \r\n\/\/\u5728\u672b\u5c3e\u6dfb\u52a0\u201c *\u201d\uff0c\u76f4\u5230\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u4e3a5\u3002 \r\nconst anonymizedCode = \"34\".padEnd(5, \"*\") \r\nconsole.log(anonymizedCode) \/\/ \"34***\" \r\n<\/pre>\n
3.\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u62c6\u5206\u4e3a\u5b57\u7b26\u6570\u7ec4<\/strong><\/div>\n

\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u6210\u5b57\u7b26\u6570\u7ec4\uff0c\u6211\u66f4\u559c\u6b22\u4f7f\u7528\u6269\u5c55\u64cd\u4f5c\u7b26(...):<\/p>\n

\r\nconst word = 'apple' \r\nconst characters = [...word] \r\nconsole.log(characters) \/\/ [\"a\", \"p\", \"p\", \"l\", \"e\"] \r\n<\/pre>\n

\u6ce8\u610f\uff0c\u8fd9\u5e76\u4e0d\u603b\u662f\u50cf\u9884\u671f\u7684\u90a3\u6837\u5de5\u4f5c\u3002\u6709\u5173\u66f4\u591a\u4fe1\u606f\uff0c\u8bf7\u53c2\u89c1\u4e0b\u4e00\u4e2a\u6280\u5de7\u3002<\/p>\n

4.\u5982\u4f55\u8ba1\u7b97\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26<\/strong><\/div>\n

\u53ef\u4ee5\u4f7f\u7528length\u5c5e\u6027\u3002<\/p>\n

\r\nconst word = \"apple\"; \r\nconsole.log(word.length) \/\/ 5 \r\n<\/pre>\n

\u4f46\u5bf9\u4e8e\u4e2d\u6587\u6765\u8bf4\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u5c31\u4e0d\u592a\u9760\u8c31\u3002<\/p>\n

\r\nconst word = \"\" \r\nconsole.log(word.length) \/\/ 2 \r\n<\/pre>\n

\u65e5\u672c\u6c49\u5b57??\u8fd4\u56delength\u4e3a2\uff0c\u4e3a\u4ec0\u4e48?JS \u5c06\u5927\u591a\u6570\u5b57\u7b26\u8868\u793a\u4e3a16\u4f4d\u4ee3\u7801\u70b9\u3002\u4f46\u662f\uff0c\u67d0\u4e9b\u5b57\u7b26\u8868\u793a\u4e3a\u4e24\u4e2a(\u6216\u66f4\u591a)16 \u4f4d\u4ee3\u7801\u70b9\uff0c\u79f0\u4e3a\u4ee3\u7406\u5bf9\u3002\u5982\u679c\u4f7f\u7528\u7684\u662flength\u5c5e\u6027\uff0cJS \u544a\u8bc9\u4f60\u4f7f\u7528\u4e86\u591a\u5c11\u4ee3\u7801\u70b9\u3002\u56e0\u6b64\uff0c??(hokke)\u7531\u4e24\u4e2a\u4ee3\u7801\u70b9\u7ec4\u6210\uff0c\u8fd4\u56de\u9519\u8bef\u7684\u503c\u3002<\/p>\n

\u90a3\u600e\u4e48\u53bb\u5224\u65ad\u5462\uff0c\u4f7f\u7528\u89e3\u6784\u64cd\u4f5c\u7b26\u53f7(...)<\/p>\n

\r\nconst word = \"\" \r\nconst characters = [...word] \r\nconsole.log(characters.length) \/\/ 1 \r\n<\/pre>\n

\u8fd9\u79cd\u65b9\u6cd5\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u90fd\u6709\u6548\uff0c\u4f46\u662f\u6709\u4e00\u4e9b\u6781\u7aef\u60c5\u51b5\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f7f\u7528\u8868\u60c5\u7b26\u53f7\uff0c\u5219\u6709\u65f6\u6b64\u957f\u5ea6\u4e5f\u662f\u9519\u8bef\u7684\u3002\u5982\u679c\u771f\u60f3\u8ba1\u7b97\u5b57\u7b26\u6b63\u786e\u957f\u5ea6\uff0c\u5219\u5fc5\u987b\u5c06\u5355\u8bcd\u5206\u89e3\u4e3a \u5b57\u7d20\u7c07(Grapheme Clusters) \uff0c\u8fd9\u8d85\u51fa\u4e86\u672c\u6587\u7684\u8303\u56f4\uff0c\u8fd9\u91cc\u5c31\u4e0d\u5728\u8fd9\u8bf4\u660e\u3002<\/p>\n

5.\u5982\u4f55\u53cd\u8f6c\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26<\/strong><\/div>\n

\u53cd\u8f6c\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\u662f\u5f88\u5bb9\u6613\u7684\u3002\u53ea\u9700\u7ec4\u5408\u6269\u5c55\u64cd\u4f5c\u7b26(...)\u3001Array.reverse\u65b9\u6cd5\u548cArray.join\u65b9\u6cd5\u3002<\/p>\n

\r\nconst word = \"apple\" \r\nconst reversedWord = [...word].reverse().join(\"\") \r\nconsole.log(reversedWord) \/\/ \"elppa\" \r\n<\/pre>\n

\u548c\u524d\u9762\u4e00\u6837\uff0c\u4e5f\u6709\u4e00\u4e9b\u8fb9\u7f18\u60c5\u51b5\u3002\u9047\u5230\u8fb9\u7f18\u7684\u60c5\u51b5\u5c31\u6709\u9700\u8981\u9996\u5148\u5c06\u5355\u8bcd\u62c6\u5206\u4e3a\u5b57\u7d20\u7c07\u3002<\/p>\n

6. \u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5b57\u6bcd\u5927\u5199<\/strong><\/div>\n

\u4e00\u4e2a\u975e\u5e38\u5e38\u89c1\u7684\u64cd\u4f5c\u662f\u5c06\u5b57\u7b26\u4e32\u7684\u7b2c\u4e00\u4e2a\u5b57\u6bcd\u5927\u5199\u3002\u867d\u7136\u8bb8\u591a\u7f16\u7a0b\u8bed\u8a00\u90fd\u6709\u4e00\u79cd\u672c\u5730\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff0c\u4f46 JS \u9700\u8981\u505a\u4e00\u4e9b\u5de5\u4f5c\u3002<\/p>\n

\r\nlet word = 'apply' \r\nword = word[0].toUpperCase() + word.substr(1) \r\nconsole.log(word) \/\/ \"Apple\" \r\n<\/pre>\n

\u53e6\u4e00\u79cd\u65b9\u6cd5:<\/p>\n

\r\n\/\/ This shows an alternative way \r\nlet word = \"apple\"; \r\n \r\n\/\/ \u4f7f\u7528\u6269\u5c55\u8fd0\u7b97\u7b26\uff08`...`\uff09\u62c6\u5206\u4e3a\u5b57\u7b26 \r\n \r\nconst characters = [...word]; \r\ncharacters[0] = characters[0].toUpperCase(); \r\nword = characters.join(\"\"); \r\n \r\nconsole.log(word); \/\/ \"Apple\" \r\n<\/pre>\n
7.\u5982\u4f55\u5728\u591a\u4e2a\u5206\u9694\u7b26\u4e0a\u5206\u5272\u5b57\u7b26\u4e32<\/strong><\/div>\n

\u5047\u8bbe\u6211\u4eec\u8981\u5728\u5206\u9694\u7b26\u4e0a\u5206\u5272\u5b57\u7b26\u4e32\uff0c\u7b2c\u4e00\u60f3\u5230\u7684\u5c31\u662f\u4f7f\u7528split\u65b9\u6cd5\uff0c\u8fd9\u70b9\uff0c\u667a\u7c73\u4eec\u80af\u5b9a\u77e5\u9053\u3002\u4f46\u662f\uff0c\u6709\u4e00\u70b9\u5927\u5bb6\u53ef\u80fd\u4e0d\u77e5\u9053\uff0c\u5c31\u662fsplit\u53ef\u4ee5\u540c\u65f6\u62c6\u5206\u591a\u4e2a\u5206\u9694\u7b26, \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5c31\u53ef\u4ee5\u5b9e\u73b0\uff1a<\/p>\n

\r\n\/\/ \u7528\u9017\u53f7(,)\u548c\u5206\u53f7(;)\u5206\u5f00\u3002 \r\n \r\nconst list = \"apples,bananas;cherries\" \r\nconst fruits = list.split(\/[,;]\/) \r\nconsole.log(fruits); \/\/ [\"apples\", \"bananas\", \"cherries\"] \r\n<\/pre>\n
8.\u5982\u4f55\u68c0\u67e5\u5b57\u7b26\u4e32\u662f\u5426\u5305\u542b\u7279\u5b9a\u5e8f\u5217<\/strong><\/div>\n

\u5b57\u7b26\u4e32\u641c\u7d22\u662f\u4e00\u9879\u5e38\u89c1\u7684\u4efb\u52a1\u3002\u5728 JS \u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528String.includes\u65b9\u6cd5\u8f7b\u677e\u5b8c\u6210\u6b64\u64cd\u4f5c\u3002\u4e0d\u9700\u8981\u6b63\u5219\u8868\u8fbe\u5f0f\u3002<\/p>\n

\r\nconst text = \"Hello, world! My name is Kai!\" \r\nconsole.log(text.includes(\"Kai\")); \/\/ true \r\n<\/pre>\n
9.\u5982\u4f55\u68c0\u67e5\u5b57\u7b26\u4e32\u662f\u5426\u4ee5\u7279\u5b9a\u5e8f\u5217\u5f00\u5934\u6216\u7ed3\u5c3e<\/strong><\/div>\n

\u5728\u5b57\u7b26\u4e32\u7684\u5f00\u5934\u6216\u7ed3\u5c3e\u8fdb\u884c\u641c\u7d22\uff0c\u53ef\u4ee5\u4f7f\u7528String.startsWith\u548cString.endsWith\u65b9\u6cd5\u3002<\/p>\n

\r\nconst text = \"Hello, world! My name is Kai!\" \r\n \r\nconsole.log(text.startsWith(\"Hello\")); \/\/ true \r\n \r\nconsole.log(text.endsWith(\"world\")); \/\/ false \r\n<\/pre>\n
10.\u5982\u4f55\u66ff\u6362\u6240\u6709\u51fa\u73b0\u7684\u5b57\u7b26\u4e32<\/strong><\/div>\n

\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u66ff\u6362\u6240\u6709\u51fa\u73b0\u7684\u5b57\u7b26\u4e32\u3002\u53ef\u4ee5\u4f7f\u7528String.replace\u65b9\u6cd5\u548c\u5e26\u6709\u5168\u5c40\u6807\u5fd7\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\u6216\u8005\uff0c\u53ef\u4ee5\u4f7f\u7528\u65b0\u7684String.replaceAll\u65b9\u6cd5\u3002\u8bf7\u6ce8\u610f\uff0c\u5e76\u975e\u5728\u6240\u6709\u6d4f\u89c8\u5668\u548cNode.js \u7248\u672c\u4e2d\u90fd\u53ef\u7528\u6b64\u65b0\u65b9\u6cd5\u3002<\/p>\n

\r\nconst text = \"I like apples. You like apples.\" \r\n \r\nconsole.log(text.replace(\/apples\/g, \"bananas\")); \r\n\/\/ \"I like bananas. You like bananas.\" \r\n \r\nconsole.log(text.replaceAll(\"apples\", \"bananas\")); \r\n\/\/ \"I lik \r\n<\/pre>\n
\u603b\u7ed3<\/strong><\/div>\n

\u5b57\u7b26\u4e32\u662f\u51e0\u4e4e\u6240\u6709\u7f16\u7a0b\u8bed\u8a00\u4e2d\u6700\u57fa\u672c\u7684\u6570\u636e\u7c7b\u578b\u4e4b\u4e00\u3002\u540c\u65f6\uff0c\u5b83\u4e5f\u662f\u65b0\u5f00\u53d1\u4eba\u5458\u5b66\u4e60\u7684\u6700\u65e9\u7684\u6570\u636e\u7c7b\u578b\u4e4b\u4e00\u3002\u7136\u800c\uff0c\u5c24\u5176\u662f\u5728JavaScript\u4e2d\uff0c\u8bb8\u591a\u5f00\u53d1\u4eba\u5458\u5e76\u4e0d\u77e5\u9053\u5173\u4e8e\u5b57\u7b26\u4e32\u7684\u4e00\u4e9b\u6709\u8da3\u7684\u7ec6\u8282\u3002\u5e0c\u671b\u6b64\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u6211\u4eec\u79f0\u4e00\u4e2a\u5b57\u7b26\u5e8f\u5217\u4e3a\u5b57\u7b26\u4e32\u3002\u8fd9\u51e0\u4e4e\u662f\u6240\u6709\u7f16\u7a0b\u8bed\u8a00\u4e2d\u90fd\u6709\u7684\u57fa\u672c\u7c7b\u578b\u4e4b\u4e00\u3002\u8fd9\u91cc\u8ddf\u5927\u5bb6\u5c55\u793a\u5173\u4e8e JS \u5b57\u7b26\u4e32\u768410\u4e2a […]<\/p>\n","protected":false},"author":1898,"featured_media":208514,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[631],"class_list":["post-208511","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread","tag-javascript"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/208511","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1898"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=208511"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/208511\/revisions"}],"predecessor-version":[{"id":208516,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/208511\/revisions\/208516"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/208514"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=208511"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=208511"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=208511"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}