标签:JAVA forCutChar 中英文 int 截取 result catIndex charStr String
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,forCutChar,中英文,int,截取,result,catIndex,charStr,String 来源: https://www.cnblogs.com/tkey/p/15268387.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。