java实现5位验证码

方法1:

String res = UUID.randomUUID().toString();
res = res.substring(0, 5);
System.out.println(res);

方法2:

String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
str=str.toLowerCase();
str += "1234567890";
		
StringBuilder sb = new StringBuilder(5);
for(int i = 0; i < 5; i++){
	//角标[0,62)
	int index = new Random().nextInt(str.length());
	char ch = str.charAt(index);
	sb.append(ch);
}
System.out.println(sb);

猜你喜欢

转载自blog.csdn.net/qzcrystal/article/details/82286425
今日推荐