1

If I have the next string template:

String test_template   = "\"%1$s\" (%2$s) (%3$s)";

Is there any way I can format it with 3rd parameter only?

MessageFormat.format(test_template, *here put only 3rd parameter*);
Hans Z.
  • 50,496
  • 12
  • 102
  • 115
ovod
  • 1,118
  • 4
  • 18
  • 33
  • pass in two empty strings then your desired one? – beresfordt Mar 25 '15 at 23:15
  • I suppose you could split the test_template string by spaces and store it in an array, then you could just put the third element of that array as the format. – not_a_bot Mar 25 '15 at 23:16
  • beresfordt, no. In this case I will lose 1st and 2nd templates. I want to leave %1$s and %2$s and substitute only 3rd – ovod Mar 25 '15 at 23:20
  • How about `MessageFormat.format(test_template, "%1$s", "%2$s", "foo");`? – McDowell Mar 26 '15 at 11:35
  • I don't understand why you use this template in the first place, if you want to replace only third part. I suppose, you mean to replace them one by one. In that case, run three `MessageFormat.format()` commands and combine the results in a fourth call format("{0}{2}{3}", res1, res2, res3). – thst Mar 26 '15 at 11:37
  • McDowell, this is what I was looking for) – ovod Mar 26 '15 at 12:07

1 Answers1

0

You could try:

String test_template2   = "{0} {1} {2}";
System.out.println(MessageFormat.format(test_template2,"first","second", "third"));
System.out.println(MessageFormat.format(test_template2,"","", "third"));

The output will be:

"first second third"

" third"

Hope helped you!

If you want to use named placeholder you can read about how here

Community
  • 1
  • 1
vathek
  • 531
  • 2
  • 9