开始String类为我们提供了类似的功能,就是String.format
public class demo01 {
public static void main(String[] args) {
Random random = new Random();
Integer i = random.nextInt(1000000);
String newLabel = String.format("%s%06d", "", i);
System.out.println(newLabel);
}
}
------------------------------------
042309
Process finished with exit code 0
public static void main(String[] args) {
long f = System.currentTimeMillis();
for (int j = 0; j < 100000; j++) {
Random random = new Random();
Integer i = random.nextInt(1000000);
String newLabel = String.format("%s%06d", "", i);
}
long e = System.currentTimeMillis();
System.out.println(e-f);
}
-------------------------
700+
后来字符串截取的方式实现
public class demo01 {
public static void main(String[] args) {
Random random = new Random();
Double aDouble = random.nextDouble();
String num = Double.toString(aDouble);
String substring = num.substring(2, 8);
System.out.println(num);
System.out.println(substring);
}
}
------------------------------------------------
0.07198521431173577
071985
public class demo01 {
public static void main(String[] args) {
long f = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
Random random = new Random();
Double aDouble = random.nextDouble();
String num = Double.toString(aDouble);
String substring = num.substring(2, 8);
}
long e = System.currentTimeMillis();
System.out.println(e-f);
}
}
---------------------------
62到125之间