java字符串自动换行_【转】String自动换行

String messageTextC = autoCut(messageTextC,50);

///

String newString = "";

char[] arr = str.toCharArray();

int txtLen = 0;// 文本长度

int charCount = 0;// 字符计数器

int startIndex = 0;// 开始索引

int endIndex = 0;// 结束索引

ArrayList result = new ArrayList();// 字符串数组

int j = 0;

for (int i = 0; i < arr.length; i++) {

if (isChinese(arr[i])) {

txtLen += 2;

charCount++;

} else {

txtLen++;

charCount++;

}

if (txtLen == charLen) {

startIndex = endIndex;

endIndex = startIndex + charCount;

charCount = 0;

txtLen = 0;

result.add(j++, str.substring(startIndex, endIndex));

} else if (txtLen == charLen + 1) {

startIndex = endIndex;

endIndex = startIndex + charCount - 1;

charCount = 0;

txtLen = 0;

result.add(j++, str.substring(startIndex, endIndex));

}

}

if (endIndex < str.length()) {

startIndex = endIndex;

result.add(j, str.substring(startIndex, str.length()));

}

result.trimToSize();

Iterator itr = result.iterator();

while (itr.hasNext()) {

newString = newString + itr.next() + "\n";

}

return newString;

private boolean isChinese(char c) {

Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);

if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS

|| ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS

|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A

|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION

|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION

|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS)

return true;

return false;

} }

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2011-07-15 16:47

浏览 2846

评论

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值