0

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
    https://stackoverflow.com/questions/10168066/how-to-print-out-all-the-elements-of-a-list-in-java – JGFMK Sep 09 '20 at 09:03
  • Does this answer your question? [How to print out all the elements of a List in Java?](https://stackoverflow.com/questions/10168066/how-to-print-out-all-the-elements-of-a-list-in-java) – JGFMK Sep 09 '20 at 09:04
  • @JGFMK that is really bad example, has nothing to do with passing list to a given `format`. – Amongalen Sep 09 '20 at 09:04
  • @Amongalen The method you have described is not flexible. Whereas the link shows a list of any size being printed with several solutions that are superior – JGFMK Sep 09 '20 at 09:07
  • It depends how you interpret OPs question. I understand it that he already has some format string with 20 "%s" in it and want to pass a list of parameters to `String.format` without a need to use `list.get(x)` 20 times. – Amongalen Sep 09 '20 at 09:14

3 Answers3

3

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);
}
Amongalen
  • 3,101
  • 14
  • 20
  • That works great. I did realize that argument is an object but had no knoweledge how to pass list of strings exactly. – Stanislav Semyonov Sep 09 '20 at 09:26
  • I will recommend to cast the array to an Object array, is just a convention in order to indicate, that you are working with vargars: `code`String result = String.format ( ( Object[] ) toFormat, list.toArray(new String[0])); – A.Casanova Apr 11 '22 at 10:25
1

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.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
0

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

vincenzopalazzo
  • 1,487
  • 2
  • 7
  • 35
  • Yeah, but he doesnt want it to say "first ... first". He wants "first ... second ... third" ... and so on. Which simply doesnt make sense. The question is unclear ... – GhostCat Sep 09 '20 at 09:14
  • I believe the `toFormat` String given by OP is just an example, in reality it's just some custom String with 20 or so `%s` – Amongalen Sep 09 '20 at 09:16
  • I agree with you, I think that the format fo the question is not clear, I published this code because is possible to customize the for each and insert, for instance, the string for "first" and "second". In addition, I think that the question is a X, Y problem http://xyproblem.info/ – vincenzopalazzo Sep 09 '20 at 09:23
  • In case of more details, I can update my question with the solution, or remove it is the answer doesn't match with the question. For the moment, I think is a good patter to find a solution – vincenzopalazzo Sep 09 '20 at 09:25
  • Yeah, i made the string *toFormat* as an example. In reality i need to format a huge sql request with multiple %s placeholders for the arguments – Stanislav Semyonov Sep 09 '20 at 09:31
  • In this case, I think that you don't need the "first" or "second" but you can customize your "patter" for each line! In addition, if you use the forEach you don't need to worry of ArrayIndexOutOfBounds exception – vincenzopalazzo Sep 09 '20 at 09:35