下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、JAVA过滤HTML代码和截取字符串的类(包括测试用例)import java.util.regex.Pattern;public class FunctionUtil public static void main(String args) String str = "<p><font size="2"><span style="mce_style="font-size: 10.5pt">"+ "依据绩abc效管理体系的规定,公司决定于</span><span
2、 style=" mce_style="font-size: 10.5pt">"+ "2008</span><span style=" mce_style="font-size: 10.5pt">年</span><span style=" "+ "mce_style="font-size: 10.5pt">12</span><span style=" mce_style="f
3、ont-size: 10.5pt">"+ "月</span><span style=" mce_style="font-size: 10.5pt">22</span><span style=" "+ "mce_style="font-size: 10.5pt">日</span><span style=" mce_style="font-size: 10.5pt">"+
4、""-2009</span><span style=" mce_style="font-size: 10.5pt">年</span><span style=" "+ "mce_style="font-size: 10.5pt">1</span><span style=" mce_style="font-size: 10.5pt">"+ "月</span><s
5、pan style=" mce_style="font-size: 10.5pt"> 23& </span><span style=" "+ "mce_style="font-size: 10.5pt">日期间进行</span><span style=" mce_style="font-size: "+ "10.5pt">2008</span><span style="mce_
6、style="font-size: 10.5pt">年年度绩效考评工作,"+ "具体事项如下:</span></font></p>"String str_text = Html2Text(str);System.out.println(str_text);String slice = abbreviate(str_text, 100, ".");System.out.println(slice);/* param str :* source string* param width :
7、* string's byte width* param ellipsis :* a string added to abbreviate string bottom* <a href=" class="referer" target="_blank">return</a> String Object*/public static String abbreviate(String str, int width, String ellipsis) if (str = null | "".equa
8、ls(str) return ""int d = 0; / byte lengthint n = 0; / char lengthfor (; n < str.length(); n+) d = (int) str.charAt(n) > 256 ? d + 2 : d + 1; if (d > width) break;if (d > width) n = n - ellipsis.length() / 2;return str.substring(0, n > 0 ? n : 0) + ellipsis; return str = str.
9、substring(0, n);/* param str :* source string* param width :* string's byte width* param ellipsis :* a string added to abbreviate string bottom * <a href=" class="referer" target="_blank">return</a> String Object*/public static String Html2Text(String inputSt
10、ring) String htmlStr = inputString; / 含html标签的字符串 String textStr = ""java.util.regex.Pattern p_script;java.util.regex.Matcher m_script;java.util.regex.Pattern p_style;java.util.regex.Matcher m_style;java.util.regex.Pattern p_html;java.util.regex.Matcher m_html;java.util.regex.Pattern p_htm
11、l1;java.util.regex.Matcher m_html1;try String regEx_script ="<s*?script>*?>sS*?<s*?/s*?scripts*?>" / 定义script的正则表达式或<script>*?>sS*?</script> / String regEx_style ="<s*?style>*?>sS*?<s*?/s*?styles*?>" / 定义style的正则表达式或<style>*?>
12、sS*?</style>/ String regEx_html = "<>+>" / 定义HTML标签的正则表达式 String regEx_html1 = "<>+"p_script = Ppile(regEx_script,Pattern.CASE_INSENSITIVE);m_script = p_script.matcher(htmlStr);htmlStr = m_script.replaceAll(""); / 过滤script标签p_style = Ppile(regEx_sty
13、le,Pattern.CASE_INSENSITIVE);m_style = p_style.matcher(htmlStr);htmlStr = m_style.replaceAll(""); / 过滤style标签p_html = Ppile(regEx_html,Pattern.CASE_INSENSITIVE);m_html = p_html.matcher(htmlStr);htmlStr = m_html.replaceAll(""); / 过滤html标签p_html1 = Ppile(regEx_html1,Pattern.CASE_INSENSITIVE);m_html1
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年企业数字化转型成本控制方案
- 城市应急巡检优化-洞察与解读
- 图神经网络中的异构融合-洞察与解读
- 皖南医学院儿科学教案04新生儿败血症
- 暖通管线施工工艺方案
- 城市环境卫生综合治理方案
- 垃圾处理设施建设方案
- BIM建筑外立面设计方案
- 有限空间作业练习试题附答案
- 江师大考研题库及答案
- 天津市河东区2026届高一上数学期末考试试题含解析
- 消化内镜ERCP技术改良
- DB37-T6005-2026人为水土流失风险分级评价技术规范
- 云南师大附中2026届高三1月高考适应性月考卷英语(六)含答案
- 2026湖北随州农商银行科技研发中心第二批人员招聘9人笔试备考试题及答案解析
- 纪念馆新馆项目可行性研究报告
- 仁爱科普版(2024)八年级上册英语Unit1~Unit6补全对话练习题(含答案)
- 骑行美食活动方案策划(3篇)
- 石化企业环保培训课件
- 2026年吕梁职业技术学院单招职业技能考试备考试题带答案解析
- 2025年新疆师范大学辅导员招聘考试真题及答案
评论
0/150
提交评论