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

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

 public class StringUtils {

     private static final String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>"; // 定义script的正则表达式
private static final String regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>"; // 定义style的正则表达式
private static final String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式
private static final String regEx_space = "\\s*|\t|\r|\n";//定义空格回车换行符 /**
* @param htmlStr
* @return
* 删除Html标签
*/
public static String delHTMLTag(String htmlStr) {
Pattern p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
Matcher m_script = p_script.matcher(htmlStr);
htmlStr = m_script.replaceAll(""); // 过滤script标签 Pattern p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
Matcher m_style = p_style.matcher(htmlStr);
htmlStr = m_style.replaceAll(""); // 过滤style标签 Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
Matcher m_html = p_html.matcher(htmlStr);
htmlStr = m_html.replaceAll(""); // 过滤html标签 Pattern p_space = Pattern.compile(regEx_space, Pattern.CASE_INSENSITIVE);
Matcher m_space = p_space.matcher(htmlStr);
htmlStr = m_space.replaceAll(""); // 过滤空格回车标签
return htmlStr.trim(); // 返回文本字符串
} public static String join(String[] strs, String split) {
if (strs == null || strs.length == 0) {
return "";
}
StringBuffer sb = new StringBuffer();
sb.append(strs[0]);
for (int i = 1; i < strs.length; i++) {
if (split != null) {
sb.append(split);
} sb.append(strs[i]);
} return sb.toString();
} public static String validString(String str) {
return TextUtils.isEmpty(str) ? "" : str;
} public static String ToDBC(String input) {
if (null == input) {
return "";
} char[] c = input.toCharArray();
for (int i = 0; i < c.length; i++) {
if (c[i] == 12288) {
c[i] = (char) 32;
continue;
}
if (c[i] > 65280 && c[i] < 65375)
c[i] = (char) (c[i] - 65248);
}
return new String(c);
} /**
* 验证输入的邮箱格式是否符合
*
* @param email
* @return 是否合法
*/
public static boolean isEmail(String email) {
boolean tag = true;
final String pattern1 = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
final Pattern pattern = Pattern.compile(pattern1);
final Matcher mat = pattern.matcher(email);
if (!mat.find()) {
tag = false;
}
return tag;
}
/**
* str -- >yyyy-MM-dd
*
* @param strDate
* @return
*/
@SuppressLint("SimpleDateFormat")
public static Date toDate(String strDate) {
Date date = null;
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
date = sdf.parse(strDate);
} catch (ParseException e) {
System.out.println(e.getMessage());
}
return date; }
}

有意思的String字符工具类的更多相关文章

  1. Java原生隐藏字符-工具类

    package com.seesun2012.common.util; /** 隐藏字符-工具类 @author seesun2012@163.com */ public class HiddenCh ...

  2. dotnet中文字符工具类

    支持繁体简体互换. using System; using System.Collections.Generic; using System.IO; using System.Linq; using ...

  3. .net使用正则表达式校验、匹配字符工具类

    开发程序离不开数据的校验,这里整理了一些数据的校验.匹配的方法: /// <summary> /// 字符(串)验证.匹配工具类 /// </summary> public c ...

  4. String字符串工具类

    字符串类(StringUtil.cs) using System; namespace Sam.OA.Common { /// <summary> /// 字符处理工具类 /// 作者:陈 ...

  5. 一个处理Date与String的工具类

    public class DateUtil { private DateUtil(){ } public static final String hhmmFormat="HH:mm" ...

  6. javaSE中级篇2 — 工具类篇 — 更新完毕

    1.工具类(也叫常用类)-- 指的是别人已经写好了的,我们只需要拿来用就行了 官网网址:Overview (Java Platform SE 8 ) (oracle.com) ---- 但是这个是英文 ...

  7. Math工具类

    public static void main(String[] args) { // 工具类,所有方法都以静态方法提供,没有实例存在的意义 // 不提供任何实例的方法,代表当前类属于无状态的. // ...

  8. Android PermissionUtils:运行时权限工具类及申请权限的正确姿势

    Android PermissionUtils:运行时权限工具类及申请权限的正确姿势 ifadai 关注 2017.06.16 16:22* 字数 318 阅读 3637评论 1喜欢 6 Permis ...

  9. jdbc 11: 封装自己的jdbc工具类

    jdbc连接mysql,封装自己的jdbc工具类 package com.examples.jdbc.utils; import java.sql.*; import java.util.Resour ...

随机推荐

  1. 分页语句-取出sql表中第31到40的记录(以自动增长ID为主键)

    sql server方案1: id from t order by id ) orde by id sql server方案2: id from t order by id) order by id ...

  2. [NOI2018] 归程 可持久化并查集

    题目描述 本题的故事发生在魔力之都,在这里我们将为你介绍一些必要的设定. 魔力之都可以抽象成一个n 个节点.m 条边的无向连通图(节点的编号从 1至 n).我们依次用 l,a描述一条边的长度.海拔. ...

  3. XMU 1071 圣斗士黄金十二宫(七)银河星爆 【计算几何】

    1071: 圣斗士黄金十二宫(七)银河星爆 Time Limit: 500 MS  Memory Limit: 64 MBSubmit: 193  Solved: 10[Submit][Status] ...

  4. 《Visual C++ 2010入门教程》系列六:VC2010常见调试技术

    <Visual C++ 2010入门教程>系列六:VC2010常见调试技术   犹豫了好久,最终还是决定开始这一章,因为我不清楚到底有没有必要写这样的一章,是应该在这里说明一些简单的调试方 ...

  5. ZOJ - 1610 Count the Colors(线段树区间更新,单点查询)

    1.给了每条线段的颜色,存在颜色覆盖,求表面上能够看到的颜色种类以及每种颜色的段数. 2.线段树区间更新,单点查询. 但是有点细节,比如: 输入: 2 0 1 1 2 3 1 输出: 1 2 这种情况 ...

  6. 《Microsoft COCO Captions Data Collection and Evaluation Server》论文笔记

    出处:CVPR2015 Motivation 本文描述了MSCoco标题数据集及评估服务器(Microsoft COCO Caption dataset and evaluation server), ...

  7. java.lang.NoSuchMethodError: org.springframework.web.context.ConfigurableWebApplicationContext.setId

    运行spring报了这个错误,网上说是spring版本冲突,检查maven依赖,发现我依赖的是spring-core.3.0.5,但是spring-orm和spring-tx依赖了spring-bea ...

  8. 【137】Photoshop相关功能

    1. photoshop中怎样批处理操作 图文教程 来源:http://www.jb51.net/photoshop/57784.html 不知道大家有没有接触过PS里面的批处理呢,当我们要完成数百张 ...

  9. php 和 js之间使用json通信

    有时候我们需要用后台从数据库中得到的数据在js中进行处理,但是当从php中获取到数据的时候,使用的是键值对形式的多维关联数组.而我们知道,js只支持索引数组,不支持关联数组,这个时候从后台传递过来的数 ...

  10. C#托盘图标

    在C#中实现托盘是多么简单 http://www.cnblogs.com/anytao/archive/2006/04/26/385377.html http://www.cnblogs.com/du ...