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

前端 未结 10 1201
忘了有多久
忘了有多久 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 21:47

    With java 8 you can now use Optional class for this:

    import static java.util.Optional.ofNullable;
    ...
    String myString = null;
    System.out.printf("myString: %s",
        ofNullable(myString).orElse("Not found")
    );
    

提交回复
热议问题