* 用HTML的格式显示多行文字,每一行根据要求截取指定长度的字符(汉、日、韩文字符长度为2),
* 不区分中英文,如果数字不正好,则少取一个字符位
*
* @param aString
* 原始字符串
* @param specialCharsLength
* 截取长度(汉、日、韩文字一个字符长度为2)
* @param row
* 分割显示行数
* @return 分割处理后的字符串List
*/
private static String cutString(String aString, int specialCharsLength,
int row) {
List<String> outputStr = subStr(aString, specialCharsLength, row);
StringBuilder sb = new StringBuilder();
sb.append("<html>");
Iterator<String> iter = outputStr.iterator();
while(iter.hasNext()) {
sb.append(iter.next());
sb.append("<br>");
}
sb.append("</html>");
return sb.toString();
}
/**
* 截取一段字符的长度(汉、日、韩文字符长度为2),不区分中英文,如果数字不正好,则少取一个字符位
*
* @param str
* 原始字符串
* @param specialCharsLength
* 截取长度(汉、日、韩文字符长度为2)
* @param row
* 分割显示行数
* @return 分割处理后的字符串List
*/
public static List<String> subStr(String str, int specialCharsLength,
int row) {
if (str == null || "".equals(str) || specialCharsLength < 1) {
return null;
}
List<String> strList = new ArrayList<String>();
char[] chars = str.toCharArray();
int tempLength = 0;
for (int i = 0; i < row; i++) {
int charsLength = getCharsLength(chars, specialCharsLength,
tempLength);
strList.add(new String(chars, tempLength, charsLength));
tempLength = tempLength + charsLength;
if (tempLength >= str.length()) {
break;
}
}
return strList;
}
/**
* 获取一段字符的长度,输入长度中汉、日、韩文字符长度为2,输出长度中所有字符均长度为1
*
* @param chars
* 一段字符
* @param specialCharsLength
* 输入长度,汉、日、韩文字符长度为2
* @return 输出长度,所有字符均长度为1
*/
public static int getCharsLength(char[] chars, int specialCharsLength,
int tempLength) {
int count = 0;
int normalCharsLength = 0;
for (int i = tempLength; i < chars.length; i++) {
int specialCharLength = getSpecialCharLength(chars[i]);
if (count <= specialCharsLength - specialCharLength) {
count += specialCharLength;
normalCharsLength++;
} else {
break;
}
}
return normalCharsLength;
}
/**
* 获取字符长度:汉、日、韩文字符长度为2,ASCII码等字符长度为1
*
* @param c
* 字符
* @return 字符长度
*/
private static int getSpecialCharLength(char c) {
if (isLetter(c)) {
return 1;
} else {
return 2;
}
}
/**
* 判断一个字符是Ascill字符还是其它字符(如汉,日,韩文字符)
*
* @param char c, 需要判断的字符
* @return boolean, 返回true,Ascill字符
*/
private static boolean isLetter(char c) {
int k = 0x80;
return c / k == 0 ? true : false;
}