当前位置: 代码迷 >> Java Web开发 >> 求一字符串过滤代码
  详细解决方案

求一字符串过滤代码

热度:88   发布时间:2016-04-17 14:05:26.0
求一字符串过滤代码~
要求:没有空格,只提取汉字以及非HTML标签的字母~在线求~按时结帖~

------解决方案--------------------
replace( " ", " ");
------解决方案--------------------
string.replace( " ", " ");就可以啦
------解决方案--------------------
1、用str.replace( " ", " ")可以去除空格
2、去除html:用正则表达式
public String clearHtml(String str){

// <[^> ]+> | </[^> ]+> 为匹配html的正则式
Pattern p = Pattern.compile( " <[^> ]+> | </[^> ]+> ");

Matcher m = p.matcher(str);
// 返回去除html 后的字符串
return m.replaceAll( " ");

}
比如:clearHtml( " <a href= 'asdfs.jsp '> 呵呵 </a> ");将返回 呵呵

3、提取汉字,也是用正则
匹配汉字的正则为:
^[\u4e00-\u9fa5](\s*[\u4e00-\u9fa5])*
具体的用法如2
  相关解决方案