A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 梦缠绕的时候 黑马粉丝团   /  2019-2-15 11:26  /  472 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

Java如何判定字符串中的中文
public static String splitString(String str, int len) {
return splitString(str, len, "...");
}
public static String splitString(String str,int len,String elide) {
if (str == null) {
return "";
}
byte[] strByte = str.getBytes();
int strLen = strByte.length;
if (len >= strLen || len < 1) {
return str;
}
int count = 0;
for (int i = 0; i < len; i++) {
int value = (int) strByte;
if (value < 0) {
count++;
}
}
if (count % 2 != 0) {
len = (len == 1) ? len + 1 : len - 1;
}
return new String(strByte, 0, len) + elide.trim();
}
方法一:
public static int countChineseCharacter(String originStr) {
int count = 0;
String temp = null;
for (int i = 0; i < originStr.length(); i++) {
temp = String.valueOf(originStr.charAt(i));
if (temp.getBytes().length == 2)
count++;
}
return count;
}

方法二:
public static int countChinese(String originalStr){
int count = 0;
Pattern pattern = Pattern.compile("[\u4e00-\u9fa5]");
Matcher matcher = pattern.matcher(originalStr);
while(matcher.find())
count++;
return count;
}

1 个回复

倒序浏览
今天也要加油鸭
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马