One step check for null value & emptiness of a string

前端 未结 7 1086
一向
一向 2020-12-18 23:51

I have a setter method.

Then when another (say generate) method is run, I need to check the value of my fields. So in the case of String property, I need to know if

相关标签:
7条回答
  • 2020-12-19 00:30

    Add maven dependency for com.google.guava

    Then in your code:

    import com.google.common.base.Strings;
    
    if(!Strings.isNullOrEmpty(s)) {
      // Do stuff here
    }
    
    0 讨论(0)
  • 2020-12-19 00:41
    if(s != null && !s.isEmpty()){
    // this will work even if 's' is NULL
    }
    
    0 讨论(0)
  • 2020-12-19 00:42

    try this

    if(s){

    }

    0 讨论(0)
  • 2020-12-19 00:45

    use org.apache.commons.lang.StringUtils, the method StringUtils.isNotBlank check both nullity and emptiness.

    0 讨论(0)
  • 2020-12-19 00:47

    Commons library, StringUtils.isBlank() or StringUtils.isEmtpy().

    isEmpty is equivalent to

    s == null || s.length() == 0
    

    isBlank is equivalent to

    s == null || s.trim().length() == 0
    
    0 讨论(0)
  • 2020-12-19 00:48

    In the jakarta commons there is a StringUtils.isEmpty(String).

    0 讨论(0)
提交回复
热议问题