在java中格式化字符串数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18335701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Formatting a string array in java
提问by ashu
I have a string array which has k elements. I want to print them out using System.out.format, but the issue is that I do not know k. So essentially, I want to use something like:
System.out.format("%s %s ... k times", str1, str2, ... strk);
(where k is a variable)
我有一个包含 k 个元素的字符串数组。我想使用 System.out.format 将它们打印出来,但问题是我不知道 k。所以本质上,我想使用类似的东西:(
System.out.format("%s %s ... k times", str1, str2, ... strk);
其中 k 是一个变量)
I was looking through the java documentation, but could not find a way to do this. Is there a simple way out?
我正在查看 java 文档,但找不到执行此操作的方法。有没有简单的出路?
Thanks!
谢谢!
采纳答案by duffymo
Use a loop:
使用循环:
for (String s : array) {
System.out.print(String.format("%s ", s);
}
System.out.println();
回答by Tricky12
for(int i = 0; i < array.length; i++) {
System.out.print("%s ", array[i]);
}
回答by Jean Logeart
Try:
尝试:
StringBuilder sb = new StringBuilder();
for(String s : myArray){
sb.append(s).append(" ");
}
sb.append(myArray.length).append(" times");
System.out.println(sb.toString()); // print the string
回答by Ruchira Gayan Ranaweera
you can use
您可以使用
System.out.format("%s". Arrays.toString(your_array));
回答by Jesper
Do you simply want to concatenate kstrings with a space between each of the strings? You don't need System.out.format
for that. You could simply create a loop to concatenate them together with a StringBuilder
:
您是否只是想将k 个字符串与每个字符串之间的空格连接起来?你不需要System.out.format
那个。您可以简单地创建一个循环将它们与 a 连接在一起StringBuilder
:
public String concatStrings(String... s) {
StringBuilder sb = new StringBuilder();
if (s.length > 0) {
sb.append(s[0]);
for (int i = 1; i < s.length; i++) {
sb.append(' ').append(s[i]);
}
}
return sb.toString();
}
回答by Reimeus
I want to specify the number of characters that I want for each string. Something like %15s
我想为每个字符串指定我想要的字符数。类似于 %15s
That will only specify the padding for each String
. If the String
length is less than the value specified in the format specifier, then the full String
is used. You could use substring
那只会指定每个String
. 如果String
长度小于格式说明符中指定的值,则使用完整String
的。你可以用substring
void displayArray(String[] str, int characters) {
for (String s: str) {
System.out.print(s.substring(0, Math.min(s.length(), characters)) + " ");
}
System.out.println();
}
回答by Peter Walser
That's typical toolbox code- code you often use and reuse, and best keep in a static-access utility class (such as StringUtil
). Here's a generic function that works on all kinds of non-primitive arrays, and lets you specify the separator (space, comma, slash, whatever):
这是典型的工具箱代码- 您经常使用和重用的代码,最好保留在静态访问实用程序类(例如StringUtil
)中。这是一个适用于各种非原始数组的通用函数,并允许您指定分隔符(空格、逗号、斜杠等):
public static <T> void print (PrintStream out, String separator, T... elements) {
for (int i = 0; i < elements.length; i++) {
if (i > 0) {
out.print(separator);
}
out.print(elements[i]);
}
}
Example Usage:
示例用法:
String[] s = {"A", "B", "C", "D", "E"};
Integer[] n = {1, 2, 3, 4, 5}; //non-primitive
print(System.out, " ", s);
print(System.out, ", ", n);
回答by Dominic Fox
Java 8:
爪哇 8:
String formatted = Stream.of(arrayOfStrings)
.collect(Collectors.joining(",","[","]"));
String formatted = Stream.of(arrayOfSomethingElse)
.map(Object::toString)
.collect(Collectors.joining(",","[","]"));