java string padding

Solutions on MaxInterview for java string padding by the best coders in the world

showing results for - "java string padding"
William
02 May 2020
1Have a look at org.apache.commons.lang.StringUtils#rightPad(String str, int size, char padChar).
2
3But the algorithm is very simple (pad right up to size chars):
4
5public String pad(String str, int size, char padChar)
6{
7  StringBuilder padded = new StringBuilder(str);
8  while (padded.length() < size)
9  {
10    padded.append(padChar);
11  }
12  return padded.toString();
13}