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

}

How to check if the string is not null?

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

}
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
sandeep
  • 59
  • 1
  • 1
  • 4

13 Answers13

42

Checking for null is done via if (string != null)

If you want to check if its null or empty - you'd need if (string != null && !string.isEmpty())

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

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • 2
    +1 for StringUtils, again. Everyone of us should spend a couple of minutes a day reading what apache commons can do for us! – Pablo Grisafi Mar 31 '11 at 15:40
5

You can do it with the following code:

 if (string != null) {

 }
RoflcoptrException
  • 51,941
  • 35
  • 152
  • 200
2

Nothing really new to add to the answers above, just wrapping it into a simple class. Commons-lang is quite all right but if all you need are these or maybe a few more helper functions, rolling your own simple class is the easiest approach, also keeping executable size down.

public class StringUtils {

  public static boolean isEmpty(String s) {
    return (s == null || s.isEmpty());
  }

  public static boolean isNotEmpty(String s) {
    return !isEmpty(s);
  }
}
shmosel
  • 49,289
  • 6
  • 73
  • 138
Gábor
  • 9,466
  • 3
  • 65
  • 79
2

Checking for null is done by:

string != null

Your example is actually checking for the empty string

You can combine the two like this:

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

But null and empty are two different things

brain
  • 5,496
  • 1
  • 26
  • 29
1
if(str != null && !str.isEmpty())

Be sure to use the parts of && in this order, because java will not proceed to evaluating the the second if the first part of && fails, thus ensuring you will not get a null pointer exception from str.isEmpty() if str is null.

Beware, it's only available since Java SE 1.6.

You have to check str.length() == 0 or str.equals("") on previous versions.

Jarvis
  • 8,494
  • 3
  • 27
  • 58
Kalpana
  • 491
  • 1
  • 7
  • 21
1

Use TextUtils Method.

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

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

Source of TextUtils class

isEmpty Method :

 public static boolean isEmpty(CharSequence str) {
        if (str == null || str.length() == 0)
            return true;
        else
            return false;
    }
Kishan Vaghela
  • 7,678
  • 5
  • 42
  • 67
0

The best way to check a String is :

import org.apache.commons.lang3.StringUtils;

if(StringUtils.isNotBlank(string)){
....
}

From the doc :

isBlank(CharSequence cs) :

Checks if a CharSequence is empty (""), null or whitespace only.

Community
  • 1
  • 1
aayoustic
  • 99
  • 9
0

As everyone is saying, you'd have to check (string!=null), in objects you're testing the memory pointer.

because every object is identified by a memory pointer, you have to check your object for a null pointer before testing anything else, so:

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

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

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

IvoC
  • 86
  • 7
0

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

You can write code to check if string is not null and not empty:

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

Then you can test it:

System.out.println(isNotEmpty.test(""));      // false
System.out.println(isNotEmpty.test(null));    // false
System.out.println(isNotEmpty.test("null"));  // true
lczapski
  • 4,026
  • 3
  • 16
  • 32
0

A common way for testing null string in Java is with Optionals:

Optional.ofNullable(myString).orElse("value was null")
Optional.ofNullable(myString).ifPresent(s -> System.out.println(s));
Optional.ofNullable(myString).orElseThrow(() -> new RuntimeException("value was null"));

And to test if it is null or empty you can use Apache org.apache.commons.lang3 library that gives you the following methods:

  • StringUtils.isEmpty(String) / StringUtils.isNotEmpty(String): It tests if the String is null or empty (" " is not empty)
  • StringUtils.isBlank(String) / StringUtils.isNotBlank(String): Same as isEmpty bt if the String is only whitespace it is considered blank

And applied to Optional you get:

Optional.ofNullable(myString).filter(StringUtils::isNotEmpty).orElse("value was null or empty");
m4nu56
  • 47
  • 1
  • 1
  • 9
  • Do not do this. It is only complicated without any benefit over the accepted answer. – Donat Oct 04 '20 at 20:32
  • It's a one-liner that does the same as ```java if(myString != null){ ... } ``` That you find it complicated it's a thing but I assume you find Java lambda complicated to read too ? – m4nu56 Oct 05 '20 at 20:30
0

Try using Strings.isNullOrEmpty("") from com.google.common.base.Strings this method returns boolean value and checks for both null and empty string.

Ashok kumar Ganesan
  • 1,098
  • 5
  • 20
  • 48
-2
if(string != null)

or

if(string.length() == 0)

or

if(("").equals(string))
David Machel
  • 286
  • 2
  • 12
-4

u can try this

if(string != null)
Akshatha
  • 599
  • 2
  • 10
  • 26