Get empty string when null

后端 未结 7 737
予麋鹿
予麋鹿 2020-12-07 19:55

I want to get string values of my fields (they can be type of long string or any object),

if a field is null then it should return empty string, I did this with guav

7条回答
  •  执笔经年
    2020-12-07 20:25

    If you don't mind using Apache commons, they have a StringUtils.defaultString(String str) that does this.

    Returns either the passed in String, or if the String is null, an empty String ("").

    If you also want to get rid of "null", you can do:

    StringUtils.defaultString(str).replaceAll("^null$", "")
    

    or to ignore case:

    StringUtils.defaultString(str).replaceAll("^(?i)null$", "")
    

提交回复
热议问题