1

Probably there is already some kind of apache or guava utility class for my requirement:

I want to always create strings of the same length. The missing characters should be filled either left or right with a fixed character. Something like:

Utils.filledString(teststring, " ", 5); //would ensure the teststring is always 5 chars long, and if not append whitespace to the right
Utils.filledString(teststring, "x", -5); //same as above, but fill the 5 chars left with an x

You get the idea, and probably it's already there, but I'm missing the right keyword to find it.

membersound
  • 74,158
  • 163
  • 522
  • 986

2 Answers2

5

There is already a solution in guava project. Google Guava String. It is called padEnd/padStart

Flown
  • 11,142
  • 3
  • 42
  • 60
2

Have a look at apache commons lang StringUtils:

StringUtils.rightPad(String str, int size, String padStr)
StringUtils.leftPad(String str, int size, String padStr

StringUtils.rightPad("bat", 5, "")    = "bat  "
StringUtils.leftPad("bat", 5, "x")    = "xxbat"
flavio.donze
  • 6,753
  • 9
  • 45
  • 80