原文:Java随機取字元串的工具類
源代碼下載下傳位址:http://www.zuidaima.com/share/1550463479532544.htm
Java 随機取字元串的工具類 可以全部是數字,字元,也可以字元和數字組合的工具類,希望能給大家帶來幫助
package com.zuidaima.test;
import java.util.Random;
public class RandomUtils {
public static final String ALLCHAR = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static final String LETTERCHAR = "abcdefghijkllmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static final String NUMBERCHAR = "0123456789";
/**
* 傳回一個定長的随機字元串(隻包含大小寫字母、數字)
* @param length 随機字元串長度
* @author www.zuidaima.com
* @return 随機字元串
*/
public static String generateString(int length)
{
StringBuffer sb = new StringBuffer();
Random random = new Random();
for (int i = 0; i < length; i++) {
sb.append(ALLCHAR.charAt(random.nextInt(ALLCHAR.length())));
}
return sb.toString();
}
/**
* 傳回一個定長的随機純字母字元串(隻包含大小寫字母)
* @param length 随機字元串長度
* @author www.zuidaima.com
* @return 随機字元串
*/
public static String generateMixString(int length)
{
StringBuffer sb = new StringBuffer();
Random random = new Random();
for (int i = 0; i < length; i++)
{
sb.append(ALLCHAR.charAt(random.nextInt(LETTERCHAR.length())));
}
return sb.toString();
}
/**
* 傳回一個定長的随機純大寫字母字元串(隻包含大小寫字母)
*
* @param length 随機字元串長度
* @return 随機字元串
*/
public static String generateLowerString(int length) {
return generateMixString(length).toLowerCase();
}
/**
* 傳回一個定長的随機純小寫字母字元串(隻包含大小寫字母)
*
* @param length 随機字元串長度
* @author www.zuidaima.com
* @return 随機字元串
*/
public static String generateUpperString(int length) {
return generateMixString(length).toUpperCase();
}
/**
* 生成一個定長的純0字元串
*
* @param length 字元串長度
* @return 純0字元串
*/
public static String generateZeroString(int length) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < length; i++) {
sb.append('0');
}
return sb.toString();
}
/**
* 根據數字生成一個定長的字元串,長度不夠前面補0
*
* @param num 數字
* @param fixdlenth 字元串長度
* @author www.zuidaima.com
* @return 定長的字元串
*/
public static String toFixdLengthString(long num, int fixdlenth) {
StringBuffer sb = new StringBuffer();
String strNum = String.valueOf(num);
if (fixdlenth - strNum.length() >= 0) {
sb.append(generateZeroString(fixdlenth - strNum.length()));
} else {
throw new RuntimeException("将數字" + num + "轉化為長度為" + fixdlenth + "的字元串發生異常!");
}
sb.append(strNum);
return sb.toString();
}
/**
* 根據數字生成一個定長的字元串,長度不夠前面補0
*
* @param num 數字
* @param fixdlenth 字元串長度
* @author www.zuidaima.com
* @return 定長的字元串
*/
public static String toFixdLengthString(int num, int fixdlenth) {
StringBuffer sb = new StringBuffer();
String strNum = String.valueOf(num);
if (fixdlenth - strNum.length() >= 0) {
sb.append(generateZeroString(fixdlenth - strNum.length()));
} else {
throw new RuntimeException("将數字" + num + "轉化為長度為" + fixdlenth + "的字元串發生異常!");
}
sb.append(strNum);
return sb.toString();
}
/**
* 每次生成的len位數都不相同
* @param param
* @return 定長的數字
*/
public static int getNotSimple(int[] param,int len)
{
Random rand = new Random();
for (int i = param.length; i > 1; i--)
{
int index = rand.nextInt(i);
int tmp = param[index];
param[index] = param[i - 1];
param[i - 1] = tmp;
}
int result = 0;
for(int i = 0; i < len; i++)
{
result = result * 10 + param[i];
}
return result;
}
public static void main(String[] args) {
System.out.println(generateString(10));
System.out.println(generateMixString(10));
System.out.println(generateLowerString(10));
System.out.println(generateUpperString(10));
System.out.println(generateZeroString(10));
System.out.println(toFixdLengthString(123, 10));
System.out.println(toFixdLengthString(123L, 10));
int[] in = {1,2,3,4,5,6,7};
System.out.println(getNotSimple(in,3));
}
}