简体   繁体   English

检查字符串是空还是空的最简单方法

[英]Easiest way to check if string is null or empty

I've got this code that checks for the empty or null string. 我有这个代码检查空字符串或空字符串。 It's working in testing. 它正在测试中。

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}"

What I'm wondering is if there's a better way than not email? or email is '' 我想知道的是,有not email? or email is ''not email? or email is ''更好的方式not email? or email is '' not email? or email is '' . not email? or email is '' Can I do the equivalent of C# string.IsNullOrEmpty(arg) in CoffeeScript with a single call? 我可以通过一次调用在CoffeeScript中完成相应的C# string.IsNullOrEmpty(arg)吗? I could always define a function for it (like I did) but I'm wondering if there's something in the language that I'm missing. 我总是可以为它定义一个函数(就像我一样),但我想知道是否有一些我缺少的语言。

Yup: 对:

passwordNotEmpty = not not password

or shorter: 或更短:

passwordNotEmpty = !!password

It isn't entirely equivalent, but email?.length will only be truthy if email is non-null and has a non-zero .length property. 它不完全等价,但如果email非空并且具有非零.length属性,则email?.length将只是真实的。 If you not this value the result should behave as you want for both strings and arrays. 如果not这个值,结果应该按字符串和数组的方式运行。

If email is null or doesn't have a .length , then email?.length will evaluate to null , which is falsey. 如果emailnull或没有.length ,那么email?.length将评估为null ,这是假的。 If it does have a .length then this value will evaluate to its length, which will be falsey if it's empty. 如果它确实有一个.length那么这个值将计算到它的长度,如果它是空的,它将是假的。

Your function could be implemented as: 您的功能可以实现为:

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

This is a case where "truthiness" comes in handy. 这是“真实性”派上用场的情况。 You don't even need to define a function for that: 您甚至不需要为此定义函数:

test1 = not (email and password)

Why does it work? 它为什么有效?

'0'       // true
'123abc'  // true
''        // false
null      // false
undefined // false
unless email? and email
  console.log 'email is undefined, null or ""'

First check if email is not undefined and not null with the existential operator, then if you know it exists the and email part will only return false if the email string is empty. 首先检查电子邮件是否未定义,并且对于存在运算符不是null,然后如果您知道它存在and email部分仅在电子邮件字符串为空时才返回false。

You can use the coffeescript or= operation 您可以使用coffeescript或=操作

s = ''    
s or= null

如果您需要检查内容是否为字符串,而不是null而不是数组,请使用简单的比较类型:

 if typeof email isnt "string"

Here is a jsfiddle demonstrating a very easy way of doing this. 这是一个jsfiddle,演示了一个非常简单的方法。

Basicly you simply do this is javascript: 基本上你只是这样做是javascript:

var email="oranste";
var password="i";

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

In coffescript: 在coffescript中:

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

Hope this helps someone :) 希望这有助于某人:)

I think the question mark is the easiest way to call a function on a thing if the thing exists. 我认为问号是在事物存在时调用函数的最简单方法。

for example 例如

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

you want to get the color, but only if the car exists... 你想得到颜色,但只有汽车存在...

coffeescript: CoffeeScript的:

 car?.color

translates to javascript: 转换为javascript:

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

it is called the existential operator http://coffeescript.org/documentation/docs/grammar.html#section-63 它被称为存在主义运算符http://coffeescript.org/documentation/docs/grammar.html#section-63

I'm pretty sure @thejh 's answer was good enough to check empty string BUT, I think we frequently need to check that 'Does it exist?' 我很确定@thejh的答案足以检查空字符串但我认为我们经常需要检查“它是否存在?” and then we need to check 'Is it empty? 然后我们需要检查'它是空的吗? include string, array and object' 包括字符串,数组和对象'

This is the shorten way for CoffeeScript to do this. 这是CoffeeScript执行此操作的缩短方式。

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

If we keep this question order, that would be checked by this order 1. does it exist? 如果我们保留此问题顺序,将通过此订单检查1.它是否存在? 2. not empty string? 2.不是空字符串? 3. not empty object? 3.不是空物?

so there wasn't any problems for all variable even in the case of not existed. 所以即使在不存在的情况下,所有变量都没有任何问题。

Based on this answer about checking if a variable has a truthy value or not , you just need one line: 基于这个关于检查变量是否具有truthy值的答案 ,您只需要一行:

result = !email or !password

& you can try it for yourself on this online Coffeescript console 你可以在这个在线Coffeescript控制台上亲自尝试一下

Instead of the accepted answer passwordNotEmpty = !!password you can use 您可以使用passwordNotEmpty = !!password而不是接受的passwordNotEmpty = !!password

passwordNotEmpty = if password then true else false

It gives the same result (the difference only in syntax). 它给出了相同的结果(仅在语法上有所不同)。

In the first column is a value, in the second is the result of if value : 在第一列是一个值,在第二列是if value的结果:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将字符串转换为数字或null的简单方法? - Easy way to convert a string to a number or null? 测试coffeescript中的类成员资格的最简单方法是什么? - What is the easiest way to test for class membership in coffeescript? jQuery和CoffeeScript:检查值是否为表格单元格值的子字符串的更优雅的方法是什么 - jQuery and CoffeeScript: What's a more elegant way to check if value is sub-string of table cell value 检查边界的更好方法 - Better way to check bounds 在用户之间同步骨干模型状态的最简单方法是什么? Node.js的 - What's the easiest way to sync the state of backbone models across users? Node.js 在库加载后将JS排队运行的最简单方法是什么? (页面末尾的jQ) - What's the easiest way to queue JS to run after a library loads? (jQ at end of page) 检查CoffeeScript中嵌套for循环中的数组是否为null - Check if array is null in nested for loop in CoffeeScript 如何检查变量是否为String类型 - How to check if variable is String type 测试是否定义了变量,如果没有定义,请在Coffeescript中为它分配一个空字符串? - Test if a variable is defined, if not, assign it an empty string in Coffeescript? 为什么CoffeeScript的字符串插值会在表达式之前添加一个空字符串? - Why does CoffeeScript's string interpolation prefix the expression with an empty string?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM