JAVA判断中英文,并根据中英文自动截取字符串
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(); } }
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。