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
  • 81,582
  • 193
  • 585
  • 1,120

2 Answers2

5

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

Flown
  • 11,480
  • 3
  • 45
  • 62
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
  • 7,432
  • 9
  • 58
  • 91