目录 1.原生Java代码实现:2.Hutool工具实现: 官方文档: https://www.hutool.cn/docs/#/core/工具类/随机工具-RandomUtil 1.原生Java代码实现: priva
private static final String SYMBOLS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; // 数字和26个字母组成private static final Random RANDOM = new SecureRandom(); // SecureRandom是加密且线程安全的public static String getRandomNumber() { char[] nonceChars = new char[16]; //指定长度为6位/自己可以要求设置 for (int index = 0; index < nonceChars.length; ++index) { nonceChars[index] = SYMBOLS.charAt(RANDOM.nextInt(SYMBOLS.length())); } return new String(nonceChars);}
public static void main(String[] args) { // 小写字母+数字,例:d2qdw920d5 String randomString1 = RandomUtil.randomString(10); // 大写字母+数字,例:I6R7Q38JWC String randomStringUpper = RandomUtil.randomStringUpper(10); // 只含有abc,例:bcccbcccac String randomString2 = RandomUtil.randomString("abc", 10); // 大写字母+小写字母+数字,例:qf0Vr2TK3J String randomString3 = RandomUtil.randomString("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", 10); // 数字,例:8 char randomNumber1 = RandomUtil.randomNumber(); // 小写字母+大写字母中,不含有0123456789,例:nmjjyscmvq String randomStringWithoutStr = RandomUtil.randomStringWithoutStr(10, "0123456789"); // 生成小写字母+数字的其中一个字符,例:m char randomChar1 = RandomUtil.randomChar(); // 生成0123456789中一个字符,例:7 char randomNumber2 = RandomUtil.randomNumber(); // 生成-!?中一个字符,例:? char randomChar2 = RandomUtil.randomChar("-!?");}
整理完毕,完结撒花~
来源地址:https://blog.csdn.net/qq_33204709/article/details/129214117
--结束END--
本文标题: Hutool 生成随机数和随机字符串
本文链接: https://www.lsjlt.com/news/371632.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-04-03
2024-04-03
2024-04-01
2024-01-21
2024-01-21
2024-01-21
2024-01-21
2023-12-23
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0