java按符号截取字符串_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();

}


版权声明:本文为weixin_42396058原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。