C# String.IsNullOrEmpty Javascript 等效项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5746947/
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
C# String.IsNullOrEmpty Javascript equivalent
提问by Stanley Cup Phil
I want to try to do string call equivalent to the C# String.IsNullOrEmpty(string)
in javascript. I looked online assuming that there was a simple call to make, but I could not find one.
我想尝试String.IsNullOrEmpty(string)
在 javascript 中执行等效于 C# 的字符串调用。我在网上看,假设有一个简单的电话要拨打,但我找不到。
For now I am using a if(string === "" || string === null)
statement to cover it, but I would rather use a predefined method (I keep getting some instances that slip by for some reason)
现在我使用一个if(string === "" || string === null)
语句来覆盖它,但我宁愿使用预定义的方法(我不断收到一些由于某种原因而溜走的实例)
What is the closest javascript (or jquery if then have one) call that would be equal?
什么是最接近的 javascript(或 jquery,如果有的话)调用是相等的?
回答by JoJo
You're overthinking. Null and empty string are both falsey values in JavaScript.
你多虑了。Null 和空字符串在 JavaScript 中都是假值。
if(!theString) {
alert("the string is null or empty");
}
Falsey:
假的:
- false
- null
- undefined
- The empty string ''
- The number 0
- The number NaN
- 错误的
- 空值
- 不明确的
- 空字符串 ''
- 数字 0
- 数字 NaN
回答by Code Maverick
If, for whatever reason, you wanted to test only null
and empty
, you could do:
如果出于某种原因,您只想测试null
and empty
,则可以执行以下操作:
function isNullOrEmpty( s )
{
return ( s == null || s === "" );
}
Note: This will also catch undefined as @Raynos mentioned in the comments.
注意:这也会像评论中提到的@Raynos 一样捕获 undefined。
回答by alexl
if (!string) {
// is emtpy
}
What is the best way to test for an empty string with jquery-out-of-the-box?
回答by awm
If you know that string
is not numeric, this will work:
如果您知道这string
不是数字,这将起作用:
if (!string) {
.
.
.
回答by BrokenGlass
you can just do
你可以做
if(!string)
{
//...
}
This will check string
for undefined, null, and empty string.
这将检查string
未定义、空值和空字符串。
回答by Craig
To be clear, if(!theString){//...}
where theString is an undeclared variable will throw an undefined error, not find it true. On the other hand if you have: if(!window.theString){//...}
or var theString; if(!theString){//...}
it will work as expected. In the case where a variable may not be declared (as opposed to being a property or simply not set), you need to use: if(typeof theString === 'undefined'){//...}
需要明确的是,if(!theString){//...}
如果 theString 是未声明的变量,则会抛出未定义的错误,而不是发现它为真。另一方面,如果你有:if(!window.theString){//...}
或者var theString; if(!theString){//...}
它会按预期工作。在可能未声明变量的情况下(而不是作为属性或只是未设置),您需要使用:if(typeof theString === 'undefined'){//...}
My preference is to create a prototype function that wraps it up for you.
我的偏好是创建一个原型函数,为您包装它。
回答by Martien de Jong
Since the answer that is marked as correct contains a small error, here is my best try at coming up with a solution. I have two options, one that takes a string, the other takes a string or a number, since I assume many people are mixing strings and numbers in javascript.
由于标记为正确的答案包含一个小错误,这是我提出解决方案的最佳尝试。我有两种选择,一种采用字符串,另一种采用字符串或数字,因为我假设很多人在 javascript 中混合使用字符串和数字。
Steps: -If the object is null it is a null or empty string. -If the type is not string (or number) it's string value is null or empty. NOTE: we might throw an exception here as well, depending on preferences. -If the trimmed string value has a length that is small than 1 it is null or empty.
步骤: - 如果对象为空,则为空或空字符串。- 如果类型不是字符串(或数字),则其字符串值为 null 或为空。注意:我们也可能会在此处抛出异常,具体取决于偏好。- 如果修剪后的字符串值的长度小于 1,则为 null 或空。
var stringIsNullOrEmpty = function(theString)
{
return theString == null || typeof theString != "string" || theString.trim().length < 1;
}
var stringableIsNullOrEmpty = function(theString)
{
if(theString == null) return true;
var type = typeof theString;
if(type != "string" && type != "number") return true;
return theString.toString().trim().length < 1;
}
回答by Ahmad Hindash
you can say it by logic
你可以按逻辑说
Let say you have a variable name a strVal, to check if is null or empty
假设您有一个变量名为 strVal,以检查它是 null 还是空
if (typeof (strVal) == 'string' && strVal.length > 0)
{
// is has a value and it is not null :)
}
else
{
//it is null or empty :(
}
回答by Anjum....
You can create one Utility method which can be reused in many places such as:
您可以创建一种可在许多地方重复使用的实用程序方法,例如:
function isNullOrEmpty(str){
var returnValue = false;
if ( !str
|| str == null
|| str === 'null'
|| str === ''
|| str === '{}'
|| str === 'undefined'
|| str.length === 0 ) {
returnValue = true;
}
return returnValue;
}