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