`
liuxingshuijinglei
  • 浏览: 24258 次
  • 性别: Icon_minigender_2
  • 来自: 沈阳
文章分类
社区版块
存档分类
最新评论

String工具类

 
阅读更多
package com.sim.common.utils;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.StringTokenizer;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
*
* @desc 公共方法类 提供字符串处理的实用方法集
* @author Hongbing.Zhang
* @date 2008-3-31
*/
public class StringUtil {
public StringUtil() {
}

public static final String escapeForIntro(String string) {
// String str = escapeHTMLTags(string);
String str = string;
str = replace(str, "\r\n", "<br>");
str = replace(str, "\n", "<br>");
str = replace(str, "'", "\\'");
return replace(str, "\r", "");

}

/**
* 得到非空的字符串,若字符串对象为null,则返回""。
*
* @param objValue
*            Object待转换的原字符串对象
* @return String 转换后的字符串
* */
public static String getNotNullStr(Object objValue) {
return (objValue == null ? "" : objValue.toString());
}

/**
* 得到非空的字符串,若字符串为null,则返回""。
*
* @param strValue
*            String待转换的原字符串
* @return String 转换后的字符串
* */
public static String getNotNullStr(String strValue) {
return (strValue == null ? "" : strValue.trim());
}

/**
* 用"0"补足一个字符串到指定长度
*
* @param str
*            - 源字符串
* @param size
*            - 补足后应达到的长度
* @return - 补零后的结果
*/
public static String fillZero(String str, int size) {
String result;
if (str.length() < size) {
char[] s = new char[size - str.length()];
for (int i = 0; i < (size - str.length()); i++) {
s[i] = '0';
}
result = new String(s) + str;
} else {
result = str;
}

return result;
}

/**
* 根据字符串(文件类型或者多行输入类型)获取字符串数组
*
* @param str1
*            String 输入字符串
* @return String[] 返回结果
*/
public static String[] getStrArryByString(String str1) {
if (str1.indexOf("\t") > 0) {
for (int i = 0; i < str1.length(); i++) {
if (str1.substring(i, i + 1).equals("\t")) {
str1 = str1.substring(0, i) + " "
+ str1.substring(i + 1, str1.length());
}
}
}
StringTokenizer stringTokenizer = new StringTokenizer(str1, "\r\n");
String[] strId = new String[stringTokenizer.countTokens()];
int i = 0;
while (stringTokenizer.hasMoreTokens()) {
strId[i] = stringTokenizer.nextToken();
i++;
}
return strId;
}

/**
* 判断一个字符串是否为 NUll 或为空
*
* @param inStr
*            inStr
* @return boolean
*/
public static boolean isValid(String inStr) {
if (inStr == null) {
return false;
} else if (inStr.equals("")) {
return false;
} else if (inStr.equals("null")) {
return false;
} else {
return true;
}
}

/**
* 判断一个字符串是否为 NUll 或为空
*
* @param inStr
*            inStr
* @return boolean
*/
public static boolean checkNotNull(String str) {
boolean flag = false;

if (str != null && str.trim().length() != 0)
flag = true;
return flag;
}

/**
* 获得指定长度的空格
*
* @param spaceNum
*            spaceNum
* @return String
*/
public static String getStrSpace(int spaceNum) {
return getStrWithSameElement(spaceNum, " ");
}

/**
* 获得指定长度的字符串
*
* @param num
*            int
* @param element
*            String
* @return String
*/
public static String getStrWithSameElement(int num, String element) {
if (num <= 0) {
return "";
}

StringBuffer sb = new StringBuffer();
for (int i = 0; i < num; i++) {
sb.append(element);
}
return sb.toString();
}

/**
* 从右或左加空格
*
* @param strIn
*            String
* @param totalLength
*            int
* @param isRightAlign
*            boolean
* @return String
*/
public static String getFillString(String strIn, int totalLength,
boolean isRightAlign) {
int spaceLength = 0;
String spaces = null;
String strOut = null;

if (strIn == null) {
strIn = "";
}

spaceLength = totalLength - strIn.length();

if (spaceLength < 0) {
spaceLength = 0;
}
spaces = StringUtil.getStrSpace(spaceLength);

if (isRightAlign) {
strOut = spaces + strIn;
} else {
strOut = strIn + spaces;

}
return strOut;
}

/**
* 以String类型返回错误抛出的堆栈信息
*
* @param t
*            Throwable
* @return String
*/
public static String getStackTrace(Throwable t) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);

t.printStackTrace(pw);
return sw.toString();
}

/**
* 转换字符串第一个字符为大写
*
* @param str
*            String
* @return String
*/
public static String getStrByUpperFirstChar(String str) {
try {
return str.substring(0, 1).toUpperCase() + str.substring(1);
} catch (Exception e) {
return "";
}

}

/**
* 转换字符串第一个字符为小写
*
* @param str
*            String
* @return String
*/
public static String getStrByLowerFirstChar(String str) {
try {
return str.substring(0, 1).toLowerCase() + str.substring(1);
} catch (Exception e) {
return "";
}

}

/**
* 通过字符串转换成相应的整型,并返回。
*
* @param strValue
*            String 待转换的字符串
* @return int 转换完成的整型
* */
public static int getStrToInt(String strValue) {
if (null == strValue) {
return 0;
}
int iValue = 0;
try {
iValue = new Integer(strValue.trim()).intValue();
} catch (Exception ex) {
iValue = 0;
}
return iValue;
}

/**
* 通过字符串转换成相应的DOUBLE,并返回。
*
* @param strValue
*            String 待转换的字符串
* @return double 转换完成的DOUBLE
* */
public static double getStrToDouble(String strValue) {
if (null == strValue) {
return 0;
}
double dValue = 0;
try {
dValue = Double.parseDouble(strValue.trim());
} catch (Exception ex) {
dValue = 0;
}
return dValue;
}

/**
* 通过字符串转换成相应的短整型,并返回。
*
* @param strValue
*            String 待转换的字符串
* @return short 转换完成的短整型
* */
public static short getStrToShort(String strValue) {
if (null == strValue) {
return 0;
}
short iValue = 0;
try {
iValue = new Short(strValue.trim()).shortValue();
} catch (Exception ex) {
iValue = 0;
}
return iValue;
}

/**
* 通过字符串转换成相应的长整型,并返回。
*
* @param strValue
*            String 待转换的字符串
* @return long 转换完成的长整型
* */
public static long getStrToLong(String strValue) {
if (null == strValue) {
return 0;
}
long lValue = 0;
try {
lValue = new Long(strValue.trim()).longValue();
} catch (Exception ex) {
lValue = 0;
}
return lValue;
}

public static String toLengthForEn(String str, int length) {
if (null != str) {
if (str.length() <= length) {
return str;
} else {
str = str.substring(0, length - 2);
str = str + "..";
return str;
}
} else {
return "";
}
}

/**
* 降字符串转换成给定长度的字符串,如超出的话截断,并在最后以两个点结尾
*
* @param str
*            String
* @param length
*            int
* @return String
*/
public static String toLengthForIntroduce(String str, int length) {
str = delTag(str);

byte[] strByte = str.getBytes();
int byteLength = strByte.length;
char[] charArray;
StringBuffer buff = new StringBuffer();
if (byteLength > (length * 2)) {
charArray = str.toCharArray();
int resultLength = 0;
for (int i = 0; i < charArray.length; i++) {
resultLength += String.valueOf(charArray[i]).getBytes().length;
if (resultLength > (length * 2)) {
break;
}
buff.append(charArray[i]);

}
buff.append("..");
str = buff.toString();
}

// str = replace(str, "'", "\\'");
str = replace(str, "\"", "\\\"");
str = replace(str, ",", ",");
return str;

}

public static String delTag(String str) {
str = str + "<>";
StringBuffer buff = new StringBuffer();
int start = 0;
int end = 0;

while (str.length() > 0) {
start = str.indexOf("<");
end = str.indexOf(">");
if (start > 0) {
buff.append(str.substring(0, start));
}
if (end > 0 && end <= str.length()) {
str = str.substring(end + 1, str.length());
} else {
str = "";
}

}
String result = buff.toString();

while (result.startsWith(" ")) {

result = result.substring(result.indexOf(" ") + 1, result.length());

}
return result;

}

public static final String replace(String line, String oldString,
String newString) {
if (line == null) {
return null;
}
int i = 0;
if ((i = line.indexOf(oldString, i)) >= 0) {
char[] line2 = line.toCharArray();
char[] newString2 = newString.toCharArray();
int oLength = oldString.length();
StringBuffer buf = new StringBuffer(line2.length);
buf.append(line2, 0, i).append(newString2);
i += oLength;
int j = i;
while ((i = line.indexOf(oldString, i)) > 0) {
buf.append(line2, j, i - j).append(newString2);
i += oLength;
j = i;
}
buf.append(line2, j, line2.length - j);
return buf.toString();
}
return line;

}

// Replace
public static String Replace(String source, String oldString,
String newString) {
if (source == null) {
return null;
}
StringBuffer output = new StringBuffer();
int lengOfsource = source.length();
int lengOfold = oldString.length();
int posStart = 0;
int pos;
while ((pos = source.indexOf(oldString, posStart)) >= 0) {
output.append(source.substring(posStart, pos));
output.append(newString);
posStart = pos + lengOfold;
}
if (posStart < lengOfsource) {
output.append(source.substring(posStart));
}
return output.toString();
}

// 此函数前台使用中,请勿随便修改,不然会造成显示混乱(以前修改版本在下方注释中)
public static String toHtml(String s) {
s = Replace(s, "<", "&lt;");
s = Replace(s, ">", "&gt;");
s = Replace(s, "\t", "    ");
s = Replace(s, "\r\n", "\n");
s = Replace(s, "\n", "<br>");
// s = Replace(s, " ", "&nbsp;");
s = Replace(s, "'", "&#39;");
s = Replace(s, "\"", "&quot;");
s = Replace(s, "\\", "&#92;");
s = Replace(s, "%", "%");
// s = Replace(s, "&", "&amp;");
return s;
}

// 逆
public static String unHtml(String s) {

// s = Replace(s, "&lt;", "<");
// s = Replace(s, "&gt;", ">");
// s = Replace(s, "    ", "\t");
// s = Replace(s, "\n", "\r\n");
s = Replace(s, "<br>", "\n");
// s = Replace(s, "&nbsp;", " ");
// s = Replace(s, "&amp;", "&");
// s = Replace(s, "&#39;", "'");
// s = Replace(s, "&#92;", "\\");
// s = Replace(s, "%", "%");
return s;
}

// 此函数后台使用中,请勿随便修改,不然会造成显示混乱(以前修改版本在下方注释中)
public static String toHtmlBack(String s) {
// 显示
s = Replace(s, "&", "&amp;");
s = Replace(s, "\\", "&#92;");
s = Replace(s, "\"", "&quot;");
s = Replace(s, "<", "&lt;");
s = Replace(s, ">", "&gt;");
s = Replace(s, "\t", "    ");
s = Replace(s, "\r\n", "\n");
// s = Replace(s, "\n", "<br>");
// s = Replace(s, " ", "&nbsp;");
// s = Replace(s, "'", "&#39;");
// s = Replace(s, "%", "%");

return s;
}

// 逆
public static String unHtmlBack(String s) {
s = Replace(s, "&lt;", "<");
s = Replace(s, "&gt;", ">");
s = Replace(s, "    ", "\t");
s = Replace(s, "\n", "\r\n");
s = Replace(s, "<br>", "\n");
s = Replace(s, "&nbsp;", " ");
s = Replace(s, "&amp;", "&");
s = Replace(s, "&#39;", "'");
s = Replace(s, "&#92;", "\\");
s = Replace(s, "%", "%");
return s;
}

/*
* public static String toHtml(String s) { //显示 s = Replace(s, "&",
* "&amp;"); s = Replace(s, "\\", "&#92;"); s = Replace(s, "\"", "&quot;");
* s = Replace(s, "<", "&lt;"); s = Replace(s, ">", "&gt;"); s = Replace(s,
* "\t", "    "); s = Replace(s, "\r\n", "\n"); // s = Replace(s, "\n",
* "<br>"); s = Replace(s, " ", "&nbsp;"); // s = Replace(s, "'", "&#39;");
* // s = Replace(s, "%", "%");
*
* return s; }
*
* public static String unHtml(String s) { s = Replace(s, "&lt;", "<"); s =
* Replace(s, "&gt;", ">"); s = Replace(s, "    ", "\t"); s = Replace(s,
* "\n", "\r\n"); s = Replace(s, "<br>", "\n"); s = Replace(s, "&nbsp;",
* " "); s = Replace(s, "&amp;", "&"); s = Replace(s, "&#39;", "'"); s =
* Replace(s, "&#92;", "\\"); s = Replace(s, "%", "%"); return s; }
*/
// 判断是否含有中文,如果含有中文返回ture
public static boolean containsChinese(String str)
throws UnsupportedEncodingException {

if (str.length() < (str.getBytes()).length)
return true;

return false;

// for (int i = 0; i < username.length(); i++) {
// String unit=Character.toString(username.charAt(i));
// byte[] unitByte=unit.getBytes("GBK");
// // ((unitByte[0]+256)*256 + (unitByte[1]+256)) <= 0xFEFE)
// if (unitByte.length == 2)
// {
// return true;
// }
// }
// return false;

}

public static boolean isEmpty(String str) {
if (str == null)
return true;
return "".equals(str.trim());
}

/**
* 根据输入的长度截取字符串,单个单词超过输入长度的强制加<BR>
* 换行
*
* @param str
*            输入的字符串
* @param len
*            输入的长度
* @return 处理过后的字符串
*/
public static String truncateStr(String str, int len) {
if (str != null && !("").equalsIgnoreCase(str)) {

String strs[] = str.split(" ");
StringBuffer buff = new StringBuffer();
if (strs.length > 0) {
for (int i = 0; i < strs.length; i++) {
StringBuffer temp = new StringBuffer();
while (strs[i].length() > len) {
temp.append(strs[i].substring(0, len) + "<BR>");
strs[i] = strs[i].substring(len);
}
temp.append(strs[i]);
buff.append(temp.toString() + " ");
}

}
return buff.toString();
} else {
return "";
}
}

public static String truncateStr2(String str, int len) {
if (str != null && !("").equalsIgnoreCase(str) && len != 0) {
String strs[] = str.split(" ");

StringBuffer buff = new StringBuffer();
for (int i = 0; i < strs.length; i++) {
StringBuffer temp = new StringBuffer();
String tempstr = "";
while (strs[i].length() > len) {
tempstr = strs[i].substring(0, len);
tempstr = tempstr.replaceAll(" ", "&nbsp; ");
tempstr = tempstr.replaceAll("<", "&lt; ");
tempstr = tempstr.replaceAll("\n", "<br> ")
.replaceAll("\"", "&quot; ")
.replaceAll("'", "&#39; ");
tempstr = tempstr + "<br>";
temp.append(tempstr);

strs[i] = strs[i].substring(len);
}
tempstr = strs[i];
tempstr = tempstr.replaceAll(" ", "&nbsp; ");
tempstr = tempstr.replaceAll("<", "&lt; ");
tempstr = tempstr.replaceAll("\n", "<br> ")
.replaceAll("\"", "&quot; ").replaceAll("'", "&#39; ");

temp.append(tempstr);
buff.append(temp.toString() + " ");
}

if (buff.length() > 0)
return buff.toString().substring(0, buff.length() - 1);
else
return str;
} else {
return "";
}
}

/**
* 编码转换,从unicode转换为GBK
*
* @param str
*            输入字符串
* @return str编码转换后的字符串
* @throws java.io.UnsupportedEncodingException
*/
public static String unicodeToGB(String l_S_Source)
throws UnsupportedEncodingException {
String l_S_Desc = "";
if (l_S_Source != null && !l_S_Source.trim().equals("")) {
byte l_b_Proc[] = l_S_Source.getBytes("GBK");
l_S_Desc = new String(l_b_Proc, "ISO8859_1");
}
return l_S_Desc;
}

/**
* 编码转换,从GBK转换为unicode
*
* @param str
*            输入字符串
* @return str 编码转换后的字符串
* @throws java.io.UnsupportedEncodingException
*/
public static String GBToUnicode(String l_S_Source)
throws UnsupportedEncodingException {
String l_S_Desc = "";
if (l_S_Source != null && !l_S_Source.trim().equals("")) {
byte l_b_Proc[] = l_S_Source.getBytes("ISO8859_1");
l_S_Desc = new String(l_b_Proc, "GBK");
}
return l_S_Desc;
}

/**
* Escapes a <code>String</code> according the JavaScript string literal
* escaping rules. The resulting string will not be quoted.
*
* <p>
* It escapes both <tt>'</tt> and <tt>"</tt>. In additional it escapes
* <tt>></tt> as <tt>\></tt> (to avoid <tt>&lt;/script></tt>). Furthermore,
* all characters under UCS code point 0x20, that has no dedicated escape
* sequence in JavaScript language, will be replaced with hexadecimal escape
* (<tt>\x<i>XX</i></tt>).
*/
public static String javaScriptStringEnc(String s) {
int ln = s.length();
for (int i = 0; i < ln; i++) {
char c = s.charAt(i);
if (c == '"' || c == '\'' || c == '\\' || c == '>' || c < 0x20) {
StringBuffer b = new StringBuffer(ln + 4);
b.append(s.substring(0, i));
while (true) {
if (c == '"') {
b.append("\\\"");
} else if (c == '\'') {
b.append("\\'");
} else if (c == '\\') {
b.append("\\\\");
} else if (c == '>') {
b.append("\\>");
} else if (c < 0x20) {
if (c == '\n') {
b.append("\\n");
} else if (c == '\r') {
b.append("\\r");
} else if (c == '\f') {
b.append("\\f");
} else if (c == '\b') {
b.append("\\b");
} else if (c == '\t') {
b.append("\\t");
} else {
b.append("\\x");
int x = c / 0x10;
b.append((char) (x < 0xA ? x + '0' : x - 0xA + 'A'));
x = c & 0xF;
b.append((char) (x < 0xA ? x + '0' : x - 0xA + 'A'));
}
} else {
b.append(c);
}
i++;
if (i >= ln) {
return b.toString();
}
c = s.charAt(i);
}
} // if has to be escaped
} // for each characters
return s;
}

private static StringUtil instance = null;

public static synchronized StringUtil getInstance() {
if (instance == null) {
instance = new StringUtil();
}
return instance;
}

/**
* 将多个连续空格替换为一个,"a  b"-->"a b"
*
* @param src
* @return
* @author WilliamLau
* @desc
*/
public static String trimContinuousSpace(String src) {
return src.replaceAll("\\s+", " ");
}

/**
* 除字符串中的空格、回车、换行符、制表符
*
* @param str
* @return
*/
public static String replaceBlank(String str) {
String dest = "";
if (str != null) {
Pattern p = Pattern.compile("\\s*|\t|\r|\n");
Matcher m = p.matcher(str);
dest = m.replaceAll("");
}
return dest;
}

public static String toFloatNumber(String num) {
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(2);
nf.setMinimumFractionDigits(2);
return nf.format(Double.parseDouble(num));
}

/**
*
* @desc 在一个字符串valueStr中每count为插入一个字符串str
* @param valueStr
*            原字符串
* @param count
*            计数
* @param str
*            插入字符串
* @return
* @author Hongbing.Zhang
*/
public static String getInsertStr(String valueStr, int count, String str) {
String result = null;
while (valueStr.length() > count) {
if (result == null) {
result = valueStr.substring(valueStr.length() - count,
valueStr.length());
} else {
result = valueStr.substring(valueStr.length() - count,
valueStr.length())
+ str + result;
}
valueStr = valueStr.substring(0, valueStr.length() - count);
}
if (result != null) {
result = valueStr + str + result;
} else {
result = valueStr;
}
return result;
}

public static String classToTableName(String str) {
String result = "";
int postion = 0;
if (str.indexOf("Model") != -1) {
postion = str.indexOf("Model");
result = str.substring(0, postion);
}
return result;
}

/**
*
* @Title:GenerateUUID
* @Description: 生成全库唯一ID
* @author shaojian.yu
* @date 2014年10月20日 下午4:37:49
* @return
*/
public static final String GenerateUUID() {
UUID uuid = UUID.randomUUID();
return uuid.toString();
}

/**
*
* @Title:isExistStrArray
* @Description: 判断字符串在数组中是否存在 
* @author shaojian.yu
* @date 2014年11月7日 下午3:52:17
* @param str
* @param strArray
* @return
*/
public static boolean isExistStrArray(String str, String[] strArray) {
boolean bResult = false;
if(str != null && strArray != null && strArray.length>0){
for (String temp : strArray) {
if (str.equals(temp)) {
bResult = true;
break;
}
}
}
return bResult;
}

/**
*
* @Title:getStringSplitForArray
* @Description: 将字符串数组转换为以单引号及逗号分隔的字符串
* 例:
* String[] str1 = {"a","b","c"};
* 转换后结果为
* String s = "'a','b','c'" ;
* @author shaojian.yu
* @date 2014年12月11日 下午2:00:35
* @param str
* @return
*/
public static String getStringSplitForArray(String[] str){
if(str.length>0){
StringBuffer s = new StringBuffer();
for(int i=0; i<str.length; i++){
s.append("'");
s.append(str[i]);
s.append("'");
if(i<str.length-1){
s.append(",");
}
}
return s.toString();
}else{
return "";
}
}

/**
*
* @Title:isZwZmXhx
* @Description: 只支持中文字母下划线
* @author shaojian.yu
* @date 2014年12月30日 上午10:39:57
* @param str
* @return
*/
public static Boolean isZwZmXhx(String str) {
Boolean bl = false;
// 首先,使用Pattern解释要使用的正则表达式,其中^表是字符串的开始,$表示字符串的结尾。
Pattern pt = Pattern.compile("^[0-9a-zA-Z_]+$");
// 然后使用Matcher来对比目标字符串与上面解释得结果
Matcher mt = pt.matcher(str);
// 如果能够匹配则返回true。实际上还有一种方法mt.find(),某些时候,可能不是比对单一的一个字符串,
// 可能是一组,那如果只要求其中一个字符串符合要求就可以用find方法了.
if (mt.matches()) {
bl = true;
}
return bl;
}

/**
*
* @Title:isZwZmXhx
* @Description: 只支持中文字母下划线汉字
* @author shaojian.yu
* @date 2014年12月30日 上午10:39:57
* @param str
* @return
*/
public static Boolean isZwZmXhxHz(String str) {
Boolean bl = false;
// 首先,使用Pattern解释要使用的正则表达式,其中^表是字符串的开始,$表示字符串的结尾。
Pattern pt = Pattern.compile("^[\\u4E00-\\u9FA5\\uF900-\\uFA2D\\w]+$");
// 然后使用Matcher来对比目标字符串与上面解释得结果
Matcher mt = pt.matcher(str);
// 如果能够匹配则返回true。实际上还有一种方法mt.find(),某些时候,可能不是比对单一的一个字符串,
// 可能是一组,那如果只要求其中一个字符串符合要求就可以用find方法了.
if (mt.matches()) {
bl = true;
}
return bl;
}

/**
*
* @Title:isCheckTel
* @Description: 校验联系方式
* @author cui.li
* @date 2015年3月25日 下午4:07:34
* @param str
* @return
*/
public static boolean isCheckTel(String str){
boolean result = false;
try {
String regExp = "^[\\d\\-\\+]{7,20}$";
Pattern p = Pattern.compile(regExp);
Matcher mt = p.matcher(str);
result = mt.matches();
} catch (Exception e) {
result = false;
}

return result;
}


/**
*
* @Title:checkEmail
* @Description: 校验邮箱
* @author cui.li
* @date 2015年3月25日 下午4:07:34
* @param str
* @return
*/
public static boolean checkEmail(String email) {
boolean result = false;
try {
String check = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
Pattern p = Pattern.compile(check);
Matcher mt = p.matcher(email);
result = mt.matches();
} catch (Exception e) {
result = false;
}

return result;
}



/**
*
* @Title:isValidDate
* @Description: 判断日期格式:yyyy-mm-dd
* @author cui.li
* @date 2015年3月25日 下午5:29:50
* @param sDate
* @return
*/
    public static boolean isValidDate(String sDate) {
        String datePattern1 = "\\d{4}-\\d{2}-\\d{2}";
        String datePattern2 = "^((\\d{2}(([02468][048])|([13579][26]))"
                + "[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|"
                + "(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?"
                + "((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?("
                + "(((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?"
                + "((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))";
        if ((sDate != null)) {
            Pattern pattern = Pattern.compile(datePattern1);
            Matcher match = pattern.matcher(sDate);
            if (match.matches()) {
                pattern = Pattern.compile(datePattern2);
                match = pattern.matcher(sDate);
                return match.matches();
            } else {
                return false;
            }
        }
        return false;
    }
   
    /**
     *
     * @Title:isCheckZip
     * @Description: 邮编校验
     * @author cui.li
     * @date 2015年3月25日 下午5:45:01
     * @param zip
     * @return
     */
    public static boolean isCheckZip(String zip) {
    boolean result = false;
try {
String check = "[1-9]\\d{5}(?!\\d)";
Pattern p = Pattern.compile(check);
Matcher mt = p.matcher(zip);
result = mt.matches();
} catch (Exception e) {
result = false;
}

return result;
    }

   
    /**
     * @Title:getNormalStrFromScientificNotation
     * @Description: 科学计数法String转普通计数法String,如:"1.3900001111E10" 转成 "13900001111";
     * @author yuanqi.jing
     * @date 2015年3月26日 上午9:17:40
     * @param scientificNotationStr
     * @return
     */
    public static String getNormalStrFromScientificNotation(String scientificNotationStr) {
    String resultStr = null;
    try {
resultStr = new DecimalFormat("00").format(Double.parseDouble(scientificNotationStr));
} catch (NumberFormatException e) {
resultStr = scientificNotationStr;//数字转换异常,原样返回
}
    return resultStr;
    }
   
    /**
     *
     * @Title:isCheckRule
     * @Description: 根据正则校验字段格式
     * @author cui.li
     * @date 2015年3月31日 上午11:49:48
     * @param check(正则表达式)
     * @param str(被校验字段)
     * @return
     */
    public static boolean isCheckRule(String check,String str) {
    boolean result = false;
try {
Pattern p = Pattern.compile(check);
Matcher mt = p.matcher(str);
result = mt.matches();
} catch (Exception e) {
result = false;
}

return result;
    }
    public static void main(String[] args) {
System.out.println(isCheckRule("^{1,10}$","34345345"));
}
}
分享到:
评论

相关推荐

    String工具类String工具类String工具类

    String工具类String工具类String工具类

    javaString工具类

    String工具类

    安卓String工具类

    安卓String工具类,验证是否是合法的邮箱 char类型转封装 复制数据Assets文件夹里的文件 时间格式

    java工具类(String)

    Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。

    String和Date工具类

    String工具类:校验是否包含表情、字符串切割、过滤空白字符、过滤特殊字符(换行符、回车符、制表符) Date工具类:时间格式化转化、时间获取、时间加减(年月日时分)、数字时间校验

    String封装工具类

    java编写的一个封装了一些比较实用的功能,具体看说明文档

    android 开发常用工具类集合

    String工具类 Toast工具类 自定义圆形imageView (CircleImageView) 自定义loadingDialog 自定义带旋转动画的loadingDialog 2018.10.27 修改Toast工具类 添加可以在服务中进行提示的toast 添加设备信息工具...

    StringUtil工具类

    StringUtil工具类 是对String 对象封装操作

    个人总结常用经典Utils工具类

    1、Utils工具类有String工具类、XmlNode节点工具类、BeanFactory相关、Common工具类、Cookie工具类、Date工具类、Http工具类、JDBC工具类、日志Log工具类、Servlet相关等。2、工具类省了重复造轮子的工作,可以直接...

    StringUtil.java工具类

    StringUtil工具类java学习的好东西!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!...

    C 的String类的功能很多很有用

    c++是一门好语言,其中的点点滴滴都充满了力量, 跟人以无限的瞎想,赶快加入我们的c++生力大军吧,有什么不会的,来问我,哈哈哈。

    Android静默安装常用工具类

    String工具类,可用于常见字符串操作,如: isEmpty(String str) 判断字符串是否为空或长度为0 isBlank(String str) 判断字符串是否为空或长度为0 或由空格组成 utf8Encode(String str) 以utf-8格式编码 ...

    比较全的java工具类

    功能比较全面的java工具类。包括数据库方面的,日期格式方面的,文件操作的,网络操作,String工具类以及常用类。

    String帮助工具类

    主要集成了java开发中对String类型 和一些常用到的方法进行的整合

    java常用工具类

    Java工具类,总结好的,放入即可使用,包含date工具类string工具类等

    java工具类 string转10进制sacii byte数组再转16进制字符数组再转16进制byte数组 string与BCD互转

    java工具类 : string转10进制sacii byte数组、 10进制sacii byte数组转16进制字符数组、 16进制字符数组转16进制byte数组、 string与BCD互转等方法,包含多个转换方法, 足够满足日常解包组包需求

    java工具类去掉字符串String中的.点。android开发java程序员常用工具类

    java工具类去掉字符串String中的.点。android开发java程序员常用工具类。博文介绍:http://blog.csdn.net/qq_21376985/article/details/51482313

    一个byte数组与字符串转化类库

    1.转换一个String字符串为byte数组 2.将字节数组转化为String类型的数据 3.转换一个int为byte数组 4.从字节数组中指定的位置读取一个Integer类型的数据 5.转换一个shor字符串为byte数组 6.从字节数组中指定的...

Global site tag (gtag.js) - Google Analytics