Java String 过滤子字符串
来源:互联网 发布:tidb 源码分析 编辑:程序博客网 时间:2024/05/16 00:53
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 withnewChar
.String
replace(CharSequence target,CharSequence replacement)
Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.
String
replaceAll(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.String
toLowerCase(Locale locale)
Converts all of the characters in this
String
to lower case using the rules of the givenLocale
.String
toString()
This object (which is already a string!) is itself returned.
String
toUpperCase()
Converts all of the characters in this
String
to upper case using the rules of the default locale.String
toUpperCase(Locale locale)
Converts all of the characters in this
String
to upper case using the rules of the givenLocale
.String
trim()
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
阅读全文
0 0
- Java String 过滤子字符串
- String 过滤字符串
- java String 中 startsWith() 对字符串开头进行过滤 endsWith()对字符串结尾进行过滤
- java.lang.string.split作用:分割字符串为子字符串
- java过滤特殊字符串
- java字符串乱码过滤
- java过滤特殊字符串
- java过滤特殊字符串
- java过滤特殊字符串
- java过滤特殊字符串
- Java过滤字符串,获取
- Java String 字符串中的最大相同子串实现
- 【java编程】String之两个字符串获取最大子串
- string 子字符串替换函数
- python String子字符串方法
- Java模块 -- String字符串操作(数字,汉字,特殊符号过滤/截取)
- Java中String类常用方法(字符串中的子字符串的个数)
- JAVA-字符串过滤类
- struts2处理请求
- hustoj 管理员和后台设置
- Example
- java基础篇之多态&接口
- 创建参数化测试
- Java String 过滤子字符串
- [数据库事务与锁]详解四: 数据库的锁机制
- LSA5 中FA地址为非0的情况
- 一个脚本搞定开发环境tomcat的部署更新,配置文件替换,图片备份等等
- 前端进阶之路--七大境界
- 设计模式之原型模式(Prototype)
- Java 并行程序设计模式 (Master-Worker模式)
- xtrabackup备份与恢复
- flask中request的一些疑惑