public class aaa {
/**
* @param args
*/
public static void main(String[] args) {
String string = "041偷功.mp3";
System.out.println(judgeChina(string, 5));
}
/**
*
* @param content 输入的内容
* @param maxSize 最大长度
* @return
*/
public static String judgeChina(String content, Integer maxSize) {
Integer index = 0;
StringBuffer sBuffer = new StringBuffer();
for (int i = 0; i < content.length(); i++) {
String retContent = content.substring(i, i + 1);
// 生成一个Pattern,同时编译一个正则表达式
boolean isChina = retContent.matches("[\u4E00-\u9FA5]");
boolean isCapital = retContent.matches("[A-Z]");
if (index == maxSize) {
sBuffer.append("..");
break;
}
if ((index == maxSize - 1 && isChina)) {
continue;
}
if (isChina || isCapital) {
index = index + 2;
} else {
index = index + 1;
}
sBuffer.append(retContent);
if (index > maxSize) {
sBuffer.append("..");
break;
}
}
return sBuffer.toString();
}
}
JAVA判断中英文,并根据中英文自动截取字符串
最新推荐文章于 2023-05-09 11:24:17 发布