如果我有,
String[] s = new String[3];
s[0] = "Ap";
s[1] = "p";
s[2] = "le";
String result = ?
Run Code Online (Sandbox Code Playgroud)
如果我想在没有循环的情况下让苹果公司退出,我该怎么做?
任何捷径?
如果不循环对你来说比防止导入另一个库更重要,或者你已经使用了apache commons lang,那么你可以使用StringUtils.join方法
import org.apache.commons.lang.StringUtils;
String joined = StringUtils.join(s, "");
Run Code Online (Sandbox Code Playgroud)
也许Apache Commons还有其他可能对您的项目感兴趣的方法.我发现它们是本机Java库中缺少功能的非常有用的资源.
如果您知道数组的长度,则可以轻松执行以下操作:
String result = s[0] + s[1] +s[2];
Run Code Online (Sandbox Code Playgroud)
另一种选择是执行以下操作(这纯粹是学术性的,我不会在现实场景中使用它,因为它会从字符串中删除[, ], 和):<space>
String result = Arrays.toString(s).replaceAll("[\\]\\[, ]", "");
Run Code Online (Sandbox Code Playgroud)
还有另一种选择,与第一次尝试一起进行,但使用类似 C 的格式化程序:
System.out.println(String.format("%s%s%s", s));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7552 次 |
| 最近记录: |