Check whether a string is not null and not empty

后端 未结 30 2119
予麋鹿
予麋鹿 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 02:58

    I've made my own utility function to check several strings at once, rather than having an if statement full of if(str != null && !str.isEmpty && str2 != null && !str2.isEmpty). This is the function:

    public class StringUtils{
    
        public static boolean areSet(String... strings)
        {
            for(String s : strings)
                if(s == null || s.isEmpty)
                    return false;
    
            return true;
        }   
    
    }
    

    so I can simply write:

    if(!StringUtils.areSet(firstName,lastName,address)
    {
        //do something
    }
    

提交回复
热议问题