【问题标题】:C# String.IsNullOrEmpty Javascript equivalentC# String.IsNullOrEmpty Javascript 等效项
【发布时间】:2011-04-21 16:23:58
【问题描述】:

我想尝试在javascript中进行相当于C#String.IsNullOrEmpty(string)的字符串调用。我在网上查找,假设有一个简单的电话可以拨打,但我找不到。

现在我使用if(string === "" || string === null) 语句来覆盖它,但我宁愿使用预定义的方法(我不断收到一些由于某种原因而忽略的实例)

最接近的 javascript(或 jquery,如果有的话)调用是相等的?

【问题讨论】:

标签: javascript jquery


【解决方案1】:

你想多了。 Null 和空字符串在 JavaScript 中都是错误值。

if(!theString) {
 alert("the string is null or empty");
}

假的:

  • 未定义
  • 空字符串''
  • 数字0
  • 数字NaN

【讨论】:

  • 令人惊讶的是,这获得了如此多的赞成票。这个答案是不正确的。您自己说过虚假值将是 0。如果用户在字段中键入 0,则这不是空字符串。空字符串实际上是一个“空”字符串。
  • 如果用户键入 0,该值将是“0”字符串而不是数字 0。所以我猜它仍然是真的。请发布您的最佳解决方案,KingOfHypocrites?
  • KingOfHypocrites 是正确的,字符串 "0" 会给出不正确的结果:"0" == false 返回 true !"0" == false 返回 true
  • 有用。不过知道你的数据集。如果“0”或“false”是可能的输入,那么这个解决方案可能并不总是有效。
【解决方案2】:

如果出于某种原因,您只想测试 nullempty,您可以这样做:

function isNullOrEmpty( s ) 
{
    return ( s == null || s === "" );
}

注意:这也将捕获未定义为 cmets 中提到的@Raynos。

【讨论】:

  • 可以优化为return (s == null || s === "")
  • @Raynos - 为什么 == 而不是 === 相对于 null?如果传入的对象未定义怎么办? undefined null,对吧?
  • s == null 捕获 nullundefined,因为 null == undefined
  • @Raynos - 好的,我明白了,但如果您想真正了解 undefined 或 null,您需要使用 ===,对吗?
【解决方案3】:
【解决方案4】:

如果您知道 string 不是数字,这将起作用:

if (!string) {
  .
  .
  .

【讨论】:

  • 字符串“0”是假的,所以你不必检查字符串是否为数字。
  • 是的,字符串 "0" 是真的,但如果变量 string 实际上不是字符串而是 Number 零,那么 string 将是假的。如果我们知道string 是一个实际的字符串或空值,这将不是问题。
【解决方案5】:

你可以这样做

if(!string)
{
  //...
}

这将检查string 中的未定义、空和空字符串。

【讨论】:

    【解决方案6】:

    需要明确的是,if(!theString){//...} 其中 theString 是一个未声明的变量将抛出一个未定义的错误,而不是发现它是真的。另一方面,如果您有:if(!window.theString){//...}var theString; if(!theString){//...},它将按预期工作。在可能未声明变量的情况下(而不是作为属性或根本未设置),您需要使用:if(typeof theString === 'undefined'){//...}

    我的偏好是创建一个原型函数来为你包装它。

    【讨论】:

      【解决方案7】:

      由于标记为正确的答案包含一个小错误,因此这是我想出解决方案的最佳尝试。我有两个选项,一个接受字符串,另一个接受字符串或数字,因为我假设很多人在 javascript 中混合字符串和数字。

      步骤: - 如果对象为 null,则为 null 或空字符串。 - 如果类型不是字符串(或数字),则它的字符串值为 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;
      }
      

      【讨论】:

        【解决方案8】:

        你可以按逻辑说出来

        假设你有一个变量名 strVal,检查它是 null 还是空

        if (typeof (strVal) == 'string' && strVal.length > 0) 
        {
        // is has a value and it is not null  :)
        }
        else
        {
        //it is null or empty :(
        }
        

        【讨论】:

          【解决方案9】:

          您可以创建一种实用方法,可以在许多地方重复使用,例如:

           function isNullOrEmpty(str){
              var returnValue = false;
              if (  !str
                  || str == null
                  || str === 'null'
                  || str === ''
                  || str === '{}'
                  || str === 'undefined'
                  || str.length === 0 ) {
                  returnValue = true;
              }
              return returnValue;
            }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2015-01-22
            • 1970-01-01
            • 2021-11-26
            • 1970-01-01
            • 1970-01-01
            • 2010-11-25
            • 2011-10-28
            相关资源
            最近更新 更多