生成随机字符串(UUID方法)

时间:2024-08-18 15:04:02

这是另一种用UUID生成随机字符串的方法。

 public class RandomGenerator{
private int length; public void setLength(int length) {
this.length = length;
}
public RandomGenerator(int length){
this.length=length;
}
public RandomGenerator(){
this.length=32;
}
public String generate(){
String s=UUID.randomUUID().toString();
s=s.substring(0,8)+s.substring(9,13)+s.substring(14,18)+s.substring(19,23)+s.substring(24);
return s.substring(0,length);
}
}