@Test public void test() { this.printToConsole(autoGenericCode("10011")); this.printToConsole(autoGenericCode("000",3)); } /** * 不夠位數的在前面補0,保留code的長度位數字 * @param code * @return */ private String autoGenericCode(String code) { String result = ""; // 保留code的位數 result = String.format("%0" + code.length() + "d", Integer.parseInt(code) + 1); return result; }
/** * 不夠位數的在前面補0,保留num的長度位數字 * @param code * @return */ private String autoGenericCode(String code, int num) { String result = ""; // 保留num的位數 // 0 代表前面補充0 // num 代表長度為4 // d 代表參數為正數型 result = String.format("%0" + num + "d", Integer.parseInt(code) + 1); return result; }
結果:
"10012"
"001"
