【问题标题】:Easiest way to check if string is null or empty检查字符串是否为空或为空的最简单方法
【发布时间】:2011-12-28 23:56:51
【问题描述】:

我有这个检查空字符串或空字符串的代码。它正在测试中。

eitherStringEmpty= (email, password) ->
  emailEmpty = not email? or email is ''
  passwordEmpty = not password? or password is ''
  eitherEmpty = emailEmpty || passwordEmpty         

test1 = eitherStringEmpty "A", "B" # expect false
test2 = eitherStringEmpty "", "b" # expect true
test3 = eitherStringEmpty "", "" # expect true
alert "test1: #{test1} test2: #{test2} test3: #{test3}"

我想知道是否有比not email? or email is '' 更好的方法。我可以通过一次调用在 CoffeeScript 中执行相当于 C#string.IsNullOrEmpty(arg) 的操作吗?我总是可以为它定义一个函数(就像我做的那样),但我想知道语言中是否有我遗漏的东西。

【问题讨论】:

    标签: coffeescript


    【解决方案1】:

    我很确定@thejh 的答案足以检查空字符串但是, 我认为我们经常需要检查“它是否存在?”然后我们需要检查'它是空的吗?包括字符串、数组和对象'

    这是 CoffeeScript 执行此操作的简化方式。

    tmp? and !!tmp and !!Object.keys(tmp).length
    

    如果我们保持这个问题的顺序,就会被这个顺序检查 1. 存在吗? 2.不是空字符串? 3. 不是空对象?

    所以即使在不存在的情况下,所有变量也没有任何问题。

    【讨论】:

      【解决方案2】:

      你可以使用coffeescript or=操作

      s = ''    
      s or= null
      

      【讨论】:

      • 哇,太棒了!谢谢!
      【解决方案3】:

      您可以使用passwordNotEmpty = !!password 而不是接受的答案

      passwordNotEmpty = if password then true else false
      

      它给出了相同的结果(只是语法上的不同)。

      第一列是一个值,第二列是if value的结果:

      0 - false
      5 - true
      'string' - true
      '' - false
      [1, 2, 3] - true
      [] - true
      true - true
      false - false
      null - false
      undefined - false
      

      【讨论】:

        【解决方案4】:

        基于this answer关于检查变量是否具有truthy值,您只需要一行:

        result = !email or !password
        

        &你可以在online Coffeescript console自己尝试一下

        【讨论】:

          【解决方案5】:
          unless email? and email
            console.log 'email is undefined, null or ""'
          

          首先使用存在运算符检查电子邮件是否未定义且不为空,然后如果您知道它存在,and email 部分将仅在电子邮件字符串为空时返回 false。

          【讨论】:

            【解决方案6】:

            如果事物存在,我认为问号是在事物上调用函数的最简单方法。

            例如

            car = {
              tires: 4,
              color: 'blue' 
            }
            

            你想得到颜色,但前提是汽车存在......

            咖啡脚本:

             car?.color
            

            翻译成javascript:

            if (car != null) {
              car.color;
            }
            

            称为存在运算符http://coffeescript.org/documentation/docs/grammar.html#section-63

            【讨论】:

            • 空字符串为存在检查返回真。
            【解决方案7】:

            是的:

            passwordNotEmpty = not not password
            

            或更短:

            passwordNotEmpty = !!password
            

            【讨论】:

            • 这是两个答案中更多的“javascript-y”。特别是如果您使用!! 版本,这是一种本质上转换为布尔值的常用方法。如果重要的话,这几乎肯定比 Jeremy 建议的定义函数要快。
            • 它们都有效,但这个得到了最多的支持。我喜欢stackoverflow.com/q/8127920/30946 的可读性。
            • 所有空格的字符串都失败了......所以对于空白无效
            • @Arkhitech blank 和 empty 不一样,所以可以。
            • 对于我的可读性来说太“聪明”了;不认为我会使用它,但干得好
            【解决方案8】:

            它不是完全等价的,但 email?.length 只有在 email 为非空且具有非零 .length 属性时才为真。如果你 not 这个值,结果应该对字符串和数组都表现得如你所愿。

            如果emailnull 或没有.length,那么email?.length 将评估为null,这是错误的。如果它确实有一个.length,那么这个值将计算它的长度,如果它是空的,这将是错误的。

            你的函数可以实现为:

            eitherStringEmpty = (email, password) ->
              not (email?.length and password?.length)
            

            【讨论】:

            • @pyrotechnick 这个问题没有询问如何区分数组和字符串。它询问如何区分空值和空字符串与非空字符串。我的观点是代码还可以将空值和空数组与非空数组区分开来,但这超出了问题的范围。
            • 不过,"eitherStringEmpty" 是您提供的方法的错误名称。
            【解决方案9】:

            这是一个jsfiddle,演示了一种非常简单的方法。

            基本上你只需这样做就是javascript:

            var email="oranste";
            var password="i";
            
            if(!(email && password)){
                alert("One or both not set");        
            }
            else{
                alert("Both set");   
            }
            

            在咖啡中:

            email = "oranste"
            password = "i"
            unless email and password
              alert "One or both not set"
            else
              alert "Both set"
            

            希望这可以帮助某人:)

            【讨论】:

              【解决方案10】:

              如果需要检查内容是字符串,不是null也不是数组,使用简单的typeof比较:

               if typeof email isnt "string"
              

              【讨论】:

              • typeof email isnt "string"'''a' 返回 false
              【解决方案11】:

              这是一个“真实”派上用场的情况。你甚至不需要为此定义一个函数:

              test1 = not (email and password)
              

              为什么有效?

              '0'       // true
              '123abc'  // true
              ''        // false
              null      // false
              undefined // false
              

              【讨论】:

              • 我假设您已经在值上调用了 .trim() 作为验证逻辑的一部分。答案更多是关于 CS 语法。
              猜你喜欢
              • 2015-06-05
              • 2017-08-19
              • 1970-01-01
              • 2010-09-27
              • 2011-05-30
              • 2012-07-13
              • 2015-06-12
              • 2020-05-29
              • 2017-01-10
              相关资源
              最近更新 更多