Hello. I have such a question, I know how to add a line with zeros

System.out.println(String.format("%010d",123)); 

But how to replace 0 with another character?

  • String.format("%10d",123).replace(' ', '*') - will not work? - PinkTux
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

1 answer 1

In this case, for example, like this:

 System.out.println(String.format("%10d", 123).replace(' ', 's')); 

How to implement this using standard String.format(...) in general , I will not say for sure. But this can be done with at least:

  1. Apache commons library : StringUtils.leftPad("test", 10, 's');
  2. Google Guava : Strings.padStart("test", 10, 's'); .

It is also possible to implement the same method with a similar functional, for elementary.