Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easiest way to check if string is null or empty

Tags:

coffeescript

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.

like image 209
jcollum Avatar asked Nov 14 '11 20:11

jcollum


People also ask

How do you check if a string is null or blank?

You can use the IsNullOrWhiteSpace method to test whether a string is null , its value is String. Empty, or it consists only of white-space characters.

Does isEmpty check for null?

isEmpty(<string>) Checks if the <string> value is an empty string containing no characters or whitespace. Returns true if the string is null or empty.

How do you check if a string is not null or empty in Java?

isEmpty(String) , which checks if a string is an empty string or null. It also provides the StringUtils. isBlank(String) method, which also checks for whitespace. That's all about determining whether a String is empty or null in Java.

How do you check for blank strings?

Both String#isEmpty and String#length can be used to check for empty strings. To be precise, String#trim will remove all leading and trailing characters with a Unicode code less than or equal to U+0020.


1 Answers

Yup:

passwordNotEmpty = not not password 

or shorter:

passwordNotEmpty = !!password 
like image 158
thejh Avatar answered Sep 21 '22 11:09

thejh