0

Alright, say you have a string you want to format, for example "this is a string by %s and %s" and this will do the job just fine if i format it for example with "John" and "Abby" but, what if instead, I want to type "Abby" and then "John", I can do it, its just not pretty and will look something like this: "this is a string by %2$s and %1$s" followed by the formatting of "John", "Abby" and it will format it however I want, but the problem with this is its a lot of work (in large files, that are formatted only in runtime with multiple variables) to make sure everything is correct. my question is this, is there a way to have it work in a "prettier" way? for example C#'s formatting is done with "{0}, {1}" which is a million times better than Java's.

I have tried a replacer with regex to replace "{0}" to, for example, "%1$s". if somebody has a solution to this problem it would help me and will be appreciated. thanks in advance

Quapi
  • 111
  • 1
  • 2
  • 5
  • Is there something that doesn't work with `%1$s`, or is the problem merely that it's ugly (I agree)? I'm not 100% sure, but I believe the concept is the same, whether it's spelled `%1$s` or `{0}`. – Julia May 19 '22 at 17:58
  • Curly braces are what the old `java.text.MessageFormat` uses :) – g00se May 19 '22 at 18:04
  • If you have not already seen it, there are different approaches mentioned in the answers to this question: [Named placeholders in string formatting](https://stackoverflow.com/q/2286648/12567365). Maybe something there is less (subjectively) ugly? – andrewJames May 19 '22 at 18:22

0 Answers0