{"id":305568,"date":"2024-05-20T19:49:38","date_gmt":"2024-05-20T11:49:38","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/305568.html"},"modified":"2024-05-20T19:49:51","modified_gmt":"2024-05-20T11:49:51","slug":"javascript-%e9%a1%b9%e7%9b%ae%e4%b8%ad%e5%a6%82%e4%bd%95%e7%94%a8-substr-%e6%88%aa%e5%8f%96%e5%ad%97%e7%ac%a6%e4%b8%b2-3","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/305568.html","title":{"rendered":"javascript \u9879\u76ee\u4e2d\u5982\u4f55\u7528 substr \u622a\u53d6\u5b57\u7b26\u4e32"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26221847\/098e71be-fc5b-4d6e-b762-a64aee1327f9.webp\" alt=\"javascript \u9879\u76ee\u4e2d\u5982\u4f55\u7528 substr \u622a\u53d6\u5b57\u7b26\u4e32\" \/><\/p>\n<p><p>\u5728JavaScript\u9879\u76ee\u4e2d\uff0c\u4f7f\u7528<code>substr<\/code>\u65b9\u6cd5\u622a\u53d6\u5b57\u7b26\u4e32\u662f\u4e00\u79cd\u57fa\u672c\u4e14\u9ad8\u6548\u7684\u64cd\u4f5c\u3002\u5b83\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u4ece\u4e00\u4e2a\u4e3b\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u51fa\u7279\u5b9a\u957f\u5ea6\u7684\u67d0\u4e00\u90e8\u5206\u5b57\u7b26\u4e32\u3002<strong><code>substr<\/code>\u7684\u6838\u5fc3\u5728\u4e8e\u5b83\u7684\u4e24\u4e2a\u53c2\u6570\uff1a\u8d77\u59cb\u4f4d\u7f6e\u548c\u957f\u5ea6<\/strong>\u3002\u8fd9\u610f\u5473\u7740\uff0c\u901a\u8fc7\u6307\u5b9a\u4e00\u4e2a\u8d77\u59cb\u7d22\u5f15\uff0c\u5e76\u5b9a\u4e49\u8981\u622a\u53d6\u7684\u5b57\u7b26\u6570\u91cf\uff0c\u6211\u4eec\u5c31\u80fd\u591f\u7075\u6d3b\u5730\u4ece\u4efb\u4f55\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u83b7\u53d6\u6240\u9700\u7684\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><p>\u8be6\u7ec6\u6765\u8bf4\uff0c<strong>\u8d77\u59cb\u4f4d\u7f6e<\/strong>\u662f<code>substr<\/code>\u65b9\u6cd5\u4e2d\u7b2c\u4e00\u4e2a\u53c2\u6570\uff0c\u5b83\u51b3\u5b9a\u4e86\u4ece\u54ea\u4e2a\u5b57\u7b26\u5f00\u59cb\u622a\u53d6\u3002\u8fd9\u4e2a\u4f4d\u7f6e\u662f\u57fa\u4e8e\u7d22\u5f15\u7684\uff0c\u5176\u4e2d\u5b57\u7b26\u4e32\u7684\u7b2c\u4e00\u4e2a\u5b57\u7b26\u7d22\u5f15\u4e3a0\u3002\u8fd9\u4e2a\u7279\u6027\u8ba9\u6211\u4eec\u80fd\u591f\u51c6\u786e\u5b9a\u4f4d\u5f00\u59cb\u622a\u53d6\u7684\u4f4d\u7f6e\uff0c\u662f\u4f7f\u7528<code>substr<\/code>\u65b9\u6cd5\u65f6\u9700\u8981\u91cd\u70b9\u638c\u63e1\u7684\u4e00\u4e2a\u6982\u5ff5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001SUBSTR\u65b9\u6cd5\u7684\u57fa\u672c\u4f7f\u7528<\/h3>\n<\/p>\n<p><p><code>substr<\/code>\u65b9\u6cd5\u80fd\u591f\u4ece\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u622a\u53d6\u6307\u5b9a\u957f\u5ea6\u7684\u5b57\u7b26\u3002\u5b83\u7684\u57fa\u672c\u8bed\u6cd5\u662f<code>str.substr(start[, length])<\/code>\uff0c\u5176\u4e2d<code>str<\/code>\u662f\u5f85\u5904\u7406\u7684\u5b57\u7b26\u4e32\uff0c<code>start<\/code>\u662f\u8d77\u59cb\u7d22\u5f15\uff08\u4ece0\u5f00\u59cb\u8ba1\u7b97\uff09\uff0c<code>length<\/code>\u662f\u53ef\u9009\u7684\uff0c\u8868\u793a\u9700\u8981\u622a\u53d6\u7684\u5b57\u7b26\u7684\u6570\u91cf\u3002\u5982\u679c<code>length<\/code>\u672a\u5b9a\u4e49\uff0c\u5219\u9ed8\u8ba4\u622a\u53d6\u4ece<code>start<\/code>\u5f00\u59cb\u5230\u5b57\u7b26\u4e32\u672b\u5c3e\u7684\u6240\u6709\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528\u793a\u4f8b<\/h4>\n<\/p>\n<p><pre><code class=\"language-javascript\">let text = &quot;Hello, World!&quot;;<\/p>\n<p>let segment = text.substr(7, 5);<\/p>\n<p>console.log(segment); \/\/ \u8f93\u51fa: World<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>substr<\/code>\u4ece\u7d22\u5f157\uff08\u5373&quot;W&quot;\u6240\u5728\u4f4d\u7f6e\uff09\u5f00\u59cb\u622a\u53d6\uff0c\u957f\u5ea6\u4e3a5\uff0c\u56e0\u6b64\u5f97\u5230\u7684\u7ed3\u679c\u662f&quot;World&quot;\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5904\u7406\u8d1f\u503c\u53c2\u6570<\/h3>\n<\/p>\n<p><p><code>substr<\/code>\u65b9\u6cd5\u5bf9\u8d1f\u503c\u7684<code>start<\/code>\u53c2\u6570\u6709\u7279\u522b\u7684\u5904\u7406\u3002\u5f53<code>start<\/code>\u4e3a\u8d1f\u503c\u65f6\uff0c\u5b83\u4f1a\u4ece\u5b57\u7b26\u4e32\u7684\u672b\u5c3e\u5411\u524d\u8ba1\u6570\u3002\u8fd9\u662f<code>substr<\/code>\u4e0e\u5176\u4ed6\u5b57\u7b26\u4e32\u64cd\u4f5c\u65b9\u6cd5\u4e0d\u540c\u4e4b\u5904\u4e4b\u4e00\uff0c\u589e\u52a0\u4e86\u5b83\u7684\u7075\u6d3b\u6027\u3002<\/p>\n<\/p>\n<p><h4>\u8d1f\u503c\u793a\u4f8b<\/h4>\n<\/p>\n<p><pre><code class=\"language-javascript\">let text = &quot;JavaScript is fun&quot;;<\/p>\n<p>let segment = text.substr(-3, 3);<\/p>\n<p>console.log(segment); \/\/ \u8f93\u51fa: fun<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>start<\/code>\u53c2\u6570\u4e3a-3\uff0c\u8868\u793a\u4ece\u5b57\u7b26\u4e32\u672b\u5c3e\u5012\u6570\u7b2c\u4e09\u4e2a\u5b57\u7b26\u5f00\u59cb\u622a\u53d6\uff0c\u957f\u5ea6\u4e3a3\uff0c\u56e0\u6b64\u83b7\u53d6\u5230\u7684\u5b57\u7b26\u4e32\u662f&quot;fun&quot;\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u7684JavaScript\u9879\u76ee\u4e2d\uff0c\u4f7f\u7528<code>substr<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5904\u7406\u591a\u79cd\u5b57\u7b26\u4e32\u64cd\u4f5c\u573a\u666f\uff0c\u5982\u4ece\u590d\u6742\u7684\u6587\u672c\u6570\u636e\u4e2d\u63d0\u53d6\u5173\u952e\u4fe1\u606f\u3001\u5904\u7406\u7528\u6237\u8f93\u5165\u3001\u751f\u6210\u6458\u8981\u7b49\u3002<\/p>\n<\/p>\n<p><h4>\u7528\u6237\u8f93\u5165\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5728\u7528\u6237\u8f93\u5165\u5904\u7406\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u622a\u53d6\u5b57\u7b26\u4e32\u6765\u6ee1\u8db3\u7279\u5b9a\u683c\u5f0f\u8981\u6c42\u6216\u9a8c\u8bc1\u3002\u4f8b\u5982\uff0c\u4ece\u7528\u6237\u63d0\u4f9b\u7684\u7535\u8bdd\u53f7\u7801\u4e2d\u53bb\u9664\u56fd\u5bb6\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function stripCountryCode(phoneNumber) {<\/p>\n<p>  if(phoneNumber.startsWith(&#039;+&#039;)) {<\/p>\n<p>    return phoneNumber.substr(3);<\/p>\n<p>  }<\/p>\n<p>  return phoneNumber;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u751f\u6210\u6587\u672c\u6458\u8981<\/h4>\n<\/p>\n<p><p>\u5728\u663e\u793a\u6587\u7ae0\u5217\u8868\u6216\u6d88\u606f\u9884\u89c8\u65f6\uff0c\u7ecf\u5e38\u9700\u8981\u622a\u53d6\u6587\u672c\u7684\u4e00\u90e8\u5206\u4f5c\u4e3a\u6458\u8981\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function generateSummary(content, length = 100) {<\/p>\n<p>  return content.length &gt; length ? content.substr(0, length) + &quot;...&quot; : content;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5c0f\u7ed3<\/h3>\n<\/p>\n<p><p><code>substr<\/code>\u662fJavaScript\u4e2d\u5904\u7406\u5b57\u7b26\u4e32\u7684\u5f3a\u5927\u5de5\u5177\u4e4b\u4e00\uff0c\u901a\u8fc7\u5b83\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u957f\u5ea6\u53c2\u6570\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u622a\u53d6\u5b57\u7b26\u4e32\u7684\u4efb\u610f\u90e8\u5206\u3002\u867d\u7136\u5728\u6700\u65b0\u7684ECMAScript\u6807\u51c6\u4e2d\uff0c<code>substr<\/code>\u65b9\u6cd5\u88ab\u63a8\u8350\u7528\u66f4\u73b0\u4ee3\u7684<code>slice<\/code>\u65b9\u6cd5\u4ee3\u66ff\uff0c\u4f46\u5728\u5f88\u591a\u9057\u7559\u9879\u76ee\u548c\u7279\u5b9a\u573a\u666f\u4e0b\uff0c<code>substr<\/code>\u4f9d\u7136\u662f\u5904\u7406\u5b57\u7b26\u4e32\u4e0d\u53ef\u6216\u7f3a\u7684\u65b9\u6cd5\u3002\u7406\u89e3\u5b83\u7684\u5de5\u4f5c\u539f\u7406\u53ca\u5e94\u7528\u573a\u666f\u5bf9\u4e8e\u4efb\u4f55JavaScript\u5f00\u53d1\u8005\u6765\u8bf4\u90fd\u662f\u4e00\u9879\u5b9d\u8d35\u7684\u6280\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u5982\u4f55\u5728 JavaScript \u9879\u76ee\u4e2d\u4f7f\u7528 substr \u65b9\u6cd5\u8fdb\u884c\u5b57\u7b26\u4e32\u622a\u53d6\uff1f<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528 JavaScript \u4e2d\u7684 <code>substr<\/code> \u65b9\u6cd5\u6765\u622a\u53d6\u5b57\u7b26\u4e32\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u5728 JavaScript \u9879\u76ee\u4e2d\u4f7f\u7528 <code>substr<\/code> \u65b9\u6cd5\u8fdb\u884c\u5b57\u7b26\u4e32\u622a\u53d6\u7684\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528 substr \u65b9\u6cd5\u7684\u8bed\u6cd5\u662f\u4ec0\u4e48\uff1f<\/strong><\/p>\n<p><code>substr<\/code> \u65b9\u6cd5\u7684\u8bed\u6cd5\u662f\uff1a<code>str.substr(start, length)<\/code>\uff0c\u5176\u4e2d <code>start<\/code> \u662f\u8981\u5f00\u59cb\u622a\u53d6\u7684\u4f4d\u7f6e\uff0c<code>length<\/code> \u662f\u8981\u622a\u53d6\u7684\u957f\u5ea6\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60f3\u8981\u4ece\u5b57\u7b26\u4e32\u7684\u7b2c\u4e09\u4e2a\u5b57\u7b26\u5f00\u59cb\u622a\u53d6\u4e94\u4e2a\u5b57\u7b26\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>str.substr(3, 5)<\/code>\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u5982\u4f55\u622a\u53d6\u5b57\u7b26\u4e32\u7684\u524d\u51e0\u4e2a\u5b57\u7b26\uff1f<\/strong><\/p>\n<p>\u5982\u679c\u60f3\u8981\u622a\u53d6\u5b57\u7b26\u4e32\u7684\u524d\u51e0\u4e2a\u5b57\u7b26\uff0c\u53ef\u4ee5\u5c06 <code>start<\/code> \u53c2\u6570\u8bbe\u7f6e\u4e3a 0\uff0c<code>length<\/code> \u53c2\u6570\u8bbe\u7f6e\u4e3a\u9700\u8981\u622a\u53d6\u7684\u5b57\u7b26\u6570\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60f3\u8981\u622a\u53d6\u5b57\u7b26\u4e32\u7684\u524d\u4e09\u4e2a\u5b57\u7b26\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>str.substr(0, 3)<\/code>\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u5982\u4f55\u622a\u53d6\u5b57\u7b26\u4e32\u7684\u540e\u51e0\u4e2a\u5b57\u7b26\uff1f<\/strong><\/p>\n<p>\u5982\u679c\u60f3\u8981\u622a\u53d6\u5b57\u7b26\u4e32\u7684\u540e\u51e0\u4e2a\u5b57\u7b26\uff0c\u53ef\u4ee5\u8ba1\u7b97\u5b57\u7b26\u4e32\u7684\u957f\u5ea6 <code>str.length<\/code>\uff0c\u7136\u540e\u5c06 <code>start<\/code> \u53c2\u6570\u8bbe\u7f6e\u4e3a\u9700\u8981\u622a\u53d6\u7684\u5b57\u7b26\u7684\u8d77\u59cb\u4f4d\u7f6e\uff0c<code>length<\/code> \u53c2\u6570\u8bbe\u7f6e\u4e3a\u9700\u8981\u622a\u53d6\u7684\u5b57\u7b26\u6570\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60f3\u8981\u622a\u53d6\u5b57\u7b26\u4e32\u7684\u540e\u4e09\u4e2a\u5b57\u7b26\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>str.substr(str.length - 3, 3)<\/code>\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u5982\u4f55\u622a\u53d6\u5b57\u7b26\u4e32\u4e2d\u95f4\u7684\u4e00\u6bb5\u5b57\u7b26\uff1f<\/strong><\/p>\n<p>\u5982\u679c\u60f3\u8981\u622a\u53d6\u5b57\u7b26\u4e32\u4e2d\u95f4\u7684\u4e00\u6bb5\u5b57\u7b26\uff0c\u53ef\u4ee5\u5c06 <code>start<\/code> \u53c2\u6570\u8bbe\u7f6e\u4e3a\u9700\u8981\u622a\u53d6\u7684\u5b57\u7b26\u7684\u8d77\u59cb\u4f4d\u7f6e\uff0c<code>length<\/code> \u53c2\u6570\u8bbe\u7f6e\u4e3a\u9700\u8981\u622a\u53d6\u7684\u5b57\u7b26\u6570\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60f3\u8981\u622a\u53d6\u5b57\u7b26\u4e32\u7684\u7b2c\u4e8c\u4e2a\u5b57\u7b26\u5230\u7b2c\u4e94\u4e2a\u5b57\u7b26\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>str.substr(1, 4)<\/code>\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u5982\u4f55\u5224\u65ad\u622a\u53d6\u7684\u5b57\u7b26\u4e32\u662f\u5426\u8d85\u51fa\u4e86\u539f\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\uff1f<\/strong><\/p>\n<p>\u5728\u4f7f\u7528 <code>substr<\/code> \u65b9\u6cd5\u622a\u53d6\u5b57\u7b26\u4e32\u4e4b\u524d\uff0c\u53ef\u4ee5\u5148\u68c0\u67e5\u622a\u53d6\u7684\u8d77\u59cb\u4f4d\u7f6e\u548c\u622a\u53d6\u7684\u957f\u5ea6\u662f\u5426\u4f1a\u8d85\u51fa\u539f\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\uff0c\u4ee5\u907f\u514d\u51fa\u73b0\u9519\u8bef\u3002\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u6216\u8005\u4f7f\u7528\u5b57\u7b26\u4e32\u7684 <code>length<\/code> \u5c5e\u6027\u6765\u5224\u65ad\u3002<\/p>\n<\/li>\n<li>\n<p><strong>substr \u548c substring \u65b9\u6cd5\u6709\u4ec0\u4e48\u533a\u522b\uff1f<\/strong><\/p>\n<p><code>substr<\/code> \u65b9\u6cd5\u548c <code>substring<\/code> \u65b9\u6cd5\u90fd\u53ef\u4ee5\u7528\u6765\u622a\u53d6\u5b57\u7b26\u4e32\uff0c\u4f46\u662f\u4e24\u8005\u7684\u7528\u6cd5\u548c\u542b\u4e49\u6709\u6240\u4e0d\u540c\u3002<code>substr<\/code> \u65b9\u6cd5\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u8868\u793a\u622a\u53d6\u7684\u957f\u5ea6\uff0c\u800c <code>substring<\/code> \u65b9\u6cd5\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u8868\u793a\u622a\u53d6\u7684\u7ed3\u675f\u4f4d\u7f6e\u3002\u6b64\u5916\uff0c<code>substr<\/code> \u65b9\u6cd5\u8fd8\u53ef\u4ee5\u63a5\u53d7\u8d1f\u6570\u4f5c\u4e3a\u53c2\u6570\uff0c\u8868\u793a\u4ece\u5b57\u7b26\u4e32\u7684\u672b\u5c3e\u5f00\u59cb\u622a\u53d6\u3002<\/p>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"\u5728JavaScript\u9879\u76ee\u4e2d\uff0c\u4f7f\u7528substr\u65b9\u6cd5\u622a\u53d6\u5b57\u7b26\u4e32\u662f\u4e00\u79cd\u57fa\u672c\u4e14\u9ad8\u6548\u7684\u64cd\u4f5c\u3002\u5b83\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u4ece\u4e00\u4e2a\u4e3b\u5b57\u7b26 [&hellip;]","protected":false},"author":3,"featured_media":305587,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/305568"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=305568"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/305568\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/305587"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=305568"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=305568"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=305568"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}