检查字符串是否为空或空的最简单方法
我有这段代码可以检查空字符串或空字符串。它正在测试中。
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}"
我想知道是否有比不发送电子邮件更好的方法?或者电子邮件是“”。我可以通过一次调用在 CoffeeScript 中执行与 C# string.IsNullOrEmpty(arg)
相同的操作吗?我总是可以为它定义一个函数(就像我所做的那样),但我想知道语言中是否缺少一些东西。
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 ''
. Can I do the equivalent of C# string.IsNullOrEmpty(arg)
in CoffeeScript with a single call? 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
是的:
或更短:
Yup:
or shorter:
它并不完全等效,但仅当
email
为非空且.length
为非零且.length
时,email?.length
才会为真> 财产。如果您不
此值,则结果对于字符串和数组都应按照您的预期运行。如果
email
为null
或没有.length
,则email?.length
将计算为 < code>null,这是错误的。如果它确实有.length
,那么该值将计算其长度,如果它为空,则该值将为 false。您的函数可以实现为:
It isn't entirely equivalent, but
email?.length
will only be truthy ifemail
is non-null and has a non-zero.length
property. If younot
this value the result should behave as you want for both strings and arrays.If
email
isnull
or doesn't have a.length
, thenemail?.length
will evaluate tonull
, which is falsey. If it does have a.length
then this value will evaluate to its length, which will be falsey if it's empty.Your function could be implemented as:
在这种情况下,“真实”就派上用场了。您甚至不需要为此定义一个函数:
它为什么有效?
This is a case where "truthiness" comes in handy. You don't even need to define a function for that:
Why does it work?
首先使用存在运算符检查 email 是否未定义且不为 null,然后如果您知道它存在,则仅当电子邮件字符串为空时
and email
部分才会返回 false。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.您可以使用coffeescript或=操作
You can use the coffeescript or= operation
如果需要检查内容是否是字符串、不是 null 也不是数组,请使用简单的 typeof 比较:
If you need to check that the content is a string, not null and not an array, use a simple typeof comparison:
这是 jsfiddle 演示了一种非常简单的方法。
基本上你只需这样做就是 javascript:
In coffescript:
希望这对某人有帮助:)
Here is a jsfiddle demonstrating a very easy way of doing this.
Basicly you simply do this is javascript:
In coffescript:
Hope this helps someone :)
我认为问号是在事物存在的情况下调用该事物的函数的最简单方法。
例如,
您想获取颜色,但前提是汽车存在...
CoffeeScript:
翻译为 javascript:
它称为存在运算符 http://coffeescript.org/documentation/docs/grammar.html#section-63
I think the question mark is the easiest way to call a function on a thing if the thing exists.
for example
you want to get the color, but only if the car exists...
coffeescript:
translates to javascript:
it is called the existential operator http://coffeescript.org/documentation/docs/grammar.html#section-63
我很确定 @thejh 的答案足以检查空字符串但是,
我认为我们经常需要检查“它是否存在?”然后我们需要检查“它是空的吗?”包括字符串、数组和对象'
这是 CoffeeScript 执行此操作的简化方法。
如果我们保留这个问题的顺序,就会按这个顺序检查
1. 存在吗?
2.不是空字符串?
3.不是空对象?
所以即使在不存在的情况下,所有变量也没有任何问题。
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?' and then we need to check 'Is it empty? include string, array and object'
This is the shorten way for CoffeeScript to do this.
If we keep this question order, that would be checked by this order
1. does it exist?
2. not empty string?
3. not empty object?
so there wasn't any problems for all variable even in the case of not existed.
基于这个答案关于检查变量是否具有
truthy
值,你只需要一行:&你可以在这个在线 Coffeescript 控制台上亲自尝试一下
Based on this answer about checking if a variable has a
truthy
value or not , you just need one line:& you can try it for yourself on this online Coffeescript console
您可以使用代替接受的答案
passwordNotEmpty = !!password
它给出相同的结果(仅在语法上有所不同)。
第一列是一个值,第二列是
if value
的结果:Instead of the accepted answer
passwordNotEmpty = !!password
you can useIt 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
: