检查字符串是否为空或为空的最简单方法 [英] Easiest way to check if string is null or empty

查看:24
本文介绍了检查字符串是否为空或为空的最简单方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

推荐答案

是的:

passwordNotEmpty = not not password

或更短:

passwordNotEmpty = !!password

这篇关于检查字符串是否为空或为空的最简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆