Check whether a string is not null and not empty

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

    With Java 8 Optional you can do:

    public Boolean isStringCorrect(String str) {
        return Optional.ofNullable(str)
                .map(String::trim)
                .map(string -> !str.isEmpty())
                .orElse(false);
    }
    

    In this expression, you will handle Strings that consist of spaces as well.

    0 讨论(0)
  • 2020-11-22 02:55

    How about:

    if(str!= null && str.length() != 0 )
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 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
    }
    
    0 讨论(0)
  • 2020-11-22 02:58

    In case you are using Java 8 and want to have a more Functional Programming approach, you can define a Function that manages the control and then you can reuse it and apply() whenever is needed.

    Coming to practice, you can define the Function as

    Function<String, Boolean> isNotEmpty = s -> s != null && !"".equals(s)
    

    Then, you can use it by simply calling the apply() method as:

    String emptyString = "";
    isNotEmpty.apply(emptyString); // this will return false
    
    String notEmptyString = "StackOverflow";
    isNotEmpty.apply(notEmptyString); // this will return true
    

    If you prefer, you can define a Function that checks if the String is empty and then negate it with !.

    In this case, the Function will look like as :

    Function<String, Boolean> isEmpty = s -> s == null || "".equals(s)
    

    Then, you can use it by simply calling the apply() method as:

    String emptyString = "";
    !isEmpty.apply(emptyString); // this will return false
    
    String notEmptyString = "StackOverflow";
    !isEmpty.apply(notEmptyString); // this will return true
    
    0 讨论(0)
  • 2020-11-22 02:59

    It is a bit too late, but here is a functional style of checking:

    Optional.ofNullable(str)
        .filter(s -> !(s.trim().isEmpty()))
        .ifPresent(result -> {
           // your query setup goes here
        });
    
    0 讨论(0)
提交回复
热议问题