博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
有意思的String字符工具类
阅读量:4488 次
发布时间:2019-06-08

本文共 3769 字,大约阅读时间需要 12 分钟。

 

  对String的操作是Java攻城师必备的,一个优秀的攻城师是懒惰,他会把自己的一些常见的代码写成可提供拓展和复用的工具类或者工具库,这些是这些优秀工程师的法宝。

  我就先从String这个基本操作开始吧,Android里有个TextUtils的类,没事点开也看看。

 

1 public class StringUtils {  2       3       4     private static final String regEx_script = "
]*?>[\\s\\S]*?<\\/script>"; // 定义script的正则表达式 5 private static final String regEx_style = "
]*?>[\\s\\S]*?<\\/style>"; // 定义style的正则表达式 6 private static final String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式 7 private static final String regEx_space = "\\s*|\t|\r|\n";//定义空格回车换行符 8 9 /** 10 * @param htmlStr 11 * @return 12 * 删除Html标签 13 */ 14 public static String delHTMLTag(String htmlStr) { 15 Pattern p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE); 16 Matcher m_script = p_script.matcher(htmlStr); 17 htmlStr = m_script.replaceAll(""); // 过滤script标签 18 19 Pattern p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE); 20 Matcher m_style = p_style.matcher(htmlStr); 21 htmlStr = m_style.replaceAll(""); // 过滤style标签 22 23 Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE); 24 Matcher m_html = p_html.matcher(htmlStr); 25 htmlStr = m_html.replaceAll(""); // 过滤html标签 26 27 Pattern p_space = Pattern.compile(regEx_space, Pattern.CASE_INSENSITIVE); 28 Matcher m_space = p_space.matcher(htmlStr); 29 htmlStr = m_space.replaceAll(""); // 过滤空格回车标签 30 return htmlStr.trim(); // 返回文本字符串 31 } 32 33 34 public static String join(String[] strs, String split) { 35 if (strs == null || strs.length == 0) { 36 return ""; 37 } 38 StringBuffer sb = new StringBuffer(); 39 sb.append(strs[0]); 40 for (int i = 1; i < strs.length; i++) { 41 if (split != null) { 42 sb.append(split); 43 } 44 45 sb.append(strs[i]); 46 } 47 48 return sb.toString(); 49 } 50 51 public static String validString(String str) { 52 return TextUtils.isEmpty(str) ? "" : str; 53 } 54 55 public static String ToDBC(String input) { 56 if (null == input) { 57 return ""; 58 } 59 60 char[] c = input.toCharArray(); 61 for (int i = 0; i < c.length; i++) { 62 if (c[i] == 12288) { 63 c[i] = (char) 32; 64 continue; 65 } 66 if (c[i] > 65280 && c[i] < 65375) 67 c[i] = (char) (c[i] - 65248); 68 } 69 return new String(c); 70 } 71 72 73 /** 74 * 验证输入的邮箱格式是否符合 75 * 76 * @param email 77 * @return 是否合法 78 */ 79 public static boolean isEmail(String email) { 80 boolean tag = true; 81 final String pattern1 = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$"; 82 final Pattern pattern = Pattern.compile(pattern1); 83 final Matcher mat = pattern.matcher(email); 84 if (!mat.find()) { 85 tag = false; 86 } 87 return tag; 88 } 89 /** 90 * str -- >yyyy-MM-dd 91 * 92 * @param strDate 93 * @return 94 */ 95 @SuppressLint("SimpleDateFormat") 96 public static Date toDate(String strDate) { 97 Date date = null; 98 try { 99 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");100 date = sdf.parse(strDate);101 } catch (ParseException e) {102 System.out.println(e.getMessage());103 }104 return date;105 106 }107 }

 

转载于:https://www.cnblogs.com/Cyning/p/3798335.html

你可能感兴趣的文章
spring boot 整合redis --sea 方式1
查看>>
Android Http请求方法汇总
查看>>
缓存技术PK:选择Memcached还是Redis?
查看>>
深度工作:充分使用每一份脑力
查看>>
UVA529 Addition Chains
查看>>
django项目部署在Apache服务器中,静态文件路径的注意点
查看>>
转:objective-c 协议和委托
查看>>
day 55 jQuery 之事件 绑定等
查看>>
前端开源项目周报0221
查看>>
虚机克隆搭建kafka服务器集群
查看>>
Laravel-lumen 配置JWT
查看>>
MySQL常用存储引擎:MyISAM与InnoDB之华山论剑
查看>>
MVC5+EF6 --自定义控制Action访问权限
查看>>
[CF786B] Legacy
查看>>
Spring 注解@Component,@Service,@Controller,@Repository
查看>>
设置RDLC中table控件的表头在每页显示
查看>>
linux中tomcat内存溢出解决办法 分类: 测试 ...
查看>>
jQuery $.each用法
查看>>
[Luogu 3902]Increasing
查看>>
clear语句处理不同类型的数据结果
查看>>