有意思的String字符工具类
对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字符工具类的更多相关文章
- Java原生隐藏字符-工具类
package com.seesun2012.common.util; /** 隐藏字符-工具类 @author seesun2012@163.com */ public class HiddenCh ...
- dotnet中文字符工具类
支持繁体简体互换. using System; using System.Collections.Generic; using System.IO; using System.Linq; using ...
- .net使用正则表达式校验、匹配字符工具类
开发程序离不开数据的校验,这里整理了一些数据的校验.匹配的方法: /// <summary> /// 字符(串)验证.匹配工具类 /// </summary> public c ...
- String字符串工具类
字符串类(StringUtil.cs) using System; namespace Sam.OA.Common { /// <summary> /// 字符处理工具类 /// 作者:陈 ...
- 一个处理Date与String的工具类
public class DateUtil { private DateUtil(){ } public static final String hhmmFormat="HH:mm" ...
- javaSE中级篇2 — 工具类篇 — 更新完毕
1.工具类(也叫常用类)-- 指的是别人已经写好了的,我们只需要拿来用就行了 官网网址:Overview (Java Platform SE 8 ) (oracle.com) ---- 但是这个是英文 ...
- Math工具类
public static void main(String[] args) { // 工具类,所有方法都以静态方法提供,没有实例存在的意义 // 不提供任何实例的方法,代表当前类属于无状态的. // ...
- Android PermissionUtils:运行时权限工具类及申请权限的正确姿势
Android PermissionUtils:运行时权限工具类及申请权限的正确姿势 ifadai 关注 2017.06.16 16:22* 字数 318 阅读 3637评论 1喜欢 6 Permis ...
- jdbc 11: 封装自己的jdbc工具类
jdbc连接mysql,封装自己的jdbc工具类 package com.examples.jdbc.utils; import java.sql.*; import java.util.Resour ...
随机推荐
- 【Android】获取控件的宽和高
有时候我们须要在Activity的时候获取控件的宽和高来做一些操作,以下介绍三种获取宽和高的方式: 1. onWindowFocusChanged @Override public void onWi ...
- myeclipse配置hadoop开发环境
1.安装Hadoop开发插件 hadoop安装包contrib/目录下有个插件hadoop-0.20.2-eclipse-plugin.jar,拷贝到myeclipse根目录下/dropins目录下. ...
- cgi fastcgi wsgi nginx python Dispatching TurboGears Python via FCGI
https://www.nginx.com/resources/wiki/start/topics/examples/simplepythonfcgi/ 117 2018-06-28 19:56:42 ...
- mysql 转换编码方式
进入mysql 的安装文件夹找到 “ my.ini” 文件 (mysql配置文件) 一.编辑MySql的配置文件 vim /etc/my.cnf 在 [mysqld] 标签下加上三行 default ...
- WdatePicker.js的使用方法(转)
WdatePicker.js的使用方法 博客分类: 其他 1. 跨无限级框架显示 无论你把日期控件放在哪里,你都不需要担心会被外层的iframe所遮挡进而影响客户体验,因为My97日期控件是可以跨 ...
- Ural2004: Scientists from Spilkovo(德布鲁因序列&思维)
Misha and Dima are promising young scientists. They make incredible discoveries every day together w ...
- NABCD项目需求报告
项目:记账小账本 N:need 需求 根据我们的调查,很多人虽然知道记账有很多的好处,但是因为种种的原因,我们都没能养成记账的好习惯,所以我们所做的记账小软件,说到底是一个行为养成类的软件,而这类软件 ...
- WEB开发框架系列教程 (一)快速创建解决方案
执行<华东信息辅助开发工具> 程序 打开程序界面如下图 输入用户名.密码进行登录 如果暂时还没有用户名和密码,点击注册提供机器码给管理员进行注册. 管理员QQ:93346562 下图是:点 ...
- asp.net MVC 枚举类型的处理的几种方式
枚举类型本质上是int类型,整型,这是非常重要的一点. 可以使用(int)将它强制转换为 整形.如果要使用MVC5提供的新辅助方法@Html.EnumDropDownListFor()方法,就必须将枚 ...
- selenium中关于driver的小记
1: Ubuntu系统将driver放入 /usr/bin 目录下,即可直接启动浏览器. 2: WIN10系统讲Driver放入python安装的根目录下,即可直接启动浏览器. 3: WIN10 ...