Java模块 -- String字符串操作(数字,汉字,特殊符号过滤/截取)
使用正则表达式,截取String字符串中的数字、汉字,以及过滤特殊符号
/**
* 提取字符串中的数字
*
* @param number
* @return
* @throws Exception
*/
public String numberIntercept(String number) throws Exception {
return Pattern.compile("[^0-9]").matcher(number).replaceAll("");
}
/**
* 提取字符串中所有的汉字
*
* @param str
* @return
* @throws Exception
*/
public String intercept(String str) throws Exception {
String regex = "[u4E00-u9FA5]";//汉字
Matcher matcher = Pattern.compile(regex).matcher(str);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
sb.append(matcher.group());
}
return sb.toString();
}
/**
* 过滤设置的特殊符号
*
* @param str
* @return
* @throws Exception
*/
public String filtration(String str) throws Exception {
String regEx = "[`~!@#$%^&*()+=|{}:;\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]";
return Pattern.compile(regEx).matcher(str).replaceAll("").trim();
}声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。
