有没有办法连接字符串,每个字符串都有一个特定的周围字符串? [英] Is there a way to join strings, each with a specific surrounding string?

查看:132
本文介绍了有没有办法连接字符串,每个字符串都有一个特定的周围字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望使用guava的 Joiner List< String> 加入到一个字符串中,但周围有列表中每个字符串周围的字符串。所以我想得一个字符串列表:

I'm looking to use guava's Joiner to join List<String> into one string, but with surrounding strings around each one in the list. So I want to take a list of Strings:

List<String> names = Arrays.asList("John", "Mary", "Henry");

并生成这一个字符串:

"your guest John is here, your guest Mary is here, your guest Henry is here"

我看到的使用 Joiner 的例子似乎是生成用逗号分隔的3个名字,但我想用每个字符串包围一些额外的字符串(每次都是相同的字符串)。

The examples I see of using Joiner seem to be to generate the 3 names separated by a comma, but I'm looking to surround each string with some extra strings (the same ones every time).

我希望我在这里足够清楚。感谢您的帮助。

I hope I'm being clear enough here. Thanks for your help.

推荐答案

执行此操作的方法是使用转换,首先:

The way to do this is with a transform, first:

 Joiner.on(", ").join(Iterables.transform(names, new Function<String, String>() {
   public String apply(String str) { return "your guest " + str + " is here"; }
 }));

这篇关于有没有办法连接字符串,每个字符串都有一个特定的周围字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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