



下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 煤炭供应链效能提升考核试卷
- 法律法规体系完善与实施考核试卷
- 会计类服务合同标准文本
- 仓管员劳动合同标准文本
- 养生馆雇佣合同标准文本
- 热电联产在能源综合利用的案例分析考核试卷
- 冷库项目合同标准文本
- 文具批发商的市场调研方法应用考核试卷
- 乐器销售合同标准文本
- 农场门窗安装合同标准文本
- 二年级除法口算天天练
- xx单位政务云商用密码应用方案V2.0
- 五年级数学下册数学易错题(60道)
- 医院用电安全知识培训
- 游戏拼图课件教学课件
- 2023年浙江农商联合银行招聘笔试真题
- 在线家庭教育行业经营分析报告
- 养老护理技术操作规范及评分标准(详细量化)
- 2022年中级灭火救援员资格考试题库及答案解析
- 2024年1月浙江省高考英语真题试卷含答案
- 《篮球:持球交叉步突破》教案四篇
评论
0/150
提交评论