在java中,参数数目可变的字符串格式

时间:2022-03-23 05:09:30

Consider a string.

考虑一个字符串。

String Str = "Entered number = %d and string = %s"

Let us say I have a list of objects

假设我有一个对象列表

List<Objects> args = new ArrayList<Objects>();
args.add(1);
args.add("abcd");

Is there any way in which I can substitute these args into Str , so that i get a string like "Entered number = 1 and string = abcd " ?

有没有什么方法可以把这些args替换成Str,这样就可以得到一个字符串,比如" enter number = 1 and string = abcd " ?

By generalizing this I was planning to dump all the questions and arguments in a file (like json) and execute them at run time. Please do let me know if there are better ways to do this.

通过概括这一点,我计划将所有问题和参数转储到一个文件中(如json),并在运行时执行它们。如果有更好的办法,请告诉我。

3 个解决方案

#1


23  

Try:

试一试:

String formatted = String.format(str, args.toArray());

This gives:

这给:

Entered number = 1 and string = abcd

#2


6  

You can use the following:

你可以使用以下方法:

String str = "Entered number = %d and string = %s";

List<Object> args = new ArrayList<Object>();
args.add(1);
args.add("abcd");

System.out.println(String.format(str, args.toArray()));

will give the output:

将输出:

Entered number = 1 and string = abcd

From JLS 8.4.1 Format parameters:

JLS 8.4.1格式参数:

The last formal parameter in a list is special; 
it may be a variable arity parameter, indicated by an 
elipsis following the type.

If the last formal parameter is a variable arity parameter of type T, 
it is considered to define a formal parameter of type T[]. 
The method is then a variable arity method. Otherwise, it is a fixed arity 
method. Invocations of a variable arity method may contain more actual 
argument expressions than formal parameters. All the actual argument 
expressions that do not correspond to the formal parameters preceding 
the variable arity parameter will be evaluated and the results stored 
into an array that will be passed to the method invocation.

See this question on *!

参见*上的问题!

#3


1  

final String myString = String.format(Str, 1, "abcd");

Use variables as appropriate

使用变量作为适当的

#1


23  

Try:

试一试:

String formatted = String.format(str, args.toArray());

This gives:

这给:

Entered number = 1 and string = abcd

#2


6  

You can use the following:

你可以使用以下方法:

String str = "Entered number = %d and string = %s";

List<Object> args = new ArrayList<Object>();
args.add(1);
args.add("abcd");

System.out.println(String.format(str, args.toArray()));

will give the output:

将输出:

Entered number = 1 and string = abcd

From JLS 8.4.1 Format parameters:

JLS 8.4.1格式参数:

The last formal parameter in a list is special; 
it may be a variable arity parameter, indicated by an 
elipsis following the type.

If the last formal parameter is a variable arity parameter of type T, 
it is considered to define a formal parameter of type T[]. 
The method is then a variable arity method. Otherwise, it is a fixed arity 
method. Invocations of a variable arity method may contain more actual 
argument expressions than formal parameters. All the actual argument 
expressions that do not correspond to the formal parameters preceding 
the variable arity parameter will be evaluated and the results stored 
into an array that will be passed to the method invocation.

See this question on *!

参见*上的问题!

#3


1  

final String myString = String.format(Str, 1, "abcd");

Use variables as appropriate

使用变量作为适当的