对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. Spring简单实现数据源的动态切换

    Spring简单实现数据源的动态切换: 1. 创建一个数据源切换类: 2. 继承AbstractRoutingDataSource,创建多数据源路由类,并注入到spring的配置文件中: 3. AOP ...

  2. mongodb09----replicattion set--健壮性

    replication set复制集 replicattion set 多台服务器维护相同的数据副本,提高服务器的可用性.一台是服务器出问题了另外2台还可以接收干,secondary平时保持只读状态, ...

  3. 【SDOI 2014】 旅行

    [题目链接] 点击打开链接 [算法] 树链剖分 每个宗教建一棵线段树,注意数据量大,要动态开点 [代码] #include<bits/stdc++.h> using namespace s ...

  4. Java Socket传输数据的文件系统介绍

    转自:http://developer.51cto.com/art/201003/189963.htm Java Socket传输数据在进行的时候有很多的事情需要我们不断的进行有关代码的学习.只有不断 ...

  5. 13_传智播客iOS视频教程_OC程序的编译链接

    C程序的编译.链接.执行怎么来的?在.C文件里面写上符合C语言部分的源代码.OC也是一样的.记住:OC程序的后缀名是.m. 为什么要链接?第一个.o的目标文件里面它启动不了.因为它没有启动代码我们要加 ...

  6. c的free注意事项和c++的简洁(析构大哥)

    #include <iostream> using namespace std; // ////c语言版本 //struct stu //{ // char *name; // int a ...

  7. E20180305-hm-xa

    raw adj. 生的,未加工的; 无经验的; 新近完成的; 发炎的,疼痛的; payload n. 有效载荷; (航天器.卫星的) 装备; (车辆等的) 装载货物; (炸弹.导弹的) 爆炸力;

  8. Linux 常用命令六 cp和mv

    一.cp命令 Linux中的复制命令. 复制文件: wang@wang:~/workpalce/python$ tree . ├── .txt ├── dir └── module directori ...

  9. bzoj 3555: [Ctsc2014]企鹅QQ【hash+瞎搞】

    首先注意 先hash一下,双hash,然后枚举删去位置,把hash值排个序,把些相等的加起来统计一下对数即可 #include<iostream> #include<cstdio&g ...

  10. HttpSession讲解

    1:session进行身份验证的原理: 当客户端第一次访问服务器的时候,此时客户端的请求中不携带任何标识给服务器,所以此时服务器无法找到与之对应的 session,所以会新建session对象,当服务 ...