StringBuilder append() and null values

后端 未结 8 922
借酒劲吻你
借酒劲吻你 2021-02-06 21:54

I have a list of Strings, and I want to concatenate them with spaces in between. So I\'m using StringBuilder. Now if any of the Strings ar

8条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-06 22:36

    import java.util.Objects;
    ...
    sb.append(Objects.toString(s, ""));
    

    Uses Java internal utils, no external libs are needed. toString takes String, checks if it is null and if it is null, then returns specified default value, in this case "" (empty string), if it is not null it returns provided string.

提交回复
热议问题