I\'m a beginner to java and this is my first post on Stackoverflow. Although my initial code is similar to other posts here, my question relates to implementing StringBuilde
This can be significantly simplified with Java 11 and it uses array copoy internally, string concatentation should also be converted to stringbuilder internally
public static String padNum(int number, String pad, int length){
String numString = String.valueOf(number);
int padLength = length - numString.length();
if(padLength > 0)
return pad.repeat(padLength) + numString;
else
return numString;
}
check if rest is more than 0 to see if we need a pad or not, then use String.repeat to generate a pad of the right size
the string length will always be >= padLength