var y = "\0"; // an empty string, but has a null character
(y === "") // false, testing against an empty string does not work
(y.length === 0) // false
(y) // true, this is also not expected
(y.match(/^[\s]*$/)) // false, again not wanted
function isEmpty(s){
return !s.length;
}
function isBlank(s){
return isEmpty(s.trim());
}
24
我使用组合,最快的检查是第一次 .
function isBlank(pString){
if (!pString || pString.length == 0) {
return true;
}
// checks for a non-white space character
// which I think [citation needed] is faster
// than removing all the whitespace and checking
// against an empty string
return !/[^\s]+/.test(pString);
}
function tell()
{
var pass = document.getElementById('pasword').value;
var plen = pass.length;
now you can check if your string is empty as like
if(plen==0)
{
alert('empty');
}
else
{
alert('you entered something');
}
}
<input type='text' id='pasword' />
String.prototype.IsNullOrEmpty = function (value) { ... }
var myvar = null;
if (1 == 2) { myvar = "OK"; } // could be set
myvar.IsNullOrEmpty(); // throws error
我测试了以下值数组 . 如果有疑问,你可以循环测试你的功能 .
// Helper items
var MyClass = function (b) { this.a = "Hello World!"; this.b = b; };
MyClass.prototype.hello = function () { if (this.b == null) { alert(this.a); } else { alert(this.b); } };
var z;
var arr = [
// 0: Explanation for printing, 1: actual value
['undefined', undefined],
['(var) z', z],
['null', null],
['empty', ''],
['space', ' '],
['tab', '\t'],
['newline', '\n'],
['carriage return', '\r'],
['"\\r\\n"', '\r\n'],
['"\\n\\r"', '\n\r'],
['" \\t \\n "', ' \t \n '],
['" txt \\t test \\n"', ' txt \t test \n'],
['"txt"', "txt"],
['"undefined"', 'undefined'],
['"null"', 'null'],
['"0"', '0'],
['"1"', '1'],
['"1.5"', '1.5'],
['"1,5"', '1,5'], // valid number in some locales, not in js
['comma', ','],
['dot', '.'],
['".5"', '.5'],
['0', 0],
['0.0', 0.0],
['1', 1],
['1.5', 1.5],
['NaN', NaN],
['/\S/', /\S/],
['true', true],
['false', false],
['function, returns true', function () { return true; } ],
['function, returns false', function () { return false; } ],
['function, returns null', function () { return null; } ],
['function, returns string', function () { return "test"; } ],
['function, returns undefined', function () { } ],
['MyClass', MyClass],
['new MyClass', new MyClass()],
['empty object', {}],
['non-empty object', { a: "a", match: "bogus", test: "bogus"}],
['object with toString: string', { a: "a", match: "bogus", test: "bogus", toString: function () { return "test"; } }],
['object with toString: null', { a: "a", match: "bogus", test: "bogus", toString: function () { return null; } }]
];
30 回答
为了检查字符串是否为空,null或未定义,我使用:
为了检查字符串是否为空,null或未定义,我使用:
用于检查字符串是空白还是仅包含空格:
检查
var a;
是否存在修剪出
false spaces
中的值,然后测试emptiness
JavaScript中没有任何代表空字符串的内容 . 检查
length
(如果您知道var将始终是一个字符串)或针对""
我没有注意到一个考虑到字符串中空字符可能性的答案 . 例如,如果我们有一个空字符串:
要测试其null,可以执行以下操作:
它适用于空字符串,并且在空字符串上,并且可以访问所有字符串 . 此外,它可以扩展为包含其他JavaScript空或空白字符(即不间断空格,字节顺序标记,行/段分隔符等) .
同时我们可以有一个功能来检查所有'empties',如 null, undefined, '', ' ', {}, [] . 所以我写了这个 .
用例和结果 .
试试这个
我经常使用这样的东西,
你最接近
str.Empty
(前提条件是str是一个字符串)是:如果一个人不仅要检测空字符串还需要空白字符串,我将添加Goral的答案:
我使用组合,最快的检查是第一次 .
我不会过分担心最多 efficient 方法 . 使用你的意图最明确的 . 对我而言,通常是
strVar == ""
.编辑:根据Constantin的评论,如果strVar可能会有一些最终包含整数0值,那么这确实是那些意图澄清情况之一 .
以上所有都很好,但这会更好 . 使用
!!
(不是)运算符 .或使用类型铸造:
两者都执行相同的功能,将变量类型转换为boolean,其中
str
是变量 .为
null,undefined,0,000,"",false
返回false
.返回
true
表示字符串"0"和空格" " .您可以使用lodash:_. isEmpty(value) .
它涵盖了很多案例,如
{}
,''
,null
,undefined
等 .但它始终返回
true
Number
类型Javascript Primitive Data Types如_.isEmpty(10)
或_.isEmpty(Number.MAX_VALUE)
都返回true
.我用 :
尝试:
我通常使用类似的东西:
如果你只是想检查是否有任何 Value ,你可以这样做
如果你需要专门检查空字符串是否超过null,我会认为使用the === operator检查
""
是最好的选择(这样你就知道它实际上是你正在比较的字符串) .功能:
你也可以使用正则表达式:
检查空白或填充空格的字符串 .
此外,如果您将填充空格的字符串视为“空” . 您可以使用此Regex进行测试:
很多答案,以及很多不同的可能性!
毫无疑问,快速简单的实施是赢家:
if (!str.length) {...}
但是,正如许多其他例子一样 . 最好的功能方法,我建议:
我知道,有点过分了 .
所有这些答案都很好 .
但我不能确定变量是一个字符串,不包含空格(这对我来说很重要),并且可以包含'0'(字符串) .
我的版本:
样本jsfiddle .
这也是检查字段是否为空的通用方法 .
如果你需要确保字符串不只是一堆空格(我假设这是用于表单验证),你需要对空格进行替换 .
我做了一些研究,如果将非字符串和非空/空值传递给测试器函数会发生什么 . 众所周知,(0 ==“”)在javascript中是正确的,但由于0是一个值而不是空或null,您可能想要测试它 .
以下两个函数仅对undefined,null,empty / whitespace值返回true,对其他所有值返回false,如数字,布尔值,对象,表达式等 .
存在更复杂的例子,但这些例子很简单并且给出了一致的结果 . 没有必要测试undefined,因为它包含在(value == null)检查中 . 您也可以通过将它们添加到String来模仿C#行为,如下所示:
你不想把它放在字符串中prototype,因为如果String-class的实例为null,则会出错:
我测试了以下值数组 . 如果有疑问,你可以循环测试你的功能 .
忽略空白字符串,您可以使用它来检查null,empty和undefined:
简洁,它适用于未定义的属性,虽然它不是最易读的 .
没有
isEmpty()
方法,你必须检查类型和长度:当
test
是undefined
或null
时,需要进行类型检查以避免运行时错误 .不要假设您检查的变量是字符串 . 不要假设如果这个var有一个长度,那么它就是一个字符串 .
问题是:仔细考虑您的应用必须做什么并且可以接受的内容 . Build 强大的东西 .
如果你的方法/函数只应该处理一个非空字符串,那么测试参数是否为非空字符串并且不做一些“技巧” .
作为一个例子,如果你在这里不小心遵循一些建议会爆炸 .
所以,我坚持
检查是否完全是一个空字符串:
检查它是否为空字符串或无值的逻辑等效项(null,undefined,0,NaN,false,...):
下划线javascript库http://underscorejs.org/提供了一个非常有用的
_.isEmpty()
函数,用于检查空字符串和其他空对象 .参考:http://underscorejs.org/#isEmpty
其他非常有用的下划线功能包括:
http://underscorejs.org/#isNull
_.isNull(object)
http://underscorejs.org/#isUndefined
_.isUndefined(value)
http://underscorejs.org/#has
_.has(object, key)