简体   繁体   中英

Checking for a string/string[] to be null or “” in java

I need to check if a input String or a String[] is null or "" for computing within a method in java.

This method takes argument as String ...

I am using the below condition; can somebody check and let me know what I am wrong here,

if(tokens==null){
    return;
} else if(tokens[0] != null && tokens[0]!=""){
    return;
} 

tokens[0]!="" is basically comparing the memory references of tokens[0] and "" , which is not what you want to do. Instead, you need to actually check the contents of the String , for example

else if(tokens[0] != null && tokens[0].isEmpty()){

Update...

You question is a little werid...you can't have a method that takes a String then try and use it as an array, unless that method takes varargs , then you would treat it as an array anyway.

public boolean isEmpty(String... values) {
    boolean isEmpty = values == null || values.length == 0;
    if (!isEmpty) {
        for (String value : values) {
            if (value == null || value.isEmpty()) {
                isEmpty = false;
                break;
            }
        }
    }
    return isEmpty;
}

This means you could use...

isEmpty("");
isEmpty("I'm not an array");
isEmpty("Check 1", "Check 2");
isEmpty(new String[]{"Check 1", "Check 2"});

Or similar....

There is no need to write two conditions.

You just try with isEmpty() ,and only one condition.

I need to check if a input String or a String[] is null or ""

That is not possible.Either the argument will be string or array ,

if tokens is an array,

if(tokens[0] !=null && tokens[0].isEmpty()){
return;
}

if tokens is a String,

if(tokens !=null && tokens.isEmpty()){
return;
}

Assuming the method takes an argument of String (not String[]) then:

if(tokens == null || tokens.length() == 0) {
    return;
}

If the method takes an argument of String[], you need to decide if you want to check:

  1. if the array is null
  2. if the array contains any elements (.length == 0)
  3. if any elements are null
  4. if any elements are zero length

Assuming you want to check all four, you could try:

if(tokens == null || tokens.length == 0) {
    return;
} else {
    for(final String token : tokens) {
        if(token == null || token.length() == 0) {
            return;
        }
    }
}

Instead of this:

} else if(tokens[0] != null && tokens[0]!=""){

Don't you want this:

} else if(tokens[0] == null || tokens[0]==""){

IE the logic is inverted.

Try the utility libraries that handle the check for null and empty concisely for String and Collections :

import org.apache.commons.collections.CollectionUtils;
import org.springframework.util.StringUtils;

if(CollectionUtils.isEmpty(tokens))
    return; // tests that the Collection parameter is neither null nor empty

for(String token : tokens)
{
  if(!StringUtils.hasText(token)) // tests that the String parameter is neither null nor empty
    return;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM