我可以使用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