从含有中文的字符串中提取中文

guodong66 2009-07-16 09:33:31
有如下字符串 String str = "123abc这个中文cde123abc也要提取123ab";

今天看到的一个题,从这个字符串中提取中文部分。 如何解决? 正则表达式么?
...全文
420 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
guodong66 2009-07-16
  • 打赏
  • 举报
回复
结贴送分。
shibenjie 2009-07-16
  • 打赏
  • 举报
回复
运行结果:
这个中文
也要提取
shibenjie 2009-07-16
  • 打赏
  • 举报
回复
public static void main(String[] args) {

String str = "123abc这个中文cde123abc也要提取123ab";
Pattern p = null;
Matcher m = null;
String value = null;

p = Pattern.compile("([\u4e00-\u9fa5]+)");
m = p.matcher(str);


while (m.find()) {
value = m.group(0);
System.out.println(value);
}

}
sd5816690 2009-07-16
  • 打赏
  • 举报
回复

String str = "123abc这个中文cde123abc也要提取123ab";
System.out.println(str.replaceAll("[^\u4e00-\u9fa5]", ""));
tenderuser 2009-07-16
  • 打赏
  • 举报
回复
可以先将你的字符串转换为字符数组,然后判断每一个字符的askII码 ,中文的在unicode中有一个特定的范围。。。 这样可以判断 ,至于用正则 不太会 。。。。
lioushuei 2009-07-16
  • 打赏
  • 举报
回复
public static String getChineseCharacter(String str) throws Exception{
StringBuffer outStr = new StringBuffer();
byte[] bytes = str.getBytes("Unicode");
byte[] tmp = new byte[4];
int i = 0;
int len = bytes.length;
tmp[0] = -1;
tmp[1] = -2;
for ( i = 2 ; i <= ( len - 2 ) ; i += 2){
if ( bytes[i+1] != 0 ){
tmp[2] = bytes[i];
tmp[3] = bytes[i+1];
outStr.append(new String(tmp,"Unicode"));
}
}
return outStr.toString();
}

按照编码区分

67,549

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧