【问题标题】:How to detect string which contains only spaces?如何检测仅包含空格的字符串?
【发布时间】:2012-04-21 18:56:33
【问题描述】:

包含一个空格的字符串长度总是等于1:

alert('My str length: ' + str.length);

空格是一个字符,所以:

str = "   ";
alert('My str length:' + str.length); // My str length: 3

如何区分空字符串和仅包含空格的字符串?如何检测只包含空格的字符串?

【问题讨论】:

标签: javascript


【解决方案1】:

为此,您可以使用正则表达式来删除字符串中的所有空格。如果结果字符串的长度是0,那么您可以确定原始字符串只包含空格。试试这个:

var str = "    ";
if (!str.replace(/\s/g, '').length) {
  console.log('string only contains whitespace (ie. spaces, tabs or line breaks)');
}

【讨论】:

  • 好吧,字符串只包含空格。
  • 您可能还想检查长度,所以空字符串不会通过:if(str.length>0 && str.replace(/\s/g, '').length==0) { // string has only spaces }
【解决方案2】:

最快的解决方案是使用正则表达式原型函数test() 并查找不是空格、制表符或换行符\S 的任何字符:

if (!/\S/.test(str)) {
  // Didn't find something other than a space which means it's empty
}

如果你有一个超长的字符串,它会产生很大的不同,因为它会在找到非空格字符后立即停止处理。

【讨论】:

  • /\S/.test(" w ") 是真的
  • @leavesster 是的,因为它发现了除空格之外的其他内容。这才是重点。我同意我的回答可能会产生误导,因为当字符串 not 为空时条件为真。我会把它改成!/\S/.test(str)。
【解决方案3】:

与 Rory 的回答类似,使用 ECMA 5,您现在只需调用 str.trim().length 而不是使用正则表达式。如果结果值为 0,则您知道您有一个仅包含空格的字符串。

if (!str.trim().length) {
  console.log('str is empty!');
}

您可以阅读有关修剪的更多信息here。

编辑:几年后看了这个之后,我注意到这可以进一步简化。由于 trim 的结果要么为真要么为假,您还可以执行以下操作:

if (!str.trim()) {
     console.log('str is empty!');
}

【讨论】:

  • 我最喜欢这个答案。非常光滑。 ?
  • 这是最优雅的解决方案。
【解决方案4】:
if(!str.trim()){
  console.log('string is empty or only contains spaces');
}

String#trim() 删除字符串开头和结尾的空格。如果字符串只包含空格,则修剪后为空,空字符串在 JavaScript 中为假。

如果字符串可能是null或undefined,我们需要在修剪之前先检查字符串本身是否为假。

if(!str || !str.trim()){
   //str is null, undefined, or contains only spaces
}

这可以使用optional chaining operator 来简化。

if(!str?.trim()){
   //str is null, undefined, or contains only spaces
}

【讨论】:

    【解决方案5】:

    您可以通过为您的字符串创建一个修剪函数来修剪您的字符串值。

    String.prototype.trim = function () {
        return this.replace(/^\s*/, "").replace(/\s*$/, "");
    }
    

    现在它可用于您的每个字符串,您可以将其用作

    str.trim().length// Result will be 0
    

    您也可以使用此方法删除字符串开头和结尾处的空格,即

    "  hello  ".trim(); // Result will be "hello"
    

    【讨论】:

    • 扩展内置对象的原型是个坏主意。
    • @exizt: 如果它只被他自己的代码使用呢?
    【解决方案6】:

    通过创建修剪函数修剪您的字符串值

    var text = "  ";
    if($.trim(text.length == 0){
      console.log("Text is empty");
    }
    else
    {
      console.log("Text is not empty");
    }
    

    【讨论】:

      【解决方案7】:

      如果我们想检查字符串是否只包含空格,那么这可能会有所帮助。

      const str = '   ';
      console.log(/\s/.test(str));
      

      这将记录为真。

      【讨论】:

      • 这将对任何包含空格的字符串返回 true,而不仅仅是那些仅包含空格的字符串。
      【解决方案8】:

      这适用于 Dart 而不是 Javascript。但是,当我搜索如何使用 Dart 执行此操作时,出现了这个问题,所以我认为其他人可能需要 Dart 的答案。

      String foo = '        ';
      if (foo.replaceAll(' ', '').length == 0) {
      print('ALL WHITE SPACE');
      }
      else {
      print('NOT ALL WHITE SPACE');
      }
      

      为了进一步说明,字符串' d ' 将打印“不是所有空白”,而字符串' ' 将打印“所有空白”。

      【讨论】:

      • 此答案应添加到带有与Dart相关标签的问题中
      猜你喜欢
      • 2011-01-03
      • 2011-01-25
      • 2013-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多