Java String 过滤子字符串
前几天写到获取Editor值的时候,获取的值(String)中竟然还包含一堆Html的标记.而我不需要或者根本不想要这些标签的存在.
遂寻找解决办法,研究过滤标记的方法:
目的:
把html的一些标记符(如<b></b>、<p></p>、<span></span>、<div></div>等)去掉。
解决方法有三,
第一种是在Editor编辑组件中添加escape="false"来屏蔽html标记,从组件角度着手.但是并不能起作用.画个问号?
第二种是用String类提供的方法,将html标记替换掉,从字符串角度.
第三种是用正则表达式去除带有html标记的富文本,从文本角度,我没有采取这种方法,可能这种方法效率较第二种高.
两种方法因为需要考虑的html标记元素还是比较多的,所以会导致效率降低。
我们来着重看一下第二种方法:
String 类提供的替换方法:
问题转换成:
过滤掉String(java)中指定的子字符串.
我们来看一下[官方文档]中有关字符串内容转换的方法:
String replace(char oldChar, char newChar)
Returns a new string resulting from replacing all occurrences of
oldChar in this string with
newChar.
Stringreplace(CharSequence target,CharSequence replacement)
Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.
StringreplaceAll(String regex,String replacement)
Replaces each substring of this string that matches the given regular expression with the given replacement.
String replaceFirst(String regex,String replacement)
Replaces the first substring of this string that matches the givenregular expression with the given replacement.
String toLowerCase()
Converts all of the characters in this
String to lower case using the rules of the default locale.
StringtoLowerCase(Locale locale)
Converts all of the characters in this
String to lower case using the rules of the given
Locale.
StringtoString()
This object (which is already a string!) is itself returned.
StringtoUpperCase()
Converts all of the characters in this
String to upper case using the rules of the default locale.
StringtoUpperCase(Locale locale)
Converts all of the characters in this
String to upper case using the rules of the given
Locale.
Stringtrim()
Returns a copy of the string, with leading and trailing whitespace omitted.
正好有我们可以用的方法,将标签一个一个的去掉:
String.replaceAll(String s1,String s2);
例如:
code.replaceAll("<br>","").replaceAll(" ","");
过滤前:
20:44:20,593 INFO [cn.edu.sdut.softlab.service.AbstractFacade] (default task-23)
public class Student { <br> public static void main(String[] args) { <br> System.out.println("hello world"); <br> } }
过滤后
20:44:21,595 INFO [cn.edu.sdut.softlab.service.AbstractFacade] (default task-23)
public class Student { public static void main(String[] args) { System.out.println("hello world"); } }
参考资料:
http://docs.Oracle.com/javase/7/docs/api/java/lang/String.html
http://www.cnblogs.com/technology/archive/2012/09/26/2703445.html https://stackoverflow.com/questions/11520885/primeface-editor-value-display-without-tags
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。