Make String.format(“%s”, arg) display null-valued arguments differently from “null”

前端 未结 10 1462
忘了有多久
忘了有多久 2021-02-06 21:22

Consider the custom toString() implementation of a bean:

@Override
public String toString() {
    String.format(\"this is %s\", this.someField);
}
<         


        
10条回答
  •  不要未来只要你来
    2021-02-06 22:04

    If you don't want to use replaceAll(), You can assign a default text(String) for someField.

    But if some time this may assign null again. So you can use validation for that case

     this.someField == null ? "defaultText" : this.someField
    

提交回复
热议问题