使用 JavaScript 删除字符串中的空格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48678081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 08:11:27  来源:igfitidea点击:

Remove whitespace in a string with JavaScript

javascript

提问by ghostface78

I'm trying to get this palindrome generator to work, and I cannot figure out how to get js to remove the white space from between the words in multi-word palindromes. race car keeps coming back false. racecar comes back true, so part of the code is working. How do I get JS to ignore the white space between "race" and "car" so that race car comes back as true?

我试图让这个回文生成器工作,但我不知道如何让 js 从多词回文中的单词之间删除空格。赛车不断返回假。Racecar 恢复原状,所以部分代码正在运行。我如何让 JS 忽略“种族”和“汽车”之间的空白,以便赛车恢复真实?

function palindrome(word) {
    var len = word.length;
    word = word.replace(/ +/g, "");


    for (var i = 0; i < Math.floor(len/2); i++ ) {
      if (word[i] !== word[len - 1 - i]) {
      return "FALSE";
       }
     }
     return "TRUE";
}

console.log(palindrome("race car"))

回答by vibhor1997a

Simply You can do this,

只需你可以做到这一点,

let str = '  aaa  aa  ';
str = str.replace(/\s+/g,'');
console.log(str);

回答by Raja Mohamed

Hope this code will help you

希望这段代码能帮到你

var str = " this is a sample "
var res = str.replace(/ /g, "");
console.log(res);

O/P - 'thisisasample'

回答by Innocent Criminal

You can try this:

你可以试试这个:

word = word.replace(/\s+/g, " ").trim();

It will remove the spaces from the front and the back as well.

它还将从正面和背面去除空间。

回答by Hussain Ali Akbar

You can do this with a regex before passing it to the palindrome function:

在将其传递给回文函数之前,您可以使用正则表达式执行此操作:

'race car'.replace(/\s+/, "") 

'race car' can also be replaced by any variable containing your string.

'race car' 也可以替换为任何包含您的字符串的变量。

回答by Mayur Phanse

You can try this.

你可以试试这个。

replace(/\s+/, " ")

回答by xianshenglu

 var len = word.length; 

get the len after word change not before;

在单词更改之后而不是之前获取 len;

function palindrome(word) {

  word = word.replace(/ +/g, "");
  var len = word.length;

  for (var i = 0; i < Math.floor(len / 2); i++) {
    if (word[i] !== word[len - 1 - i]) {
      return "FALSE";
    }
  }
  return "TRUE";
}
console.log(palindrome("race car"))

and

  word = word.replace(/ +/g, "");

suggest use regular expression:

建议使用正则表达式:

  word = word.replace(/\s+/g, "");     

回答by ThunderStorm

You are taking lenbefore removing white spaces. So the last element you were asking returning undefined, that's result in no match and FALSE output.

len在删除空格之前正在拍摄。因此,您要求返回的最后一个元素undefined导致不匹配和 FALSE 输出。

Try following snippet:

尝试以下代码段:

 function palindrome(word) {
          word = word.replace(/ +/g, "");
          var len = word.length;
          for (var i = 0; i < Math.floor(len/2); i++ ) {
          if (word[i] !== word[len - 1 - i]) {
          return "FALSE";
           }
         }
         return "TRUE";
    }
 console.log(palindrome("race car"))

回答by Rajesh Kumar Swain

Please use the following code to remove space between two words.

请使用以下代码删除两个单词之间的空格。

word = word.replace(/\s/g, ''));