在Cactoos框架中是否有某种文本装饰器(或者其他方式),可以将空字符串替换为空字符串?比如Google Guava中的Strings.nullToEmpty函数。
我已经找到了NoNulls装饰器,但我只需要替换而不抛出异常。
所以它必须看起来像这样:
String someNullString = null;
new StrictEmptyText(
new TextOf(someNullString) // this row produces NPE for now
).asString(); // ""非常感谢你的帮助。
发布于 2019-03-04 04:35:04
不,没有任何Text实现可以直接为您完成此任务。
使用纯cactoos:
new TextOf(
new UncheckedScalar<>(
new Ternary<>(
someNullString != null,
someNullString,
""
)
).value()
)发布于 2019-03-04 03:12:55
您可以在Java8中使用Optional::ofNullable,如下所示:
String str = Optional.ofNullable(someNullString)
.orElse(""); // return empty if someNullString is null or someNullString if not nullhttps://stackoverflow.com/questions/54972616
复制相似问题