When dealing with employee numbers etc., there was a slight hole when I was investigating how to match the final number of digits with any digit, so make a note of it.
String.format("%xyz" , n); % = Regular expression x = the number you want to fill y = number of digits to fill (including n) z = Decimal or other decimal acronym (this time d)
String.replace (convert what to what);
public class strPractice02 {
public static void main(String[] args) {
//Add 4 digits of zero before 9
String str = String.format("%05d", 9);
System.out.println(str);
//Replace with a non-zero character
String str2 = String.format("%15d", 9);
System.out.println(str2);
//An alternative if it somehow goes blank
String str3 = String.format("%5d", 9).replace(" ", "1");
System.out.println(str3);
}
}
Execution result
00009
9
11119
When I was investigating, it was written that even numbers other than 0 could be filled in by substituting them into the x part, but str2, which I tried to fill with 1, had a 14-digit blank. So I replaced the whitespace.
Recommended Posts