黑马程序员技术交流社区
标题:
如何将在一段混乱的字符串中提取中文?
[打印本页]
作者:
冰霜之卅
时间:
2015-9-22 23:33
标题:
如何将在一段混乱的字符串中提取中文?
比如说“sdsd王434g二3425小gdgs”
得到 王二小
作者:
meihua
时间:
2015-9-23 07:19
遍历,判断?
空格,数字 ,字母都有对应的ASCII码,不在这些范围内就提取出来
作者:
pegasus
时间:
2015-9-23 10:11
String strr = "swsdnboin王O*Uoamsonmjd9adxn晓lcancd323五";
String str1 = "";
for (int i = 0; i < strr.length(); i++) {
if (strr.substring(i, i + 1).getBytes().length == 2) {
str1 = str1 + strr.substring(i, i + 1);
}
}
System.out.println(str1);
复制代码
作者:
pegasus
时间:
2015-9-23 10:15
public static void main(String[] args) {
String regEx1 = "[\\u4e00-\\u9fa5]";
String str = "1 2fdAsz我是hhhZ大傻";
String s1 = matchResult(Pattern.compile(regEx1), str);
System.out.println(s1);
}
public static String matchResult(Pattern p, String str) {
StringBuilder sb = new StringBuilder();
Matcher m = p.matcher(str);
while (m.find())
for (int i = 0; i <= m.groupCount(); i++) {
sb.append(m.group());
}
return sb.toString();
}
复制代码
作者:
pegasus
时间:
2015-9-23 10:20
String str = "123abc中文cde123abc提取123ab我ABC汉DEF和我们678,854中华人民共和国,美国";
Pattern pattern = null;
Matcher matcher = null;
String value = null;
pattern = Pattern.compile("([\u4e00-\u9fa5]+)");
matcher = pattern.matcher(str);
while (matcher.find()) {
value = matcher.group(0);
System.out.print(value);
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2