如何使用番石榴连接字符串? [英] How to concatenate strings using Guava?

查看:89
本文介绍了如何使用番石榴连接字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一些代码来连接字符串:

I wrote some code to concatenate Strings:

String inputFile = "";      

for (String inputLine : list) {
    inputFile +=inputLine.trim());
}

但是我不能使用+进行连接,所以我决定使用番石榴.因此,我需要使用Joiner.

But I can't use + to concatenate, so I decide to go with Guava. So I need to use Joiner.

inputFile =joiner.join(inputLine.trim());

但这给我一个错误.我需要帮助来解决此问题.非常感谢.

But it's giving me an error. I need help to fix this. Many Thanks.

推荐答案

您不需要循环,可以使用Guava进行以下操作:

You don't need the loop, you can do the following with Guava:

// trim the elements:
List<String> trimmed = Lists.transform(list, new Function<String, String>() {
    @Override
    public String apply(String in) {
        return in.trim();
    }
});
// join them:
String joined = Joiner.on("").join(trimmed);

这篇关于如何使用番石榴连接字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆