Check whether a string is not null and not empty

后端 未结 30 2120
予麋鹿
予麋鹿 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:55

    To check on if all the string attributes in an object is empty(Instead of using !=null on all the field names following java reflection api approach

    private String name1;
    private String name2;
    private String name3;
    
    public boolean isEmpty()  {
    
        for (Field field : this.getClass().getDeclaredFields()) {
            try {
                field.setAccessible(true);
                if (field.get(this) != null) {
                    return false;
                }
            } catch (Exception e) {
                System.out.println("Exception occurred in processing");
            }
        }
        return true;
    }
    

    This method would return true if all the String field values are blank,It would return false if any one values is present in the String attributes

提交回复
热议问题