如何删除字符串中的空格?例如:
输入:
'/var/www/site/Brand new document.docx'
输出:
'/var/www/site/Brandnewdocument.docx'
如何删除字符串中的空格?例如:
输入:
'/var/www/site/Brand new document.docx'
输出:
'/var/www/site/Brandnewdocument.docx'
当前回答
使用replaceAll似乎是最简单、最干净的方法。(我不能保证最快)
“/var/www/site/Brand new document.docx”。替代品(',')
见文档。
replaceAll()方法返回一个新字符串,其中模式的所有匹配项都被替换。模式可以是字符串或RegExp,替换可以是为每个匹配调用的字符串或函数。
其他回答
如果没有regexp,它只适用于一种情况。
input = input.replace(' ', '');
这是更快的简单! 在某些情况下能帮到你们中的一些人。
使用replaceAll似乎是最简单、最干净的方法。(我不能保证最快)
“/var/www/site/Brand new document.docx”。替代品(',')
见文档。
replaceAll()方法返回一个新字符串,其中模式的所有匹配项都被替换。模式可以是字符串或RegExp,替换可以是为每个匹配调用的字符串或函数。
你还可以使用JS中最新的字符串方法之一:replaceAll
'/var/www/site/Brand new document.docx'.replaceAll(' ', '');
var str='/var/www/site/全新文档.docx'; document。写入(str.replace(/\ s/g,"); ----------
从字符串中删除空格最简单的方法是使用replace
let str = '/var/www/site/Brand new document.docx';
let result = str.replace(/\s/g, '');