How to check a string is not null?

if(string.equals("")) { } 

How to check if a string is a string?

 if(!string.equals("")) { } 
+4
source share
11 answers

Checking a null value is done using if (string != null)

If you want to check if it is null or empty, you will need if (string != null && !string.isEmpty())

I prefer to use commons-lang StringUtils.isNotEmpty(..)

+37
source

You can do this with the following code:

  if (string != null) { } 
+4
source

Checking the zero value is done using:

 string != null 

Your example actually checks for an empty string

You can combine these two types:

 if (string != null && !string.equals("")) { ... 

But null and empty are two different things.

+2
source

Nothing really new to add to the answers above, just wrap it in a simple class. Commons-lang is fine, but if you only need these or maybe a few more helper functions, the easiest way is to use your own simple class and reduce the size of the executable.

 public class StringUtils { public static boolean isEmpty(String s) { return (s == null || s.isEmpty()); } public static boolean isNotEmpty(String s) { return !isEmpty(s); } } 
+2
source
 if(str != null && !str.isEmpty()) 

Be sure to use the && parts in this order, since java will not continue to evaluate the second if the first && & part fails, thereby ensuring that you will not get an exception from the null pointer from str.isEmpty () if str is null.

Beware, it is only available with Java SE 1.6.

You should check str.length() == 0 or str.equals("") in previous versions.

+1
source

Use the TextUtils method.

TextUtils.isEmpty (str) : Returns true if the string is null or 0-length. Parameters: str checked string Returns: true if str has zero or zero length

 if(TextUtils.isEmpty(str)){ // str is null or lenght is 0 } 

Source class TextUtils

isEmpty:

  public static boolean isEmpty(CharSequence str) { if (str == null || str.length() == 0) return true; else return false; } 
+1
source

As everyone says, you will need to check (string! = Null), in the objects that you are testing the memory pointer.

since each object is identified by a pointer to memory, you must check your object for a null pointer before testing anything else, therefore:

(string! = null &! string.equals ("")) good

(! string.equals ("") && string! = null) can give you a nullpointer exception.

if you don't need trailing spaces, you can always use trim () before equals () so "and" "gives you the same result

0
source

The best way to check the string:

 import org.apache.commons.lang3.StringUtils; if(StringUtils.isNotBlank(string)){ .... } 

From the doc :

isBlank (CharSequence cs):

Checks if CharSequence is empty (""), null or just spaces.

0
source

You can use Predicate and its new method (since Java 11) Predicate :: not

You can write code to check if the string is neither empty nor empty:

 Predicate<String> notNull = Predicate.not(Objects::isNull); Predicate<String> notEmptyString = Predicate.not(String::isEmpty); Predicate<String> isNotEmpty = notNull.and(notEmptyString); 

Then you can check this:

 System.out.println(isNotEmpty.test("")); // false System.out.println(isNotEmpty.test(null)); // false System.out.println(isNotEmpty.test("null")); // true 
0
source
 if(string != null) 

or

 if(string.length() == 0) 

or

 if(("").equals(string)) 
-2
source

u can try this

 if(string != null) 
-4
source

Source: https://habr.com/ru/post/1346040/


All Articles