package app.share.other;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Random;
/**
* 额外的字符串处理
* @author Administrator
*
*/
public class StringExtender {
/**
* 将指定的字符串按照最大长度限制,切割为字符串数组(不管换行符)
* @param str
* @param maxLength
* @return
*/
public static String[] CutToArray(String str,int maxLength)
{
if(str == null || str.length() <= 0 || maxLength < 1)
return null ;
String[]strs = null ;
int length = str.length() / maxLength ;
int retain = str.length() % maxLength ;
if(retain != 0)//余数不为0
{
length ++ ;
}
if(retain == 0)//余数为0
retain = maxLength ;
strs = new String[length] ;
int i = 0;
for( ; i < length - 1 ; i ++)//循环到倒数第一次
strs[i] = str.substring(i * maxLength, i * maxLength + maxLength);
strs[i] = str.substring(i * maxLength, i * maxLength + retain);
return strs ;
}
/**
* 将指定的字符串按照最大长度限制,切割为字符串数组(受换行符影响)
* @param str
* @param maxLength
* @return
*/
public static ArrayList<String> CutToArrayEx(String str,int maxLength)
{
if(str == null || str.length() <= 0 || maxLength < 1)
return null ;
ArrayList<String> strs = null ;
int length = str.length() / maxLength ;
int retain = str.length() % maxLength ;
if(retain != 0)//余数不为0
{
length ++ ;
}
if(retain == 0)//余数为0
retain = maxLength ;
strs = new ArrayList<String>() ;
int i = 0;
for( ; i < length - 1 ; i ++)//循环到倒数第一次
{
String[] st = str.substring(i * maxLength, i * maxLength + maxLength).split("\n");
for(int j = 0 ; j < st.length ; j ++)
strs.add(st[j]);
}
String[] st = str.substring(i * maxLength, i * maxLength + retain).split("\n");
for(int j = 0 ; j < st.length ; j ++)
strs.add(st[j]);
return strs;
}
/**
* 获取随机汉子字符
* @return
*/
public static String GetRandomCNChar()
{
Random random = new Random();///随机数
String[] rBase = { "0", "1", "2", "3", "4", "5", "6", "7", "8","9", "a", "b", "c", "d", "e", "f" };
// 生成第1位的区码
int r1 = random.nextInt(3) + 11; //生成11到14之间的随机数
String str_r1 = rBase[r1];
// 生成第2位的区码
int r2;
if (r1 == 13)
{
r2 = random.nextInt(7); //生成0到7之间的随机数
}
else
{
r2 = random.nextInt(16); //生成0到16之间的随机数
}
String str_r2 = rBase[r2];
// 生成第1位的位码
int r3 = random.nextInt(6) + 10; //生成10到16之间的随机数
String str_r3 = rBase[r3];
// 生成第2位的位码
int r4;
if (r3 == 10)
{
r4 = random.nextInt(15) + 1; //生成1到16之间的随机数
}
else if (r3 == 15)
{
r4 = random.nextInt(15); //生成0到15之间的随机数
}
else
{
r4 = random.nextInt(16); //生成0到16之间的随机数
}
String str_r4 = rBase[r4];
// 将生成机内码转换为汉字
byte[] bytes = new byte[2];
//将生成的区码保存到字节数组的第1个元素中
String str_r12 = str_r1 + str_r2;
int tempLow = Integer.parseInt(str_r12, 16);
bytes[0] = (byte) tempLow;
//将生成的位码保存到字节数组的第2个元素中
String str_r34 = str_r3 + str_r4;
int tempHigh = Integer.parseInt(str_r34, 16);
bytes[1] = (byte) tempHigh;
String ctmp;
try {
ctmp = new String(bytes,"gb2312");
} catch (UnsupportedEncodingException e) {
return "?";
} //根据字节数组生成汉字
return ctmp ;
}
/**
* 获取随机汉字组合
* @param minLength
* @param maxLength
* @return
*/
public static String GetRandomCHString(int minLength,int maxLength)
{
if(minLength < 1)
minLength = 1 ;
Random random = new Random() ;
String str = new String();
int length = random.nextInt(maxLength - minLength + 1) + minLength ;
System.out.println("GetRandomCHString:length="+length);
for(int i = 0 ; i < length ; i ++)
str += GetRandomCNChar();//65535
return str;
}
}
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Random;
/**
* 额外的字符串处理
* @author Administrator
*
*/
public class StringExtender {
/**
* 将指定的字符串按照最大长度限制,切割为字符串数组(不管换行符)
* @param str
* @param maxLength
* @return
*/
public static String[] CutToArray(String str,int maxLength)
{
if(str == null || str.length() <= 0 || maxLength < 1)
return null ;
String[]strs = null ;
int length = str.length() / maxLength ;
int retain = str.length() % maxLength ;
if(retain != 0)//余数不为0
{
length ++ ;
}
if(retain == 0)//余数为0
retain = maxLength ;
strs = new String[length] ;
int i = 0;
for( ; i < length - 1 ; i ++)//循环到倒数第一次
strs[i] = str.substring(i * maxLength, i * maxLength + maxLength);
strs[i] = str.substring(i * maxLength, i * maxLength + retain);
return strs ;
}
/**
* 将指定的字符串按照最大长度限制,切割为字符串数组(受换行符影响)
* @param str
* @param maxLength
* @return
*/
public static ArrayList<String> CutToArrayEx(String str,int maxLength)
{
if(str == null || str.length() <= 0 || maxLength < 1)
return null ;
ArrayList<String> strs = null ;
int length = str.length() / maxLength ;
int retain = str.length() % maxLength ;
if(retain != 0)//余数不为0
{
length ++ ;
}
if(retain == 0)//余数为0
retain = maxLength ;
strs = new ArrayList<String>() ;
int i = 0;
for( ; i < length - 1 ; i ++)//循环到倒数第一次
{
String[] st = str.substring(i * maxLength, i * maxLength + maxLength).split("\n");
for(int j = 0 ; j < st.length ; j ++)
strs.add(st[j]);
}
String[] st = str.substring(i * maxLength, i * maxLength + retain).split("\n");
for(int j = 0 ; j < st.length ; j ++)
strs.add(st[j]);
return strs;
}
/**
* 获取随机汉子字符
* @return
*/
public static String GetRandomCNChar()
{
Random random = new Random();///随机数
String[] rBase = { "0", "1", "2", "3", "4", "5", "6", "7", "8","9", "a", "b", "c", "d", "e", "f" };
// 生成第1位的区码
int r1 = random.nextInt(3) + 11; //生成11到14之间的随机数
String str_r1 = rBase[r1];
// 生成第2位的区码
int r2;
if (r1 == 13)
{
r2 = random.nextInt(7); //生成0到7之间的随机数
}
else
{
r2 = random.nextInt(16); //生成0到16之间的随机数
}
String str_r2 = rBase[r2];
// 生成第1位的位码
int r3 = random.nextInt(6) + 10; //生成10到16之间的随机数
String str_r3 = rBase[r3];
// 生成第2位的位码
int r4;
if (r3 == 10)
{
r4 = random.nextInt(15) + 1; //生成1到16之间的随机数
}
else if (r3 == 15)
{
r4 = random.nextInt(15); //生成0到15之间的随机数
}
else
{
r4 = random.nextInt(16); //生成0到16之间的随机数
}
String str_r4 = rBase[r4];
// 将生成机内码转换为汉字
byte[] bytes = new byte[2];
//将生成的区码保存到字节数组的第1个元素中
String str_r12 = str_r1 + str_r2;
int tempLow = Integer.parseInt(str_r12, 16);
bytes[0] = (byte) tempLow;
//将生成的位码保存到字节数组的第2个元素中
String str_r34 = str_r3 + str_r4;
int tempHigh = Integer.parseInt(str_r34, 16);
bytes[1] = (byte) tempHigh;
String ctmp;
try {
ctmp = new String(bytes,"gb2312");
} catch (UnsupportedEncodingException e) {
return "?";
} //根据字节数组生成汉字
return ctmp ;
}
/**
* 获取随机汉字组合
* @param minLength
* @param maxLength
* @return
*/
public static String GetRandomCHString(int minLength,int maxLength)
{
if(minLength < 1)
minLength = 1 ;
Random random = new Random() ;
String str = new String();
int length = random.nextInt(maxLength - minLength + 1) + minLength ;
System.out.println("GetRandomCHString:length="+length);
for(int i = 0 ; i < length ; i ++)
str += GetRandomCNChar();//65535
return str;
}
}