How to check if my string is equal to null ?

How to check if my string is equal to null ?

Asked on October 26, 2018 in String.
Add Comment


  • 3 Answer(s)

    This code is used for checking:

    if (myString != null && !myString.isEmpty())
    {
      // doSomething
    }
    
    Answered on October 26, 2018.
    Add Comment

                If the given String has the null, then makes calling myString.equals (null) or myString.equals(“”) would fail with the NullPointerException.

               There is no way to call any instance methods on a null variable.

               Check if there is null :

    if (myString != null && !myString.equals(""))
    {
    //do something
    }
    
    Answered on October 26, 2018.
    Add Comment

    Apache commons StringUtils.isNotEmpty is the best way to check if the given string is equal to null .

    Answered on October 26, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.