确定字符串是否为“空”

时间:2022-12-19 07:23:20

I need a JavaScript function to tell me whether a string object is empty. By "empty", I mean that it's not all just whitespace characters. I've written this prototype:

我需要一个JavaScript函数来告诉我字符串对象是否为空。所谓的“空”,我的意思是它并不仅仅是空白字符。我写这个原型:

String.prototype.isEmpty = function() {
  return this.length === 0 || this === " " || this.test(/^\s*$/);
}

Is this alright?

这是好的吗?

Is there a more-performant version of this out there?

还有更精彩的版本吗?

6 个解决方案

#1


12  

Use

使用

String.prototype.isEmpty = function() {  
  if (!this.match(/\S/)) {
    return ('enter some valid input.Empty space is not allowed');
  } else {
   return "correct input";
  }
}


alert("test 1:"+("    ".isEmpty()));
alert("test 2:"+("   \t ".isEmpty()));
alert("test 3:"+("  \n   ".isEmpty()));
alert("test 4:"+("hi".isEmpty()));

Note:

注意:

\s will match a whitespace character: space, tab or new line.

\s将匹配空格字符:空格、制表符或新行。

\S will match non whitespace character:anything but not a space, tab or new line. If your string has a single character which is not a space, tab or new line, then it's not empty. Therefore you just need to search for one character: \S

\S将匹配非空格字符:除了空格、制表符或新行之外的任何字符。如果您的字符串只有一个字符,不是空格、制表符或新行,那么它不是空的。因此你只需要搜索一个字符:\S

#2


4  

It looks like you need to use /^\s*$/.test(this) instead of this.test(/^\s*$/). There is no test() method for strings, unless you're using some JavaScript library that implements this method.

看起来你需要使用/ ^ \ s *美元/ test()而不是this.test(美元/ ^ \ s * /)。对于字符串,没有test()方法,除非使用实现该方法的JavaScript库。

The /^\s*$/.test(this) would have been enough, but the first two expressions would short circuit if any one of them evaluates to true, without having to test the regular expression. That should be pretty efficient.

/ ^ \ s * $ / test()将是足够的,但是前两个表达式会短路,如果其中任何一个评估为true,而无需测试正则表达式。这应该是非常有效的。

As @Matthew Crumley noted in a comment above, your this === " " expression will always evaluate to false. You could remove this expression, or use == if you would will be expecting a lot of strings with just a single space character:

正如@Matthew Crumley在上面的评论中所指出的,您的this === " "表达式将始终被评估为false。您可以删除这个表达式,或者使用== =,如果您希望大量的字符串只有一个空格字符:

String.prototype.isEmpty = function() {
  return this.length === 0 || this == " " || /^\s*$/.test(this);
}

#3


1  

String.prototype.isEmpty = function() {
  return this.length == 0 || /^\s*$/.test(this);
}

There is just a possibility out of 255 (not considering Unicode characters with code greater than 255) that a string with length 1 contains the space character. Considering strings with lenght greater than 1, the possibility get even lower.

有一种可能性是255(不考虑编码大于255的Unicode字符),长度为1的字符串包含空格字符。考虑到长度大于1的字符串,可能性更低。

#4


1  

Generally, using RegExp.prototype.test for checking for a pattern match without compilation of a return array of matches (String.prototype.match) likely has a better performance. I'd try -- but haven't tested yet -- something like:

一般来说,使用RegExp.prototype。在不编译返回匹配数组(String.prototype.match)的情况下检查模式匹配的测试可能具有更好的性能。我想试试——但还没试过——比如:

function isEmpty() {

     var string = arguments[0] ;
     var EMPTY_STRING_PATTERN = /^\s*$/ , empty = false ;

     if( EMPTY_STRING_PATTERN.exec(string) === true ) empty = true ;

     return empty ;

}

On a side note, it is considered bad practice to fiddle with the prototype object of core javascript objects.

另一方面,对核心javascript对象的原型对象进行修改被认为是糟糕的做法。

#5


0  

from example https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/Trim

来自https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/Trim的例子

var orig="   foo  "
document.write(orig.trim())  // foo

document.write(orig.trim().length) // 3

tested on FF 3.6.8, Safari 5.0.1, Google Chrome 5.0.375.127, but produce js error on IE 8

在FF 3.6.8, Safari 5.0.1,谷歌Chrome 5.0.375.127上测试,但是在IE 8上产生js错误

#6


0  

I fiddled with some of the things:

我摆弄了一些东西:

  • I'm returning the inverse of this.length, for 0 that is true, for everything other number that is false.
  • 我返回的是这个的倒数。长度,对于0,这是对的,对于所有其它的数都是错的。
  • I removed the check for " ". I actually haven't seen much, if any cases where a single space was entered, removing the check is IMO faster on average.
  • 我取消了“”的支票。实际上我并没有看到太多,如果有任何情况下只有一个空格被输入,删除支票在我看来是更快的。
  • And flipped the regex <-> string like the others did.
  • 并像其他人一样翻转regex <->字符串。

String.prototype.isEmpty = function() {
return !this.length || /^\s*$/.test(this);
}

String.prototype。isEmpty = function() {return !this。长度| | / ^ \ s *美元/ test();}

#1


12  

Use

使用

String.prototype.isEmpty = function() {  
  if (!this.match(/\S/)) {
    return ('enter some valid input.Empty space is not allowed');
  } else {
   return "correct input";
  }
}


alert("test 1:"+("    ".isEmpty()));
alert("test 2:"+("   \t ".isEmpty()));
alert("test 3:"+("  \n   ".isEmpty()));
alert("test 4:"+("hi".isEmpty()));

Note:

注意:

\s will match a whitespace character: space, tab or new line.

\s将匹配空格字符:空格、制表符或新行。

\S will match non whitespace character:anything but not a space, tab or new line. If your string has a single character which is not a space, tab or new line, then it's not empty. Therefore you just need to search for one character: \S

\S将匹配非空格字符:除了空格、制表符或新行之外的任何字符。如果您的字符串只有一个字符,不是空格、制表符或新行,那么它不是空的。因此你只需要搜索一个字符:\S

#2


4  

It looks like you need to use /^\s*$/.test(this) instead of this.test(/^\s*$/). There is no test() method for strings, unless you're using some JavaScript library that implements this method.

看起来你需要使用/ ^ \ s *美元/ test()而不是this.test(美元/ ^ \ s * /)。对于字符串,没有test()方法,除非使用实现该方法的JavaScript库。

The /^\s*$/.test(this) would have been enough, but the first two expressions would short circuit if any one of them evaluates to true, without having to test the regular expression. That should be pretty efficient.

/ ^ \ s * $ / test()将是足够的,但是前两个表达式会短路,如果其中任何一个评估为true,而无需测试正则表达式。这应该是非常有效的。

As @Matthew Crumley noted in a comment above, your this === " " expression will always evaluate to false. You could remove this expression, or use == if you would will be expecting a lot of strings with just a single space character:

正如@Matthew Crumley在上面的评论中所指出的,您的this === " "表达式将始终被评估为false。您可以删除这个表达式,或者使用== =,如果您希望大量的字符串只有一个空格字符:

String.prototype.isEmpty = function() {
  return this.length === 0 || this == " " || /^\s*$/.test(this);
}

#3


1  

String.prototype.isEmpty = function() {
  return this.length == 0 || /^\s*$/.test(this);
}

There is just a possibility out of 255 (not considering Unicode characters with code greater than 255) that a string with length 1 contains the space character. Considering strings with lenght greater than 1, the possibility get even lower.

有一种可能性是255(不考虑编码大于255的Unicode字符),长度为1的字符串包含空格字符。考虑到长度大于1的字符串,可能性更低。

#4


1  

Generally, using RegExp.prototype.test for checking for a pattern match without compilation of a return array of matches (String.prototype.match) likely has a better performance. I'd try -- but haven't tested yet -- something like:

一般来说,使用RegExp.prototype。在不编译返回匹配数组(String.prototype.match)的情况下检查模式匹配的测试可能具有更好的性能。我想试试——但还没试过——比如:

function isEmpty() {

     var string = arguments[0] ;
     var EMPTY_STRING_PATTERN = /^\s*$/ , empty = false ;

     if( EMPTY_STRING_PATTERN.exec(string) === true ) empty = true ;

     return empty ;

}

On a side note, it is considered bad practice to fiddle with the prototype object of core javascript objects.

另一方面,对核心javascript对象的原型对象进行修改被认为是糟糕的做法。

#5


0  

from example https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/Trim

来自https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/Trim的例子

var orig="   foo  "
document.write(orig.trim())  // foo

document.write(orig.trim().length) // 3

tested on FF 3.6.8, Safari 5.0.1, Google Chrome 5.0.375.127, but produce js error on IE 8

在FF 3.6.8, Safari 5.0.1,谷歌Chrome 5.0.375.127上测试,但是在IE 8上产生js错误

#6


0  

I fiddled with some of the things:

我摆弄了一些东西:

  • I'm returning the inverse of this.length, for 0 that is true, for everything other number that is false.
  • 我返回的是这个的倒数。长度,对于0,这是对的,对于所有其它的数都是错的。
  • I removed the check for " ". I actually haven't seen much, if any cases where a single space was entered, removing the check is IMO faster on average.
  • 我取消了“”的支票。实际上我并没有看到太多,如果有任何情况下只有一个空格被输入,删除支票在我看来是更快的。
  • And flipped the regex <-> string like the others did.
  • 并像其他人一样翻转regex <->字符串。

String.prototype.isEmpty = function() {
return !this.length || /^\s*$/.test(this);
}

String.prototype。isEmpty = function() {return !this。长度| | / ^ \ s *美元/ test();}