英文:
Format text with a list of argument in java
问题
我有一个带有多个 %s 用于字符串格式化的字符串。我有一个字符串数组,这些字符串应该是字符串格式化的参数。像这样:
List<String> list = new ArrayList<>();
list.add("A");
list.add("B");
String toFormat = "这是第一个值 %s,这是第二个值 %s";
String result = String.format(toFormat, list.get(0), list.get(1));
但是在元素数量大于2的情况下,这看起来不太好。如何在不逐个从列表中选择每个参数的情况下格式化字符串?
英文:
I have a sting with multiple %s for string formatting. And i have an array of strings which are supposed to be arguments for string formatting. Like this
List<String> list = new ArrayList<>();
list.add("A");
list.add("B");
String toFormat = "This is a first value %s, This is a second value %s"
String result = String.formant (toFormat, list.get(0), list.get(1));
But it doesn't look good with a number of element greater than 2. How can i format a string without picking each argument from list individually?
答案1
得分: 3
public static void main(String args[]) {
List<String> list = new ArrayList<>();
list.add("A");
list.add("B");
String toFormat = "This is a first value %s, This is a second value %s";
String result = String.format(toFormat, list.toArray(new String[0]));
System.out.println(result);
}
英文:
String.format()
take as parameters a format and array of Objects (vararg is an array of Objects behind the scene). All you need to do is to convert your list to array of Strings and pass that to String.format()
, like so:
public static void main(String args[]) {
List<String> list = new ArrayList<>();
list.add("A");
list.add("B");
String toFormat = "This is a first value %s, This is a second value %s";
String result = String.format (toFormat, list.toArray(new String[0]));
System.out.println(result);
}
答案2
得分: 1
重新思考你的问题:
String toFormat = "这是第一个值%s,这是第二个值%s";
所以重点是:你有多个参数,但是每个参数都应该被特殊处理。意思是:假设你有3个参数。那么你的格式必须包括这是第三个值
。当你有4个参数时,字符串第四个值
...必须从某处获取!
如果这是你想要的,那么你需要一个额外的映射,像这样:
Map<Integer, String> namedPositionByIndex = ...
它将映射类似于(1, "第一个")
,(2, "第二个")
等等的对。
并且使用这个映射,你现在可以动态地组合一个字符串。当然,它只会对映射中最大的索引起作用。
英文:
Rethink your question:
String toFormat = "This is a first value %s, This is a second value %s"
So the point is: you have multiple arguments, but each argument should be treated specially. Meaning: assume you have 3 arguments. Then your format must include this is the third value
. And when you have 4 arguments, the string fourth
... must come from somewhere!
If that is what you want, then you need an additional mapping, like:
Map<Integer, String> namedPositionByIndex = ...
that maps pairs like (1, "first")
, (2, "second")
and so on.
And using that map, you can now pull together a string that works dynamically. Of course, it will only work for the largest index in your map.
答案3
得分: 0
使用字符串构建器(StringBuilder)并动态构建一个大字符串。
在Java8或更高版本中,你可以像这样做:
import java.util.List;
import java.util.ArrayList;
import java.lang.StringBuilder;
class Main {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("A");
list.add("B");
StringBuilder builder = new StringBuilder();
list.forEach(it -> {
builder.append(String.format("This is a first value %s,", it));
}
));
System.out.print(builder.toString());
}
}
结果看起来像:This is a first value A,This is a first value B,
你可以在forEach
内部添加更多代码来修复字符串模式。
英文:
Can you use the String Builder and use it to build a big string dynamically.
With Java8 or greater, you can do something like this
import java.util.List;
import java.util.ArrayList;
import java.lang.StringBuilder;
class Main {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("A");
list.add("B");
StringBuilder builder = new StringBuilder();
list.forEach(it -> {
builder.append(String.format("This is a first value %s,", it)
}
));
System.out.print(builder.toString());
}
}
The result looks like: This is a first value A,This is a first value B,
You can add more code inside the forEach
to fix the string patter
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论