Check whether a string is not null and not empty

后端 未结 30 2461
予麋鹿
予麋鹿 2020-11-22 02:13

How can I check whether a string is not null and not empty?

public void doStuff(String str)
{
    if (str != null && str != \"**here I want to check          


        
30条回答
  •  无人共我
    2020-11-22 03:01

    You can use StringUtils.isEmpty(), It will result true if the string is either null or empty.

     String str1 = "";
     String str2 = null;
    
     if(StringUtils.isEmpty(str)){
         System.out.println("str1 is null or empty");
     }
    
     if(StringUtils.isEmpty(str2)){
         System.out.println("str2 is null or empty");
     }
    

    will result in

    str1 is null or empty

    str2 is null or empty

提交回复
热议问题