add RandomUtil.randomStringWithoutStr

This commit is contained in:
Looly 2019-10-09 10:59:34 +08:00
parent 17efa74406
commit 8e5bbdf170
2 changed files with 80 additions and 73 deletions

View File

@ -13,6 +13,7 @@
* 【core】 增加DateUtil.parseCST方法issue#570@Github * 【core】 增加DateUtil.parseCST方法issue#570@Github
* 【core】 增加defaultIfEmpty方法 * 【core】 增加defaultIfEmpty方法
* 【crypto】 修改bigIntToFixexLengthBytes为bigIntToFixedLengthBytespr#575@Github * 【crypto】 修改bigIntToFixexLengthBytes为bigIntToFixedLengthBytespr#575@Github
* 【core】 RandomUtil增加randomStringWithoutStrpr#76@Gitee
### Bug修复 ### Bug修复
* 【all】 修复阶乘计算错误bugissue#I12XE4@Gitee * 【all】 修复阶乘计算错误bugissue#I12XE4@Gitee

View File

@ -27,15 +27,20 @@ import cn.hutool.core.lang.WeightRandom.WeightObj;
* 随机工具类 * 随机工具类
* *
* @author xiaoleilu * @author xiaoleilu
*
*/ */
public class RandomUtil { public class RandomUtil {
/** 用于随机选的数字 */ /**
* 用于随机选的数字
*/
public static final String BASE_NUMBER = "0123456789"; public static final String BASE_NUMBER = "0123456789";
/** 用于随机选的字符 */ /**
* 用于随机选的字符
*/
public static final String BASE_CHAR = "abcdefghijklmnopqrstuvwxyz"; public static final String BASE_CHAR = "abcdefghijklmnopqrstuvwxyz";
/** 用于随机选的字符和数字 */ /**
* 用于随机选的字符和数字
*/
public static final String BASE_CHAR_NUMBER = BASE_CHAR + BASE_NUMBER; public static final String BASE_CHAR_NUMBER = BASE_CHAR + BASE_NUMBER;
/** /**
@ -83,9 +88,9 @@ public class RandomUtil {
* *
* @param isSecure 是否为强随机数生成器 (RNG) * @param isSecure 是否为强随机数生成器 (RNG)
* @return {@link Random} * @return {@link Random}
* @since 4.1.15
* @see #getSecureRandom() * @see #getSecureRandom()
* @see #getRandom() * @see #getRandom()
* @since 4.1.15
*/ */
public static Random getRandom(boolean isSecure) { public static Random getRandom(boolean isSecure) {
return isSecure ? getSecureRandom() : getRandom(); return isSecure ? getSecureRandom() : getRandom();
@ -401,9 +406,6 @@ public class RandomUtil {
public static String randomStringWithoutStr(int length, String elemData) { public static String randomStringWithoutStr(int length, String elemData) {
String baseStr = BASE_CHAR_NUMBER; String baseStr = BASE_CHAR_NUMBER;
baseStr = StrUtil.removeAll(baseStr, elemData.toCharArray()); baseStr = StrUtil.removeAll(baseStr, elemData.toCharArray());
if(baseStr.equals("")){
return "";
}
return randomString(baseStr, length); return randomString(baseStr, length);
} }
@ -425,6 +427,9 @@ public class RandomUtil {
* @return 随机字符串 * @return 随机字符串
*/ */
public static String randomString(String baseString, int length) { public static String randomString(String baseString, int length) {
if (StrUtil.isEmpty(baseString)) {
return StrUtil.EMPTY;
}
final StringBuilder sb = new StringBuilder(length); final StringBuilder sb = new StringBuilder(length);
if (length < 1) { if (length < 1) {
@ -505,6 +510,7 @@ public class RandomUtil {
} }
// ------------------------------------------------------------------- UUID // ------------------------------------------------------------------- UUID
/** /**
* @return 随机UUID * @return 随机UUID
* @deprecated 请使用{@link IdUtil#randomUUID()} * @deprecated 请使用{@link IdUtil#randomUUID()}