Is there some easy way to pad Strings in Java?
Seems like something that should be in some StringUtil-like API, but I can't find anything that does this.
Padding to 10 characters:String.format("%10s","foo").replace(' ','*');String.format("%-10s","bar").replace(' ','*');String.format("%10s","longer than 10 chars").replace(' ','*');output:*******foo bar*******longer*than*10*charsDisplay '*' for characters of password:Stringpassword="secret123";Stringpadded=String.format("%"+password.length()+"s","").replace(' ','*');output has the same length as the password string:secret123*********