package com.xxx.utils;
import com.google.common.collect.Lists;
import java.util.List;
/**
* 字符工具类
*/
public final class CharUtil {
public CharUtil() {
}
/**
* 实际字符串分隔
* @param charStr 需要分隔的字符串
* @param catIndex 分隔长度
* @return List
*/
private static String[] cut(String charStr,int catIndex){
String sb = "";
int charLength = 0;
for (int i = 0;i<charStr.length() ;i++ ){
int asciiCode = charStr.codePointAt(i);
if (asciiCode >=0 && asciiCode<=255){
charLength+=1;
} else{
charLength+=2;
}
if (charLength<=catIndex) {
sb += charStr.charAt(i);
}else{
break;
}
}
return new String[]{charStr.substring(sb.length()),sb};
}
/**
* 把字符串按照字符的长度进行分隔
* @param charStr 需要分隔的字符串
* @param catIndex 分隔长度
* @return List
*/
public static List<String> cutString(String charStr, int catIndex){
List<String> result = Lists.newArrayList();
boolean endCond = true;
while (endCond){
String[] forCutChar = cut(charStr, catIndex);
result.add(forCutChar[1]);
if(forCutChar[0].length() > catIndex){
charStr = forCutChar[0];
}else{
endCond = false;
if(StringUtil.isNotBlank(forCutChar[0])){
result.add(forCutChar[0]);
}
}
}
return result;
}
}
JAVA实现中英文混合文字友好截取功能
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
js中英文字符串长度 js中英文字符串截取(转)
摘要:我们在用程序处理文本的过程中,经常碰到中英文字符串的问题,比如说我们要得到
prototype 字符串 Math 中文字符