{"id":169208,"date":"2024-05-08T16:40:59","date_gmt":"2024-05-08T08:40:59","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/169208.html"},"modified":"2024-05-08T16:41:03","modified_gmt":"2024-05-08T08:41:03","slug":"javascript-%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-string-%e7%9a%84-trim-%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/169208.html","title":{"rendered":"JavaScript \u5982\u4f55\u4f7f\u7528 String \u7684 trim \u51fd\u6570"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27035627\/183e014b-eab0-4d2a-97cb-9c5c6d5a4605.webp\" alt=\"JavaScript \u5982\u4f55\u4f7f\u7528 String \u7684 trim \u51fd\u6570\" \/><\/p>\n<p><p>JavaScript\u7684<code>String<\/code>\u7684<code>trim<\/code>\u51fd\u6570\u4e3b\u8981\u7528\u4e8e\u53bb\u9664\u5b57\u7b26\u4e32\u4e24\u7aef\u7684\u7a7a\u767d\u5b57\u7b26\u3001\u5305\u62ec\u7a7a\u683c\u3001\u5236\u8868\u7b26\u7b49\u3002<strong>\u6838\u5fc3\u7528\u6cd5\u5305\u62ec\u53bb\u9664\u5b57\u7b26\u4e32\u4e24\u7aef\u7684\u7a7a\u767d\u5b57\u7b26\u3001\u589e\u5f3a\u9875\u9762\u6027\u80fd\u3001\u6539\u5584\u7528\u6237\u8f93\u5165\u7684\u6570\u636e\u5904\u7406\u3001\u4e3a\u5b57\u7b26\u4e32\u5339\u914d\u548c\u6bd4\u8f83\u6807\u51c6\u5316\u683c\u5f0f<\/strong>\u3002\u5176\u4e2d\uff0c<strong>\u53bb\u9664\u5b57\u7b26\u4e32\u4e24\u7aef\u7684\u7a7a\u767d\u5b57\u7b26<\/strong>\u662f<code>trim<\/code>\u51fd\u6570\u7684\u76f4\u63a5\u5e94\u7528\u3002\u5f53\u7528\u6237\u8f93\u5165\u6570\u636e\u65f6\uff0c\u7ecf\u5e38\u4f1a\u56e0\u4e3a\u8bef\u89e6\u6216\u4e0d\u6ce8\u610f\u800c\u5728\u6570\u636e\u524d\u540e\u6dfb\u52a0\u591a\u4f59\u7684\u7a7a\u767d\u5b57\u7b26\uff0c\u4f7f\u7528<code>trim<\/code>\u51fd\u6570\u53ef\u4ee5\u6709\u6548\u5730\u6e05\u9664\u8fd9\u4e9b\u4e0d\u5fc5\u8981\u7684\u7a7a\u683c\uff0c\u786e\u4fdd\u6570\u636e\u7684\u51c6\u786e\u6027\u548c\u4e00\u81f4\u6027\uff0c\u4ece\u800c\u5728\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u6570\u636e\u5904\u7406\u6216\u5b58\u50a8\u4e4b\u524d\u6807\u51c6\u5316\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8<code>String<\/code>\u7684<code>trim<\/code>\u51fd\u6570\u7684\u4f7f\u7528\u65b9\u6cd5\u53ca\u5176\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\u7684\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001STRING TRIM\u51fd\u6570\u7684\u57fa\u672c\u4f7f\u7528<\/h3>\n<\/p>\n<p><p><code>trim<\/code>\u51fd\u6570\u662fJavaScript\u4e2d<code>String<\/code>\u5bf9\u8c61\u7684\u539f\u578b\u65b9\u6cd5\uff0c\u5b83\u4e0d\u63a5\u53d7\u4efb\u4f55\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u4e32\uff0c\u8be5\u5b57\u7b26\u4e32\u662f\u901a\u8fc7\u5220\u9664\u539f\u5b57\u7b26\u4e32\u5f00\u5934\u548c\u7ed3\u5c3e\u7684\u6240\u6709\u7a7a\u767d\u5b57\u7b26\u5f97\u5230\u7684\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\u4ee3\u7801<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">let originalString = &quot;   Hello World!   &quot;;<\/p>\n<p>let trimmedString = originalString.trim();<\/p>\n<p>console.log(trimmedString); \/\/ \u8f93\u51fa: &quot;Hello World!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8be5\u4f8b\u5b50\u4e2d\uff0c\u5373\u4f7f\u539f\u59cb\u5b57\u7b26\u4e32<code>originalString<\/code>\u524d\u540e\u6709\u591a\u4f59\u7684\u7a7a\u683c\uff0c\u8c03\u7528<code>trim<\/code>\u65b9\u6cd5\u4e4b\u540e\uff0c\u8fd9\u4e9b\u7a7a\u683c\u90fd\u88ab\u79fb\u9664\uff0c\u7ed3\u679c\u53ea\u5269\u4e0b&quot;Hello World!&quot;\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u589e\u5f3a\u9875\u9762\u6027\u80fd\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728Web\u5f00\u53d1\u4e2d\uff0c\u51cf\u5c11\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u7aef\u7684\u65e0\u6548\u64cd\u4f5c\u53ef\u4ee5\u6709\u6548\u63d0\u5347\u9875\u9762\u7684\u6027\u80fd\u3002<strong>\u4f7f\u7528trim\u51fd\u6570\u6765\u53bb\u9664\u7528\u6237\u8f93\u5165\u7684\u591a\u4f59\u7a7a\u683c\u53ef\u4ee5\u51cf\u5c11\u53d1\u9001\u5230\u670d\u52a1\u5668\u7684\u6570\u636e\u91cf<\/strong>\uff0c\u4ece\u800c\u8282\u7701\u5e26\u5bbd\u8d44\u6e90\u3002\u6b64\u5916\uff0c\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u5b57\u7b26\u5904\u7406\u4e5f\u80fd\u63d0\u5347\u670d\u52a1\u5668\u5904\u7406\u8bf7\u6c42\u7684\u6548\u7387\u3002<\/p>\n<\/p>\n<p><p><strong>\u6027\u80fd\u4f18\u5316\u793a\u4f8b<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">\/\/ \u7528\u6237\u8f93\u5165\u5904\u7406\u524d<\/p>\n<p>function processInput(input) {<\/p>\n<p>    \/\/ \u76f4\u63a5\u5904\u7406\u8f93\u5165\u6570\u636e<\/p>\n<p>}<\/p>\n<p>\/\/ \u7528\u6237\u8f93\u5165\u5904\u7406\u540e<\/p>\n<p>function processInput(input) {<\/p>\n<p>    let trimmedInput = input.trim(); \/\/ \u51cf\u5c11\u65e0\u6548\u7a7a\u683c\u5b57\u7b26<\/p>\n<p>    \/\/ \u5904\u7406\u4f18\u5316\u540e\u7684\u8f93\u5165\u6570\u636e<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u53bb\u9664\u65e0\u6548\u7684\u7a7a\u683c\u5b57\u7b26\u540e\uff0c\u670d\u52a1\u5668\u5c06\u5904\u7406\u66f4\u52a0\u51c6\u786e\u548c\u9ad8\u6548\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u6539\u5584\u7528\u6237\u8f93\u5165\u6570\u636e\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u8868\u5355\u9a8c\u8bc1\u6216\u4efb\u4f55\u9700\u8981\u7528\u6237\u8f93\u5165\u6570\u636e\u7684\u573a\u666f\u4e2d\uff0c\u4f7f\u7528<code>trim<\/code>\u65b9\u6cd5\u53bb\u9664\u4e24\u7aef\u7684\u7a7a\u767d\u5b57\u7b26\u662f\u4e00\u79cd\u6700\u4f73\u5b9e\u8df5\u3002<strong>\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u56e0\u591a\u4f59\u7684\u7a7a\u767d\u5b57\u7b26\u9020\u6210\u7684\u6570\u636e\u9a8c\u8bc1\u5931\u8d25<\/strong>\uff0c\u5e76\u63d0\u9ad8\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<\/p>\n<p><p><strong>\u7528\u6237\u6570\u636e\u5904\u7406\u793a\u4f8b<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function validateInput(input) {<\/p>\n<p>    let trimmedInput = input.trim();<\/p>\n<p>    if (trimmedInput.length === 0) {<\/p>\n<p>        console.log(&quot;Input cannot be only spaces.&quot;);<\/p>\n<p>    } else {<\/p>\n<p>        \/\/ \u7ee7\u7eed\u5176\u4ed6\u9a8c\u8bc1\u903b\u8f91<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7.trim()\u6e05\u7406\u8f93\u5165\uff0c\u53ef\u4ee5\u9632\u6b62\u7a7a\u683c\u7b49\u7a7a\u767d\u5b57\u7b26\u7684\u5e72\u6270\uff0c\u4f7f\u5f97\u6570\u636e\u9a8c\u8bc1\u903b\u8f91\u66f4\u52a0\u4e25\u8c28\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5b57\u7b26\u4e32\u5339\u914d\u548c\u6bd4\u8f83\u7684\u6807\u51c6\u5316\u683c\u5f0f<\/h3>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u6bd4\u8f83\u5f80\u5f80\u9700\u8981\u51c6\u786e\u7684\u5339\u914d\u5b57\u7b26\u5e8f\u5217\uff0c<strong>\u4f7f\u7528<code>trim<\/code>\u51fd\u6570\u53ef\u4ee5\u786e\u4fdd\u5728\u5b57\u7b26\u4e32\u6bd4\u8f83\u524d\u4e24\u7aef\u6ca1\u6709\u591a\u4f59\u7684\u7a7a\u683c\u5b57\u7b26<\/strong>\uff0c\u8fd9\u5bf9\u4e8e\u6570\u636e\u5e93\u67e5\u8be2\u3001\u65e5\u5fd7\u5206\u6790\u3001\u7528\u6237\u8eab\u4efd\u9a8c\u8bc1\u7b49\u60c5\u51b5\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<\/p>\n<p><p><strong>\u5b57\u7b26\u4e32\u6bd4\u8f83\u793a\u4f8b<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">let username = &quot; user &quot;;<\/p>\n<p>let inputName = &quot;user&quot;;<\/p>\n<p>if (username.trim() === inputName.trim()) {<\/p>\n<p>    console.log(&quot;Matching usernames.&quot;);<\/p>\n<p>} else {<\/p>\n<p>    console.log(&quot;Usernames do not match.&quot;);<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u5339\u914d\u548c\u6bd4\u8f83\u4e4b\u524d\uff0c\u9996\u5148\u4f7f\u7528.trim()\u7f8e\u5316\u8fd9\u4e9b\u5b57\u7b26\u4e32\uff0c\u786e\u4fdd\u4e86\u6bd4\u8f83\u64cd\u4f5c\u7684\u51c6\u786e\u6027\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u8ba8\u8bba\uff0c\u6211\u4eec\u4e86\u89e3\u5230JavaScript\u4e2d\u7684<code>String<\/code>\u7684<code>trim<\/code>\u51fd\u6570\u65e2\u7b80\u6d01\u53c8\u6709\u7528\uff0c\u5b83\u901a\u8fc7\u64cd\u7eb5\u5b57\u7b26\u4e32\u53ef\u4ee5\u63d0\u5347\u591a\u79cdweb\u7f16\u7a0b\u573a\u666f\u7684\u6027\u80fd\u548c\u7528\u6237\u4f53\u9a8c\u3002\u65e0\u8bba\u662f\u5904\u7406\u7528\u6237\u8f93\u5165\u3001\u53d1\u9001\u8bf7\u6c42\u3001\u5b58\u50a8\u6570\u636e\u8fd8\u662f\u6267\u884c\u6bd4\u8f83\u64cd\u4f5c\uff0c<code>trim<\/code>\u65b9\u6cd5\u90fd\u662f\u786e\u4fdd\u6570\u636e\u683c\u5f0f\u4e00\u81f4\u6027\u548c\u51c6\u786e\u6027\u7684\u91cd\u8981\u5de5\u5177\u3002\u5728\u7f16\u7a0b\u5b9e\u8df5\u4e2d\u5408\u7406\u5730\u5e94\u7528\u6b64\u51fd\u6570\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u4ee3\u7801\u8d28\u91cf\u548c\u7a0b\u5e8f\u7a33\u5b9a\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>Q1\uff1aString \u7684 trim \u51fd\u6570\u7684\u4f5c\u7528\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\ntrim\u51fd\u6570\u53ef\u4ee5\u7528\u6765\u53bb\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u9996\u5c3e\u7a7a\u683c\uff0c\u4f7f\u5b57\u7b26\u4e32\u66f4\u52a0\u6574\u6d01\u548c\u6613\u8bfb\u3002<\/p>\n<p><strong>Q2\uff1atrim \u51fd\u6570\u5982\u4f55\u5728 JavaScript \u4e2d\u4f7f\u7528\uff1f<\/strong><br \/>\n\u8981\u4f7f\u7528 trim \u51fd\u6570\uff0c\u9996\u5148\u9700\u8981\u8bbf\u95ee\u4e00\u4e2a\u5b57\u7b26\u4e32\u53d8\u91cf\uff0c\u7136\u540e\u4f7f\u7528\u70b9\u64cd\u4f5c\u7b26\uff08.\uff09\u5c06 trim \u65b9\u6cd5\u9644\u52a0\u5230\u8be5\u53d8\u91cf\u4e0a\u3002\u4f8b\u5982\uff1a<code>let str = &quot;  hello world  &quot;; str.trim();<\/code>\u3002<\/p>\n<p><strong>Q3\uff1atrim \u51fd\u6570\u8fd8\u6709\u5176\u4ed6\u7528\u9014\u5417\uff1f<\/strong><br \/>\n\u9664\u4e86\u53bb\u9664\u5b57\u7b26\u4e32\u4e24\u7aef\u7684\u7a7a\u683c\u4e4b\u5916\uff0ctrim \u51fd\u6570\u8fd8\u53ef\u4ee5\u7528\u6765\u53bb\u9664\u5176\u4ed6\u5b57\u7b26\uff0c\u4f8b\u5982\u53bb\u9664\u6362\u884c\u7b26<code>(\\n)<\/code>\u3001\u5236\u8868\u7b26<code>(\\t)<\/code>\u7b49\u3002\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6216\u81ea\u5b9a\u4e49\u7684\u5b57\u7b26\u96c6\u6765\u6307\u5b9a\u9700\u8981\u53bb\u9664\u7684\u5b57\u7b26\u3002\u4f8b\u5982\uff1a<code>let str = &quot;1,2,3,4,5,&quot;; str.trim(&quot;,&quot;);<\/code>\u5c06\u53bb\u9664\u5b57\u4e32\u4e24\u7aef\u7684\u9017\u53f7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"JavaScript\u7684String\u7684trim\u51fd\u6570\u4e3b\u8981\u7528\u4e8e\u53bb\u9664\u5b57\u7b26\u4e32\u4e24\u7aef\u7684\u7a7a\u767d\u5b57\u7b26\u3001\u5305\u62ec\u7a7a\u683c\u3001\u5236\u8868\u7b26\u7b49\u3002\u6838\u5fc3\u7528\u6cd5 [&hellip;]","protected":false},"author":3,"featured_media":169211,"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\/169208"}],"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=169208"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/169208\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/169211"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=169208"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=169208"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=169208"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}