java 多行显示的文字_Java,指定文字列,可以按照指定的文字数多行显示

/**

* 用HTML的格式显示多行文字,每一行根据要求截取指定长度的字符(汉、日、韩文字符长度为2),

* 不区分中英文,如果数字不正好,则少取一个字符位

*

* @param aString

*            原始字符串

* @param specialCharsLength

*            截取长度(汉、日、韩文字一个字符长度为2)

* @param row

*            分割显示行数

* @return 分割处理后的字符串List

*/

private static String cutString(String aString, int specialCharsLength,

int row) {

List outputStr = subStr(aString, specialCharsLength, row);

StringBuilder sb = new StringBuilder();

sb.append("");

Iterator iter = outputStr.iterator();

while(iter.hasNext()) {

sb.append(iter.next());

sb.append("
");

}

sb.append("");

return sb.toString();

}

/**

* 截取一段字符的长度(汉、日、韩文字符长度为2),不区分中英文,如果数字不正好,则少取一个字符位

*

* @param str

*            原始字符串

* @param specialCharsLength

*            截取长度(汉、日、韩文字符长度为2)

* @param row

*            分割显示行数

* @return 分割处理后的字符串List

*/

public static List subStr(String str, int specialCharsLength,

int row) {

if (str == null || "".equals(str) || specialCharsLength < 1) {

return null;

}

List strList = new ArrayList();

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;  }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值