我可以使用Apache的通用文本RandomStringGenerator
生成随机字母数字字符串(数字和小写/大写字母)吗?RandomStringGenerator生成字母数字字符串
5
A
回答
22
是,使用下面的代码:
import static org.apache.commons.text.CharacterPredicates.DIGITS;
import static org.apache.commons.text.CharacterPredicates.LETTERS;
// ...
RandomStringGenerator generator = new RandomStringGenerator.Builder()
.withinRange('0', 'z')
.filteredBy(LETTERS, DIGITS)
.build();
4
我用这个:
static char[] chars = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'q', 'w', 'e', 'r', 't', 'z', 'u', 'i', 'o', 'p', 'a', 's',
'd', 'f', 'g', 'h', 'j', 'k', 'l', 'y', 'x', 'c', 'v', 'b', 'n', 'm', 'Q', 'W', 'E', 'R', 'T', 'Z', 'U', 'I', 'O', 'P', 'A',
'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'Y', 'X', 'C', 'V', 'B', 'N', 'M' };
private static String randomString(int length) {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < length; i++) {
stringBuilder.append(chars[new Random().nextInt(chars.length)]);
}
return stringBuilder.toString();
}
或
private static String randomString(int length) {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < length; i++) {
switch (new Random().nextInt(3)) {
case 0:
stringBuilder.append((char) (new Random().nextInt(9) + 48));
break;
case 1:
stringBuilder.append((char) (new Random().nextInt(25) + 65));
break;
case 2:
stringBuilder.append((char) (new Random().nextInt(25) + 97));
break;
default:
break;
}
}
return stringBuilder.toString();
}
enter code here
+0
这还包括以下字符::; <=>?@ [\]^_' –
+0
@DavidRiccitelli是的,但现在正确。 – mate0406
0
与Java 8 Lambda表达式:
private final RandomStringGenerator generator = new RandomStringGenerator.Builder()
.usingRandom(max -> RandomUtils.getRandomAlphanumericChar()).build();
....................
public class RandomUtils {
private final static char[] ALPHA_NUMERIC_CHARS = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4',
'5', '6', '7', '8', '9'};
private final static Random random = new SecureRandom();
public static char getRandomAlphanumericChar() {
return ALPHA_NUMERIC_CHARS[random.nextInt(ALPHA_NUMERIC_CHARS.length)];
}
}
相关问题
- 1. 生成字母数字串顺序
- 2. C#生成下一个字母数字字符串序列
- 3. 如何生成一个字符串使用字母和数字?
- 4. 生成独特的随机字母数字字符串
- 5. 生成字母数字标识(唯一字符串)
- 6. 字母数字字符串
- 7. 在UNIX shell中生成字母数字8个字符的字符串
- 8. 将字符串分割成数字或字母字符与JavaScript
- 9. 在Arduino中生成由数字和字母组成的字符串哈希
- 10. 字符串和字符串生成器
- 11. 生成字符串
- 12. 混合字符串值的字母数字排序:字母数字字母
- 13. 分割字符串转换成字母
- 14. 递增的字母数字字符串
- 15. jQuery - 乘以字母数字字符串
- 16. 如何生成20个字符的字母数字唯一ID
- 17. 用小写字母和数字生成唯一的随机字符串
- 18. 生成长度为6的唯一非顺序字母数字字符串
- 19. SQL - 生成具有特定格式的字母数字字符串
- 20. 如何将字符串拆分为字母字符串和数字字符串?
- 21. 生成总是包含某个字母的字符串python
- 22. 生成随机字符串,缺少一个字母
- 23. 递归性。字母数字字符串中的字母
- 24. 正则表达式字母数字字符串+特殊字母
- 25. 生成字母数字员工ID
- 26. 自动生成字母数字ID
- 27. 计数的字母总数字符串
- 28. 将字符串生成器拆分为字符串字符串特定字符
- 29. 字母字符检查字符串
- 30. 字符串放下字母?
'LETTERS'和'DIGITS'从哪里来?它们是常量吗? –
'import static org.apache.commons.text.CharacterPredicates.DIGITS; import static org.apache.commons.text.CharacterPredicates.LETTERS;'。我更新了答案。 –
为什么不切断中间人,并使用'.filteredBy(Character :: isLetter,Character :: isDigit)'? – toKrause