新增随机子字符串方法
This commit is contained in:
parent
bf943d5410
commit
855821cb95
@ -8,6 +8,7 @@ import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
|
|||||||
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
|
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
|
||||||
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
|
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
@ -46,7 +47,7 @@ public class WStringUtil {
|
|||||||
* @param valStr
|
* @param valStr
|
||||||
* @return String[]
|
* @return String[]
|
||||||
*/
|
*/
|
||||||
public static String[] StrList(String valStr) {
|
public static String[] strList(String valStr) {
|
||||||
int i = 0;
|
int i = 0;
|
||||||
String TempStr = valStr;
|
String TempStr = valStr;
|
||||||
String[] returnStr = new String[valStr.length() + 1 - TempStr.replace(",", "").length()];
|
String[] returnStr = new String[valStr.length() + 1 - TempStr.replace(",", "").length()];
|
||||||
@ -54,7 +55,6 @@ public class WStringUtil {
|
|||||||
while (valStr.indexOf(',') > 0) {
|
while (valStr.indexOf(',') > 0) {
|
||||||
returnStr[i] = valStr.substring(0, valStr.indexOf(','));
|
returnStr[i] = valStr.substring(0, valStr.indexOf(','));
|
||||||
valStr = valStr.substring(valStr.indexOf(',') + 1, valStr.length());
|
valStr = valStr.substring(valStr.indexOf(',') + 1, valStr.length());
|
||||||
|
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
return returnStr;
|
return returnStr;
|
||||||
@ -282,4 +282,29 @@ public class WStringUtil {
|
|||||||
return strBuf.toString();
|
return strBuf.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从字符串中随机取出新的字符串
|
||||||
|
*
|
||||||
|
* @param input 输入字符串
|
||||||
|
* @param outLength 新字符串长度
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static String randomSubStr(String input, int outLength) {
|
||||||
|
if (input == null || input.isEmpty()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (outLength >= input.length()) {
|
||||||
|
outLength = input.length();
|
||||||
|
}
|
||||||
|
if (outLength <= 0) {
|
||||||
|
outLength = 6;
|
||||||
|
}
|
||||||
|
StringBuilder subStrSB = new StringBuilder();
|
||||||
|
Random random = new Random();
|
||||||
|
for (int i = 0; i < outLength; i++) {
|
||||||
|
subStrSB.append(input.charAt(random.nextInt(input.length())));
|
||||||
|
}
|
||||||
|
return subStrSB.toString();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user