本文翻译自:How to remove spaces from a string using JavaScript?

How to remove spaces in a string? 如何删除字符串中的空格? For instance: 例如:

Input: 输入:

'/var/www/site/Brand new document.docx'

Output: 输出:

'/var/www/site/Brandnewdocument.docx'

#1楼

参考:https://stackoom.com/question/P1IM/如何使用JavaScript从字符串中删除空格


#2楼

var input = '/var/www/site/Brand new document.docx';//remove space
input = input.replace(/\s/g, '');//make string lower
input = input.toLowerCase();alert(input);

Click here for working example 点击此处查看工作示例


#3楼

  var output = '/var/www/site/Brand new document.docx'.replace(/ /g, ""); orvar output = '/var/www/site/Brand new document.docx'.replace(/ /gi,"");

Note: Though you use 'g' or 'gi' for removing spaces both behaves the same. 注意:虽然使用'g'或'gi'来删除空格,但两者的行为都相同。

If we use 'g' in the replace function, it will check for the exact match. 如果我们在替换函数中使用'g',它将检查完全匹配。 but if we use 'gi', it ignores the case sensitivity. 但如果我们使用'gi',它会忽略区分大小写。

for reference click here . 供参考点击这里 。


#4楼

Following @rsplak answer: actually, using split/join way is faster than using regexp. 关注@rsplak回答:实际上,使用split / join方式比使用regexp更快。 See the performance test case 查看性能测试用例

So 所以

var result = text.split(' ').join('')

operates faster than 运作速度比

var result = text.replace(/\\s+/g, '')

On small texts this is not relevant, but for cases when time is important, eg in text analisers, especially when interacting with users, that is important. 在小文本上,这是不相关的,但对于时间很重要的情况,例如在文本分析器中,特别是在与用户交互时,这很重要。


On the other hand, \\s+ handles wider variety of space characters. 另一方面, \\s+处理更多种类的空间字符。 Among with \\n and \\t , it also matches character, and that is what   \\n\\t ,它也匹配字符,这就是  is turned in, when getting text using textDomNode.nodeValue . 在使用textDomNode.nodeValue获取文本时textDomNode.nodeValue

So I think that conclusion in here can be made as follows: if you only need to replace spaces ' ' , use split/join. 所以我认为这里的结论可以如下:如果你只需要替换空格 ' ' ,请使用split / join。 If there can be different symbols of symbol class - use replace(/\\s+/g, '') 如果符号类可以有不同的符号 - 使用replace(/\\s+/g, '')


#5楼

SHORTEST and FASTEST : str.replace(/ /g, ''); 最短和最快str.replace str.replace(/ /g, '');


Benchmark: 基准测试:

Here my results - (2018.07.13) MacOs High Sierra 10.13.3 on Chrome 67.0.3396 (64-bit), Safari 11.0.3 (13604.5.6), Firefox 59.0.2 (64-bit) ): 在这里我的结果 - (2018.07.13)MacO High Sierra 10.13.3在Chrome 67.0.3396(64位),Safari 11.0.3(13604.5.6),Firefox 59.0.2(64位)):

SHORT strings SHORT字符串

Short string similar to examples from OP question 短字符串类似于OP问题的例子

The fastest solution on all browsers is / /g (regexp1a) - Chrome 17.7M (operation/sec), Safari 10.1M, Firefox 8.8M. 所有浏览器上最快的解决方案是/ /g (regexp1a) - Chrome 17.7M(操作/秒),Safari 10.1M,Firefox 8.8M。 The slowest for all browsers was split-join solution. 所有浏览器中最慢的是split-join解决方案。 Change 更改 to \\s or add + or i to regexp slows down processing. \\s或添加+i到regexp减慢处理速度。

LONG strings 长串

For string about ~3 milion character results are: 对于大约3个百万字符串的字符串结果是:

  • regexp1a : Safari 50.14 ops/sec, Firefox 18.57, Chrome 8.95 regexp1a :Safari 50.14 ops / sec,Firefox 18.57,Chrome 8.95
  • regexp2b : Safari 38.39, Firefox 19.45, Chrome 9.26 regexp2b :Safari 38.39,Firefox 19.45,Chrome 9.26
  • split-join : Firefox 26.41, Safari 23.10, Chrome 7.98, split-join :Firefox 26.41,Safari 23.10,Chrome 7.98,

You can run it on your machine: https://jsperf.com/remove-string-spaces/1 您可以在您的计算机上运行它: https : //jsperf.com/remove-string-spaces/1


#6楼

Although there are ways to use regex to remove spaces, there is a simple function that can remove all whitespace called .trim(); 虽然有办法使用正则表达式来删除空格,但是有一个简单的函数可以删除所有名为.trim();空格.trim(); :

var str = "abr a cadab ra";
str = str.trim();
//str = "abracadabra"

如何使用JavaScript从字符串中删除空格?相关推荐

  1. python字符串去掉空行_Python从字符串中删除空格

    python字符串去掉空行 There are various ways to remove spaces from a string in Python. This tutorial is aime ...

  2. 在Java中从字符串中删除空格

    我有一个像这样的字符串: mysz = "name=john age=13 year=2001"; 我想删除字符串中的空格. 我试过trim()但这只删除了整个字符串前后的空格. ...

  3. python字符串去掉空行_从python中的字符串中删除空格

    python字符串去掉空行 如何在python中删除字符串中的空格 (How to remove whitespaces in a string in python) str.lstrip()str. ...

  4. C语言数组字符串清除空格,用C语言从字符串中删除空格

    如果不使用stdio.h和stdlib.h以外的任何库,我就不知道如何删除句子开头的空格. #include int main() { char text[1000], result[1000]; i ...

  5. python字符串剔除空格和逗号_用逗号分隔并在Python中删除空格

    用逗号分隔并在Python中删除空格 我有一些python代码分裂逗号,但不剥离空格: >>> string = "blah, lots , of , spaces, he ...

  6. String中删除空格的7种方法!

    字符串,是Java中最常用的一个数据类型了.我们在日常开发时候会经常使用字符串做很多的操作.比如字符串的拼接.截断.替换等. 本文我们介绍一个比较常见又容易被忽略的一个操作,那就是移除字符串中的空格. ...

  7. [转载] Python从字符串中删除字符

    参考链接: Python | 字符串translate Sometimes we want to remove all occurrences of a character from a string ...

  8. Python从字符串中删除字符

    Sometimes we want to remove all occurrences of a character from a string. There are two common ways ...

  9. 统计substr在母串中出现次数/删除字符串中所有空格

    int count(char*str,char*substr) { int i,j,k,num=0; for(i=0;str[i]!='\0';i++) { for(j=i,k=0;substr[k] ...

最新文章

  1. HISAT2+StringTie+Ballgown安装及使用流程
  2. linux sort,uniq,cut,wc命令详解
  3. 终于把CString转化为char*了
  4. 7 useLayoutEffect、useDebugValue
  5. 4位无符号比较器设计
  6. python数据录入和分析_hive+python数据分析入门
  7. mysql 获取年预提,【判断题】正确核算待摊费用和预提费用,有助于划分本期费用与非本期费用的界限。...
  8. faststart可以卸载吗_你的手机你做主!免 ROOT 卸载安卓手机预装APP
  9. elasticsearch同义词配置elasticsearch-analysis-dynamic-synonym
  10. springmvc form中 commandName和modelAttribute的疑问
  11. Asterisk增加g729编码支持
  12. markdown下载破解地址 和 常用语法
  13. 关于看算法导论不懂的时候的思考
  14. NX/UG二次开发简单干涉
  15. CSDN如何收藏别人的博客文章
  16. Github开始强制使用PAT(Personal Access Token)了
  17. 我的爬虫入门作(一)
  18. wps“公式编辑器”的MT Extra 字体无效,将无法显示和打印某些字体。请重新安装“公式编辑器”,以便正确安装其字体
  19. type-c英文怎么读音发音,type-c怎么读英语发音
  20. 51单片机接上拉电阻原理

热门文章

  1. jquery submit ie6下失效的原因分析及解决方法
  2. 网络学习(一)网络版块主题介绍
  3. 网易邮箱的web服务器使用的是apache
  4. 博客搬家到CSDN:http://blog.csdn.net/yeweiouyang
  5. 【分布式】Zookeeper的服务器角色
  6. CF-697B Barnicle与691C Exponential notation
  7. iOS读取通讯录获取好友通讯录信息[名字(姓+名字),手机号码(多个号码)等]...
  8. 在ubuntu linux 中编写一个自己的bash脚本
  9. C语言中的static
  10. 使用XmlWriter写XML文件