今天又用了将近一个下午的时间将Java中String类以及常用的一些操作进行归类,写下了如下的一个工具类如下。关于各个方法的注释一个很清楚了这里就不在解释了!
package cn.zzu.wgp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class WgpStringUtils {
private static Pattern numericPattern = Pattern.compile("^[0-9\\-]+$");// 判断是否为数字
/**
* 字符串为 null或"" 时返回true
*
* @param string
* @return
*/
public static boolean isNull(String string) {
return string == null || string.trim().equals("") || string.trim().equalsIgnoreCase("null");
}
/**
* 字符串不为空时返回true
*
* @param string
* @return
*/
public static boolean notNull(String string) {
return !isNull(string);
}
/**
* 分割字符,从开始到第一个split字符串为止
*
* @param src
* 源字符串
* @param split
* 截止字符串
* @return
*/
public static String subStr(String src, String split) {
if (!isNull(src)) {
int index = src.indexOf(split);
if (index >= 0) {
return src.substring(0, index);
}
}
return src;
}
/**
* 通过规格切割一个字符返回一个字符数组
*
* @param src
* 要切割的字符
* @param split
* 分割的规则
* @return
*/
public static String[] subStrArray(String src, String split) {
String[] str = null;
if (!isNull(src)) {
str = src.split(split);
}
return str;
}
/**
* 去除页面的非法字符检查
*
* @author shazao
* @param str
* @return
*/
public static String replaceStr(String str) {
if (str != null && str.length() > 0) {
str = str.replaceAll("~", ",");
str = str.replaceAll(" ", ",");
str = str.replaceAll(" ", ",");
str = str.replaceAll(" ", ",");
str = str.replaceAll("`", ",");
str = str.replaceAll("!", ",");
str = str.replaceAll("@", ",");
str = str.replaceAll("#", ",");
str = str.replaceAll("\\$", ",");
str = str.replaceAll("%", ",");
str = str.replaceAll("\\^", ",");
str = str.replaceAll("&", ",");
str = str.replaceAll("\\*", ",");
str = str.replaceAll("\\(", ",");
str = str.replaceAll("\\)", ",");
str = str.replaceAll("-", ",");
str = str.replaceAll("_", ",");
str = str.replaceAll("=", ",");
str = str.replaceAll("\\+", ",");
str = str.replaceAll("\\{", ",");
str = str.replaceAll("\\[", ",");
str = str.replaceAll("\\}", ",");
str = str.replaceAll("\\]", ",");
str = str.replaceAll("\\|", ",");
str = str.replaceAll("\\\\", ",");
str = str.replaceAll(";", ",");
str = str.replaceAll(":", ",");
str = str.replaceAll("'", ",");
str = str.replaceAll("\\\"", ",");
str = str.replaceAll("<", ",");
str = str.replaceAll(">", ",");
str = str.replaceAll("\\.", ",");
str = str.replaceAll("\\?", ",");
str = str.replaceAll("/", ",");
str = str.replaceAll("~", ",");
str = str.replaceAll("`", ",");
str = str.replaceAll("!", ",");
str = str.replaceAll("@", ",");
str = str.replaceAll("#", ",");
str = str.replaceAll("$", ",");
str = str.replaceAll("%", ",");
str = str.replaceAll("︿", ",");
str = str.replaceAll("&", ",");
str = str.replaceAll("×", ",");
str = str.replaceAll("(", ",");
str = str.replaceAll(")", ",");
str = str.replaceAll("-", ",");
str = str.replaceAll("_", ",");
str = str.replaceAll("+", ",");
str = str.replaceAll("=", ",");
str = str.replaceAll("{", ",");
str = str.replaceAll("[", ",");
str = str.replaceAll("}", ",");
str = str.replaceAll("]", ",");
str = str.replaceAll("|", ",");
str = str.replaceAll("\", ",");
str = str.replaceAll(":", ",");
str = str.replaceAll(";", ",");
str = str.replaceAll(""", ",");
str = str.replaceAll("'", ",");
str = str.replaceAll("<", ",");
str = str.replaceAll(",", ",");
str = str.replaceAll(">", ",");
str = str.replaceAll(".", ",");
str = str.replaceAll("?", ",");
str = str.replaceAll("/", ",");
str = str.replaceAll("·", ",");
str = str.replaceAll("¥", ",");
str = str.replaceAll("……", ",");
str = str.replaceAll("(", ",");
str = str.replaceAll(")", ",");
str = str.replaceAll("——", ",");
str = str.replaceAll("-", ",");
str = str.replaceAll("【", ",");
str = str.replaceAll("】", ",");
str = str.replaceAll("、", ",");
str = str.replaceAll("”", ",");
str = str.replaceAll("’", ",");
str = str.replaceAll("《", ",");
str = str.replaceAll("》", ",");
str = str.replaceAll("“", ",");
str = str.replaceAll("。", ",");
}
return str;
}
/**
* 移除html标签
*
* @param htmlstr
* @return
*/
public static String removeHtmlTag(String htmlstr) {
Pattern pat = Pattern.compile("\\s*<.*?>\\s*", Pattern.DOTALL | Pattern.MULTILINE | Pattern.CASE_INSENSITIVE); // \\s?[s|Sc|Cr|Ri|Ip|Pt|T]
Matcher m = pat.matcher(htmlstr);
String rs = m.replaceAll("");
rs = rs.replaceAll(" ", " ");
rs = rs.replaceAll("<", "<");
rs = rs.replaceAll(">", ">");
return rs;
}
/*
* 判断两个字符串是否相等 如果等为null 则判断相等,一个为null另一个not null则判断不相等 否则如果s1=s2则相等
*
* @param s1
*
* @param s2
*/
public static boolean equals(String s1, String s2) {
if (isNull(s1) && isNull(s2)) {
return true;
} else if (!isNull(s1) && !isNull(s2)) {
return s1.equals(s2);
}
return false;
}
/**
* 判断一个字符串是否为数字
*
* @param src
* @return
*/
public static boolean isNumeric(String src) {
boolean return_value = false;
if (src != null && src.length() > 0) {
Matcher m = numericPattern.matcher(src);
if (m.find()) {
return_value = true;
}
}
return return_value;
}
/**
* 检查字符串是否属于手机号
*
* @param phone
* @return
*/
public static boolean isMobile(String phone) {
if (isNull(phone))
return false;
if (phone.trim().length() != 11 || !isNumeric(phone))
return false;
if (!phone.trim().substring(0, 2).equals("13") && !phone.trim().substring(0, 2).equals("15")
&& !phone.trim().substring(0, 2).equals("18"))
return false;
return true;
}
// public static String random(int bit) {
// if (bit == 0)
// bit = 6;
// String str = "";
// str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz";//
// 初始化种子
return RandomStringUtils.random(bit, str);// 返回6位的字符串
// }
/**
* 把字符串转换为html代码
*
* @param str
* @return
*/
public static String replaceHtml(String str) {
try {
str = str.trim();
str = str.replaceAll("&", "&");
str = str.replaceAll("<", "<");
str = str.replaceAll(">", ">");
str = str.replaceAll(" ", " ");
str = str.replace("'", "'");
str = str.replaceAll("\"", """);
str = str.replace("\r\n", "<br />");
str = str.replace("\n", "<br />");
str = str.replace("\r", "<br />");
return str;
} catch (NullPointerException e) {
return null;
}
}
/**
* 转换NULL为""
* @param sou
* @return
*/
public static String changeNull(String sou) {
return sou == null || sou.equals("null") ? "" : sou;
}
}