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 "
?
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.
Try:
String formatted = String.format(str, args.toArray());
This gives:
Entered number = 1 and string = abcd
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With