{"id":2029,"date":"2025-02-25T22:35:32","date_gmt":"2025-02-25T14:35:32","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2029"},"modified":"2025-02-25T22:35:32","modified_gmt":"2025-02-25T14:35:32","slug":"javascript-%e5%ad%97%e7%ac%a6%e4%b8%b2","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/02\/25\/javascript-%e5%ad%97%e7%ac%a6%e4%b8%b2\/","title":{"rendered":"JavaScript \u5b57\u7b26\u4e32"},"content":{"rendered":"\n<p>\u5728 JavaScript \u4e2d\uff0c<strong>\u5b57\u7b26\u4e32\uff08String\uff09<\/strong> \u662f\u4e00\u7ec4\u5b57\u7b26\u7684\u96c6\u5408\uff0c\u53ef\u4ee5\u7528\u6765\u8868\u793a\u6587\u672c\u6570\u636e\u3002\u5b57\u7b26\u4e32\u662f\u5e38\u7528\u7684\u6570\u636e\u7c7b\u578b\u4e4b\u4e00\uff0cJavaScript \u63d0\u4f9b\u4e86\u8bb8\u591a\u5185\u7f6e\u65b9\u6cd5\u6765\u64cd\u4f5c\u5b57\u7b26\u4e32\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>\u521b\u5efa\u5b57\u7b26\u4e32<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">a. <strong>\u4f7f\u7528\u5355\u5f15\u53f7\u6216\u53cc\u5f15\u53f7<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>let str1 = 'Hello, World!';  \/\/ \u4f7f\u7528\u5355\u5f15\u53f7\nlet str2 = \"Hello, World!\";  \/\/ \u4f7f\u7528\u53cc\u5f15\u53f7<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">b. <strong>\u4f7f\u7528\u6a21\u677f\u5b57\u9762\u91cf\uff08Template Literals\uff09<\/strong><\/h4>\n\n\n\n<p>\u6a21\u677f\u5b57\u9762\u91cf\u7528\u53cd\u5f15\u53f7\uff08<code>`<\/code>\uff09\u5305\u88f9\uff0c\u53ef\u4ee5\u5728\u5b57\u7b26\u4e32\u4e2d\u5d4c\u5165\u53d8\u91cf\u6216\u8868\u8fbe\u5f0f\uff0c\u652f\u6301\u591a\u884c\u5b57\u7b26\u4e32\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>let name = \"Alice\";\nlet greeting = `Hello, ${name}!`;  \/\/ \u63d2\u503c\nconsole.log(greeting);  \/\/ \u8f93\u51fa: Hello, Alice!\n\nlet multiLineString = `This is\na multi-line\nstring.`;\nconsole.log(multiLineString);  \/\/ \u8f93\u51fa: This is\n                                \/\/       a multi-line\n                                \/\/       string.<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>\u5b57\u7b26\u4e32\u7684\u5e38\u7528\u65b9\u6cd5<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">a. <strong>\u5b57\u7b26\u4e32\u957f\u5ea6<\/strong>\uff1a\u4f7f\u7528 <code>.length<\/code> \u5c5e\u6027\u53ef\u4ee5\u83b7\u53d6\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u3002<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>let str = \"Hello, World!\";\nconsole.log(str.length);  \/\/ \u8f93\u51fa: 13<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">b. <strong>\u8bbf\u95ee\u5b57\u7b26<\/strong>\uff1a\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\u3002\u7d22\u5f15\u4ece 0 \u5f00\u59cb\u3002<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>let str = \"Hello\";\nconsole.log(str&#91;0]);  \/\/ \u8f93\u51fa: H\nconsole.log(str&#91;4]);  \/\/ \u8f93\u51fa: o<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">c. <strong>\u5b57\u7b26\u4e32\u62fc\u63a5<\/strong>\uff1a\u4f7f\u7528 <code>+<\/code> \u6216 <code>.concat()<\/code> \u65b9\u6cd5\u8fde\u63a5\u5b57\u7b26\u4e32\u3002<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>let str1 = \"Hello\";\nlet str2 = \"World!\";\nlet result = str1 + \" \" + str2;  \/\/ \u4f7f\u7528 + \u62fc\u63a5\nconsole.log(result);  \/\/ \u8f93\u51fa: Hello World!\n\n\/\/ \u4f7f\u7528 concat() \u62fc\u63a5\nlet result2 = str1.concat(\" \", str2);\nconsole.log(result2);  \/\/ \u8f93\u51fa: Hello World!<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">d. <strong>\u67e5\u627e\u5b50\u5b57\u7b26\u4e32<\/strong>\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>indexOf()<\/strong>\uff1a\u67e5\u627e\u5b50\u5b57\u7b26\u4e32\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\u4f4d\u7f6e\u3002<\/li>\n\n\n\n<li><strong>lastIndexOf()<\/strong>\uff1a\u67e5\u627e\u5b50\u5b57\u7b26\u4e32\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u4f4d\u7f6e\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>let str = \"Hello, World!\";\nconsole.log(str.indexOf(\"World\"));  \/\/ \u8f93\u51fa: 7\nconsole.log(str.lastIndexOf(\"o\"));  \/\/ \u8f93\u51fa: 8<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">e. <strong>\u63d0\u53d6\u5b50\u5b57\u7b26\u4e32<\/strong>\uff1a\u4f7f\u7528 <code>slice()<\/code>\u3001<code>substring()<\/code> \u6216 <code>substr()<\/code> \u63d0\u53d6\u90e8\u5206\u5b57\u7b26\u4e32\u3002<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>let str = \"Hello, World!\";\nconsole.log(str.slice(0, 5));  \/\/ \u8f93\u51fa: Hello\nconsole.log(str.substring(7, 12));  \/\/ \u8f93\u51fa: World\nconsole.log(str.substr(7, 5));  \/\/ \u8f93\u51fa: World<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">f. <strong>\u8f6c\u6362\u5927\u5c0f\u5199<\/strong>\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>let str = \"Hello, World!\";\nconsole.log(str.toUpperCase());  \/\/ \u8f93\u51fa: HELLO, WORLD!\nconsole.log(str.toLowerCase());  \/\/ \u8f93\u51fa: hello, world!<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">g. <strong>\u66ff\u6362\u5185\u5bb9<\/strong>\uff1a\u4f7f\u7528 <code>replace()<\/code> \u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u90e8\u5206\u5185\u5bb9\u3002<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>let str = \"Hello, World!\";\nlet newStr = str.replace(\"World\", \"JavaScript\");\nconsole.log(newStr);  \/\/ \u8f93\u51fa: Hello, JavaScript!<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">h. <strong>\u5206\u5272\u5b57\u7b26\u4e32<\/strong>\uff1a\u4f7f\u7528 <code>split()<\/code> \u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u6309\u6307\u5b9a\u7684\u5206\u9694\u7b26\u62c6\u5206\u6210\u6570\u7ec4\u3002<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>let str = \"apple,banana,orange\";\nlet fruits = str.split(\",\");\nconsole.log(fruits);  \/\/ \u8f93\u51fa: &#91;\"apple\", \"banana\", \"orange\"]<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">i. <strong>\u53bb\u9664\u7a7a\u767d\u5b57\u7b26<\/strong>\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>trim()<\/strong>\uff1a\u53bb\u9664\u5b57\u7b26\u4e32\u4e24\u7aef\u7684\u7a7a\u767d\u5b57\u7b26\u3002<\/li>\n\n\n\n<li><strong>trimStart()<\/strong> \u548c <strong>trimEnd()<\/strong>\uff1a\u5206\u522b\u53bb\u9664\u5b57\u7b26\u4e32\u5f00\u59cb\u548c\u7ed3\u675f\u7684\u7a7a\u767d\u5b57\u7b26\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>let str = \"  Hello, World!  \";\nconsole.log(str.trim());  \/\/ \u8f93\u51fa: Hello, World!<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">j. <strong>\u5b57\u7b26\u66ff\u6362\u4e0e\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong>\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>let str = \"Hello 123, Hello 456!\";\nlet result = str.replace(\/\\d+\/g, \"*\");\nconsole.log(result);  \/\/ \u8f93\u51fa: Hello *, Hello *!<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>\u5b57\u7b26\u4e32\u6a21\u677f\u4e0e\u8868\u8fbe\u5f0f<\/strong><\/h3>\n\n\n\n<p>\u6a21\u677f\u5b57\u9762\u91cf\uff08Template Literals\uff09\u4f7f\u5f97\u63d2\u5165\u53d8\u91cf\u6216\u8868\u8fbe\u5f0f\u53d8\u5f97\u975e\u5e38\u7b80\u4fbf\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>let name = \"John\";\nlet age = 30;\nlet introduction = `My name is ${name}, and I am ${age} years old.`;\nconsole.log(introduction);  \/\/ \u8f93\u51fa: My name is John, and I am 30 years old.<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>\u591a\u884c\u5b57\u7b26\u4e32<\/strong><\/h3>\n\n\n\n<p>\u4f7f\u7528\u6a21\u677f\u5b57\u9762\u91cf\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u521b\u5efa\u591a\u884c\u5b57\u7b26\u4e32\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>let text = `This is a multi-line string.\nIt can span across multiple lines.\nJust like this one!`;\nconsole.log(text);\n\/\/ \u8f93\u51fa:\n\/\/ This is a multi-line string.\n\/\/ It can span across multiple lines.\n\/\/ Just like this one!<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">5. <strong>\u7f16\u7801\u4e0e\u89e3\u7801<\/strong><\/h3>\n\n\n\n<p>JavaScript \u63d0\u4f9b\u4e86 <code>encodeURIComponent()<\/code> \u548c <code>decodeURIComponent()<\/code> \u51fd\u6570\u6765\u5bf9 URL \u4e2d\u7684\u5b57\u7b26\u4e32\u8fdb\u884c\u7f16\u7801\u548c\u89e3\u7801\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>let url = \"https:\/\/www.example.com?search=JavaScript &amp; more\";\nlet encodedUrl = encodeURIComponent(url);\nconsole.log(encodedUrl);  \/\/ \u8f93\u51fa: https%3A%2F%2Fwww.example.com%3Fsearch%3DJavaScript%20%26%20more\n\nlet decodedUrl = decodeURIComponent(encodedUrl);\nconsole.log(decodedUrl);  \/\/ \u8f93\u51fa: https:\/\/www.example.com?search=JavaScript &amp; more<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">6. <strong>\u5b57\u7b26\u4e32\u7684\u4e0d\u53ef\u53d8\u6027<\/strong><\/h3>\n\n\n\n<p>JavaScript \u4e2d\u7684\u5b57\u7b26\u4e32\u662f<strong>\u4e0d\u53ef\u53d8\u7684<\/strong>\uff0c\u8fd9\u610f\u5473\u7740\u4e00\u65e6\u521b\u5efa\u4e86\u5b57\u7b26\u4e32\uff0c\u5176\u5185\u5bb9\u4e0d\u80fd\u88ab\u6539\u53d8\u3002\u6bcf\u6b21\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u4fee\u6539\u64cd\u4f5c\u65f6\uff0c\u90fd\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>let str = \"Hello\";\nstr&#91;0] = \"h\";  \/\/ \u65e0\u6548\nconsole.log(str);  \/\/ \u8f93\u51fa: Hello<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5b57\u7b26\u4e32\u662f JavaScript \u4e2d\u5e38\u7528\u7684\u6570\u636e\u7c7b\u578b\uff0c\u53ef\u4ee5\u901a\u8fc7\u4e0d\u540c\u65b9\u5f0f\u521b\u5efa\u548c\u64cd\u4f5c\u3002<\/li>\n\n\n\n<li>JavaScript \u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5904\u7406\u5b57\u7b26\u4e32\uff0c\u5982\u67e5\u627e\u3001\u66ff\u6362\u3001\u62fc\u63a5\u3001\u5927\u5c0f\u5199\u8f6c\u6362\u3001\u63d0\u53d6\u5b50\u5b57\u7b26\u4e32\u7b49\u3002<\/li>\n\n\n\n<li>\u5b57\u7b26\u4e32\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u5373\u65e0\u6cd5\u76f4\u63a5\u6539\u53d8\u5df2\u6709\u5b57\u7b26\u4e32\u7684\u5185\u5bb9\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 JavaScript \u4e2d\uff0c\u5b57\u7b26\u4e32\uff08String\uff09 \u662f\u4e00\u7ec4\u5b57\u7b26\u7684\u96c6\u5408\uff0c\u53ef\u4ee5\u7528\u6765\u8868\u793a\u6587\u672c\u6570\u636e\u3002\u5b57\u7b26\u4e32\u662f\u5e38\u7528\u7684\u6570 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[],"class_list":["post-2029","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2029","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2029"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2029\/revisions"}],"predecessor-version":[{"id":2030,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2029\/revisions\/2030"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2029"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2029"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2029"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}