JDK UUID產生32位隨機數
比如說做微信二次開發,要提供32位隨機數
String uuid = UUID.randomUUID().toString().toUpperCase().replaceAll("-", "");
上面這種寫法可以分解成下面這種寫法
import java.util.UUID;
public class GetRandom {
public static void main(String[] args) {
UUID uuid = UUID.randomUUID();
System.out.println(".{" + uuid.toString() + "}");
System.out.println(uuid.toString());
/**
* 36
*/
System.out.println(uuid.toString().length());
/**
* 32
*/
System.out.println(uuid.toString().replace("-", "").length());
/**
* 6c0222ede7f54cada717a9abfb372239
*/
System.out.println(uuid.toString().replace("-", ""));
}
}
輸出結果
.{470be345-6a5a-4935-b09d-f1268689adf4}
470be345-6a5a-4935-b09d-f1268689adf4
36
32
470be3456a5a4935b09df1268689adf4