0001 /**
0002  * all rights reserved by zhanqiong, 2005
0003  */
0004 package com.koubei.util;
0005  
0006 import java.beans.XMLDecoder;
0007 import java.io.BufferedInputStream;
0008 import java.io.ByteArrayInputStream;
0009 import java.io.UnsupportedEncodingException;
0010 import java.net.URLEncoder;
0011 import java.text.DecimalFormat;
0012 import java.util.ArrayList;
0013 import java.util.HashMap;
0014 import java.util.HashSet;
0015 import java.util.Iterator;
0016 import java.util.LinkedHashMap;
0017 import java.util.List;
0018 import java.util.Map;
0019 import java.util.Set;
0020 import java.util.regex.Matcher;
0021 import java.util.regex.Pattern;
0022  
0023 import org.apache.commons.lang.RandomStringUtils;
0024 import org.apache.commons.logging.Log;
0025 import org.apache.commons.logging.LogFactory;
0026  
0027 import com.opensymphony.util.TextUtils;
0028  
0029 /**
0030  * @author chen
0031  *
0032  */
0033 public class StringUtil {
0034     private static Pattern numericPattern = Pattern.compile("^[0-9\\-]+$");
0035     private static Pattern numericStringPattern = Pattern
0036             .compile("^[0-9\\-\\-]+$");
0037     private static Pattern floatNumericPattern = Pattern
0038             .compile("^[0-9\\-\\.]+$");
0039     private static Pattern abcPattern = Pattern.compile("^[a-z|A-Z]+$");
0040     public static final String splitStrPattern = ",|,|;|;|、|\\.|。|-|_|\\(|\\)|\\[|\\]|\\{|\\}|\\\\|/| | |\"";
0041     private static Log logger = LogFactory.getLog(StringUtil.class);
0042  
0043     /**
0044      * 判断是否数字表示
0045      *
0046      * @param src
0047      *            源字符串
0048      * @return 是否数字的标志
0049      */
0050     public static boolean isNumeric(String src) {
0051         boolean return_value = false;
0052         if (src != null && src.length() > 0) {
0053             Matcher m = numericPattern.matcher(src);
0054             if (m.find()) {
0055                 return_value = true;
0056             }
0057         }
0058         return return_value;
0059     }
0060  
0061     /**
0062      * 判断是否数字表示
0063      *
0064      * @param src
0065      *            源字符串
0066      * @return 是否数字的标志
0067      */
0068     public static boolean isNumericString(String src) {
0069         boolean return_value = false;
0070         if (src != null && src.length() > 0) {
0071             Matcher m = numericStringPattern.matcher(src);
0072             if (m.find()) {
0073                 return_value = true;
0074             }
0075         }
0076         return return_value;
0077     }
0078  
0079     /**
0080      * 判断是否纯字母组合
0081      *
0082      * @param src
0083      *            源字符串
0084      * @return 是否纯字母组合的标志
0085      */
0086     public static boolean isABC(String src) {
0087         boolean return_value = false;
0088         if (src != null && src.length() > 0) {
0089             Matcher m = abcPattern.matcher(src);
0090             if (m.find()) {
0091                 return_value = true;
0092             }
0093         }
0094         return return_value;
0095     }
0096  
0097     /**
0098      * 判断是否浮点数字表示
0099      *
0100      * @param src
0101      *            源字符串
0102      * @return 是否数字的标志
0103      */
0104     public static boolean isFloatNumeric(String src) {
0105         boolean return_value = false;
0106         if (src != null && src.length() > 0) {
0107             Matcher m = floatNumericPattern.matcher(src);
0108             if (m.find()) {
0109                 return_value = true;
0110             }
0111         }
0112         return return_value;
0113     }
0114  
0115     /**
0116      * 把string array or list用给定的符号symbol连接成一个字符串
0117      *
0118      * @param array
0119      * @param symbol
0120      * @return
0121      */
0122     public static String joinString(List array, String symbol) {
0123         String result = "";
0124         if (array != null) {
0125             for (int i = 0; i < array.size(); i++) {
0126                 String temp = array.get(i).toString();
0127                 if (temp != null && temp.trim().length() > 0)
0128                     result += (temp + symbol);
0129             }
0130             if (result.length() > 1)
0131                 result = result.substring(0, result.length() - 1);
0132         }
0133         return result;
0134     }
0135  
0136     public static String subStringNotEncode(String subject, int size) {
0137         if (subject != null && subject.length() > size) {
0138             subject = subject.substring(0, size) + "...";
0139         }
0140         return subject;
0141     }
0142  
0143     public static String subString(String subject, int size) {
0144         subject = TextUtils.htmlEncode(subject);
0145         if (subject.length() > size) {
0146             subject = subject.substring(0, size) + "...";
0147         }
0148         return subject;
0149     }
0150  
0151     /**
0152      * 截取字符串 超出的字符用symbol代替   
0153      *
0154      * @param len
0155      *             字符串长度 长度计量单位为一个GBK汉字  两个英文字母计算为一个单位长度
0156      * @param str
0157      * @param symbol
0158      * @return
0159      */
0160     public static String getLimitLengthString(String str, int len, String symbol) {
0161         int iLen = len * 2;
0162         int counterOfDoubleByte = 0;
0163         String strRet = "";
0164         try {
0165             if (str != null) {
0166                 byte[] b = str.getBytes("GBK");
0167                 if (b.length <= iLen) {
0168                     return str;
0169                 }
0170                 for (int i = 0; i < iLen; i++) {
0171                     if (b[i] < 0) {
0172                         counterOfDoubleByte++;
0173                     }
0174                 }
0175                 if (counterOfDoubleByte % 2 == 0) {
0176                     strRet = new String(b, 0, iLen, "GBK") + symbol;
0177                     return strRet;
0178                 } else {
0179                     strRet = new String(b, 0, iLen - 1, "GBK") + symbol;
0180                     return strRet;
0181                 }
0182             } else {
0183                 return "";
0184             }
0185         } catch (Exception ex) {
0186             return str.substring(0, len);
0187         } finally {
0188             strRet = null;
0189         }
0190     }
0191  
0192     /**
0193      * 截取字符串 超出的字符用symbol代替   
0194      *
0195      * @param len
0196      *             字符串长度 长度计量单位为一个GBK汉字  两个英文字母计算为一个单位长度
0197      * @param str
0198      * @param symbol
0199      * @return12
0200      */
0201     public static String getLimitLengthString(String str, int len) {
0202         return getLimitLengthString(str, len, "...");
0203     }
0204  
0205     public static String subStr(String subject, int size) {
0206         subject = TextUtils.htmlEncode(subject);
0207         if (subject.length() > size) {
0208             subject = subject.substring(0, size);
0209         }
0210         return subject;
0211     }
0212  
0213     /**
0214      *
0215      * 截取字符,不转码
0216      *
0217      * @param subject
0218      * @param size
0219      * @return
0220      */
0221     public static String subStrNotEncode(String subject, int size) {
0222         if (subject.length() > size) {
0223             subject = subject.substring(0, size);
0224         }
0225         return subject;
0226     }
0227  
0228     /**
0229      * 把string array or list用给定的符号symbol连接成一个字符串
0230      *
0231      * @param array
0232      * @param symbol
0233      * @return
0234      */
0235     public static String joinString(String[] array, String symbol) {
0236         String result = "";
0237         if (array != null) {
0238             for (int i = 0; i < array.length; i++) {
0239                 String temp = array[i];
0240                 if (temp != null && temp.trim().length() > 0)
0241                     result += (temp + symbol);
0242             }
0243             if (result.length() > 1)
0244                 result = result.substring(0, result.length() - 1);
0245         }
0246         return result;
0247     }
0248  
0249     /**
0250      * 取得字符串的实际长度(考虑了汉字的情况)
0251      *
0252      * @param SrcStr
0253      *            源字符串
0254      * @return 字符串的实际长度
0255      */
0256     public static int getStringLen(String SrcStr) {
0257         int return_value = 0;
0258         if (SrcStr != null) {
0259             char[] theChars = SrcStr.toCharArray();
0260             for (int i = 0; i < theChars.length; i++) {
0261                 return_value += (theChars[i] <= 255) ? 1 : 2;
0262             }
0263         }
0264         return return_value;
0265     }
0266  
0267     /**
0268      * 检查联系人信息是否填写,电话,手机,email必须填至少一个,email填了的话检查格式
0269      *
0270      * @param phoneCity
0271      * @param phoneNumber
0272      * @param phoneExt
0273      * @param mobileNumber
0274      * @param email
0275      * @return
0276      */
0277     public static boolean checkContactInfo(String phoneCity,
0278             String phoneNumber, String phoneExt, String mobileNumber,
0279             String email) {
0280         String result = (phoneCity == null ? "" : phoneCity.trim())
0281                 + (phoneNumber == null ? "" : phoneNumber.trim())
0282                 + (phoneExt == null ? "" : phoneExt.trim())
0283                 + (mobileNumber == null ? "" : mobileNumber.trim())
0284                 + (email == null ? "" : email.trim());
0285         if (result.length() < 1)
0286             return false;
0287         if (!isEmail(email))
0288             return false;
0289  
0290         return true;
0291     }
0292  
0293     /**
0294      * 检查数据串中是否包含非法字符集
0295      *
0296      * @param str
0297      * @return [true]|[false] 包含|不包含
0298      */
0299     public static boolean check(String str) {
0300         String sIllegal = "'\"";
0301         int len = sIllegal.length();
0302         if (null == str)
0303             return false;
0304         for (int i = 0; i < len; i++) {
0305             if (str.indexOf(sIllegal.charAt(i)) != -1)
0306                 return true;
0307         }
0308  
0309         return false;
0310     }
0311  
0312     /***************************************************************************
0313      * getHideEmailPrefix - 隐藏邮件地址前缀。
0314      *
0315      * @param email
0316      *            - EMail邮箱地址 例如: linwenguo@koubei.com 等等...
0317      * @return 返回已隐藏前缀邮件地址, 如 *********@koubei.com.
0318      * @version 1.0 (2006.11.27) Wilson Lin
0319      **************************************************************************/
0320     public static String getHideEmailPrefix(String email) {
0321         if (null != email) {
0322             int index = email.lastIndexOf('@');
0323             if (index > 0) {
0324                 email = repeat("*", index).concat(email.substring(index));
0325             }
0326         }
0327         return email;
0328     }
0329  
0330     /***************************************************************************
0331      * repeat - 通过源字符串重复生成N次组成新的字符串。
0332      *
0333      * @param src
0334      *            - 源字符串 例如: 空格(" "), 星号("*"), "浙江" 等等...
0335      * @param num
0336      *            - 重复生成次数
0337      * @return 返回已生成的重复字符串
0338      * @version 1.0 (2006.10.10) Wilson Lin
0339      **************************************************************************/
0340     public static String repeat(String src, int num) {
0341         StringBuffer s = new StringBuffer();
0342         for (int i = 0; i < num; i++)
0343             s.append(src);
0344         return s.toString();
0345     }
0346  
0347     /**
0348      * 检查是否是email, when null or '' return true
0349      *
0350      * @param email
0351      * @return
0352      */
0353     public static boolean isEmail(String email) {
0354         if (email != null && email.trim().length() > 0) {
0355             if (!TextUtils.verifyEmail(email)) {
0356                 return false;
0357             } else {
0358                 return true;
0359             }
0360         }
0361         return false;
0362     }
0363  
0364     /**
0365      * 根据指定的字符把源字符串分割成一个数组
0366      *
0367      * @param src
0368      * @return
0369      */
0370     public static List<String> parseString2ListByCustomerPattern(
0371             String pattern, String src) {
0372  
0373         if (src == null)
0374             return null;
0375         List<String> list = new ArrayList<String>();
0376         String[] result = src.split(pattern);
0377         for (int i = 0; i < result.length; i++) {
0378             list.add(result[i]);
0379         }
0380         return list;
0381     }
0382  
0383     /**
0384      * 根据指定的字符把源字符串分割成一个数组
0385      *
0386      * @param src
0387      * @return
0388      */
0389     public static List<String> parseString2ListByPattern(String src) {
0390         String pattern = ",|,|、|。";
0391         return parseString2ListByCustomerPattern(pattern, src);
0392     }
0393  
0394     /**
0395      * 格式化一个float
0396      *
0397      * @param format
0398      *            要格式化成的格式 such as #.00, #.#
0399      */
0400  
0401     public static String formatFloat(float f, String format) {
0402         DecimalFormat df = new DecimalFormat(format);
0403         return df.format(f);
0404     }
0405  
0406     /**
0407      * 判断是否是空字符串 null和"" 都返回 true
0408      *
0409      * @author Robin Chang
0410      * @param s
0411      * @return
0412      */
0413     public static boolean isEmpty(String s) {
0414         if (s != null && !s.equals("")) {
0415             return false;
0416         }
0417         return true;
0418     }
0419  
0420     /**
0421      * 自定义的分隔字符串函数 例如: 1,2,3 =>[1,2,3] 3个元素 ,2,3=>[,2,3] 3个元素 ,2,3,=>[,2,3,]
0422      * 4个元素 ,,,=>[,,,] 4个元素
0423      *
0424      * 5.22算法修改,为提高速度不用正则表达式 两个间隔符,,返回""元素
0425      *
0426      * @param split
0427      *            分割字符 默认,
0428      * @param src
0429      *            输入字符串
0430      * @return 分隔后的list
0431      * @author Robin
0432      */
0433     public static List<String> splitToList(String split, String src) {
0434         // 默认,
0435         String sp = ",";
0436         if (split != null && split.length() == 1) {
0437             sp = split;
0438         }
0439         List<String> r = new ArrayList<String>();
0440         int lastIndex = -1;
0441         int index = src.indexOf(sp);
0442         if (-1 == index && src != null) {
0443             r.add(src);
0444             return r;
0445         }
0446         while (index >= 0) {
0447             if (index > lastIndex) {
0448                 r.add(src.substring(lastIndex + 1, index));
0449             } else {
0450                 r.add("");
0451             }
0452  
0453             lastIndex = index;
0454             index = src.indexOf(sp, index + 1);
0455             if (index == -1) {
0456                 r.add(src.substring(lastIndex + 1, src.length()));
0457             }
0458         }
0459         return r;
0460     }
0461  
0462     /**
0463      * 把 名=值 参数表转换成字符串 (a=1,b=2 =>a=1&b=2)
0464      *
0465      * @param map
0466      * @return
0467      */
0468     public static String linkedHashMapToString(LinkedHashMap<String, String> map) {
0469         if (map != null && map.size() > 0) {
0470             String result = "";
0471             Iterator it = map.keySet().iterator();
0472             while (it.hasNext()) {
0473                 String name = (String) it.next();
0474                 String value = (String) map.get(name);
0475                 result += (result.equals("")) ? "" : "&";
0476                 result += String.format("%s=%s", name, value);
0477             }
0478             return result;
0479         }
0480         return null;
0481     }
0482  
0483     /**
0484      * 解析字符串返回 名称=值的参数表 (a=1&b=2 => a=1,b=2)
0485      *
0486      * @see test.koubei.util.StringUtilTest#testParseStr()
0487      * @param str
0488      * @return
0489      */
0490     @SuppressWarnings("unchecked")
0491     public static LinkedHashMap<String, String> toLinkedHashMap(String str) {
0492         if (str != null && !str.equals("") && str.indexOf("=") > 0) {
0493             LinkedHashMap result = new LinkedHashMap();
0494  
0495             String name = null;
0496             String value = null;
0497             int i = 0;
0498             while (i < str.length()) {
0499                 char c = str.charAt(i);
0500                 switch (c) {
0501                 case 61: // =
0502                     value = "";
0503                     break;
0504                 case 38: // &
0505                     if (name != null && value != null && !name.equals("")) {
0506                         result.put(name, value);
0507                     }
0508                     name = null;
0509                     value = null;
0510                     break;
0511                 default:
0512                     if (value != null) {
0513                         value = (value != null) ? (value + c) : "" + c;
0514                     } else {
0515                         name = (name != null) ? (name + c) : "" + c;
0516                     }
0517                 }
0518                 i++;
0519  
0520             }
0521  
0522             if (name != null && value != null && !name.equals("")) {
0523                 result.put(name, value);
0524             }
0525  
0526             return result;
0527  
0528         }
0529         return null;
0530     }
0531  
0532     /**
0533      * 根据输入的多个解释和下标返回一个值
0534      *
0535      * @param captions
0536      *            例如:"无,爱干净,一般,比较乱"
0537      * @param index
0538      *            1
0539      * @return 一般
0540      */
0541     public static String getCaption(String captions, int index) {
0542         if (index > 0 && captions != null && !captions.equals("")) {
0543             String[] ss = captions.split(",");
0544             if (ss != null && ss.length > 0 && index < ss.length) {
0545                 return ss[index];
0546             }
0547         }
0548         return null;
0549     }
0550  
0551     /**
0552      * 数字转字符串,如果num<=0 则输出"";
0553      *
0554      * @param num
0555      * @return
0556      */
0557     public static String numberToString(Object num) {
0558         if (num == null) {
0559             return null;
0560         } else if (num instanceof Integer && (Integer) num > 0) {
0561             return Integer.toString((Integer) num);
0562         } else if (num instanceof Long && (Long) num > 0) {
0563             return Long.toString((Long) num);
0564         } else if (num instanceof Float && (Float) num > 0) {
0565             return Float.toString((Float) num);
0566         } else if (num instanceof Double && (Double) num > 0) {
0567             return Double.toString((Double) num);
0568         } else {
0569             return "";
0570         }
0571     }
0572  
0573     /**
0574      * 货币转字符串
0575      *
0576      * @param money
0577      * @param style
0578      *            样式 [default]要格式化成的格式 such as #.00, #.#
0579      * @return
0580      */
0581  
0582     public static String moneyToString(Object money, String style) {
0583         if (money != null && style != null
0584                 && (money instanceof Double || money instanceof Float)) {
0585             Double num = (Double) money;
0586  
0587             if (style.equalsIgnoreCase("default")) {
0588                 // 缺省样式 0 不输出 ,如果没有输出小数位则不输出.0
0589                 if (num == 0) {
0590                     // 不输出0
0591                     return "";
0592                 } else if ((num * 10 % 10) == 0) {
0593                     // 没有小数
0594                     return Integer.toString((int) num.intValue());
0595                 } else {
0596                     // 有小数
0597                     return num.toString();
0598                 }
0599  
0600             } else {
0601                 DecimalFormat df = new DecimalFormat(style);
0602                 return df.format(num);
0603             }
0604         }
0605         return null;
0606     }
0607  
0608     /**
0609      * 在sou中是否存在finds 如果指定的finds字符串有一个在sou中找到,返回true;
0610      *
0611      * @param sou
0612      * @param find
0613      * @return
0614      */
0615     public static boolean strPos(String sou, String... finds) {
0616         if (sou != null && finds != null && finds.length > 0) {
0617             for (int i = 0; i < finds.length; i++) {
0618                 if (sou.indexOf(finds[i]) > -1)
0619                     return true;
0620             }
0621         }
0622         return false;
0623     }
0624  
0625     public static boolean strPos(String sou, List<String> finds) {
0626         if (sou != null && finds != null && finds.size() > 0) {
0627             for (String s : finds) {
0628                 if (sou.indexOf(s) > -1)
0629                     return true;
0630             }
0631         }
0632         return false;
0633     }
0634  
0635     public static boolean strPos(String sou, String finds) {
0636         List<String> t = splitToList(",", finds);
0637         return strPos(sou, t);
0638     }
0639  
0640     /**
0641      * 判断两个字符串是否相等 如果都为null则判断为相等,一个为null另一个not null则判断不相等 否则如果s1=s2则相等
0642      *
0643      * @param s1
0644      * @param s2
0645      * @return
0646      */
0647     public static boolean equals(String s1, String s2) {
0648         if (StringUtil.isEmpty(s1) && StringUtil.isEmpty(s2)) {
0649             return true;
0650         } else if (!StringUtil.isEmpty(s1) && !StringUtil.isEmpty(s2)) {
0651             return s1.equals(s2);
0652         }
0653         return false;
0654     }
0655  
0656     public static int toInt(String s) {
0657         if (s != null && !"".equals(s.trim())) {
0658             try {
0659                 return Integer.parseInt(s);
0660             } catch (Exception e) {
0661                 return 0;
0662             }
0663         }
0664         return 0;
0665     }
0666  
0667     public static double toDouble(String s) {
0668         if (s != null && !"".equals(s.trim())) {
0669             return Double.parseDouble(s);
0670         }
0671         return 0;
0672     }
0673  
0674     public static boolean isPhone(String phone) {
0675         if (phone == null && "".equals(phone)) {
0676             return false;
0677         }
0678         String[] strPhone = phone.split("-");
0679         try {
0680             for (int i = 0; i < strPhone.length; i++) {
0681                 Long.parseLong(strPhone[i]);
0682             }
0683  
0684         } catch (Exception e) {
0685             return false;
0686         }
0687         return true;
0688  
0689     }
0690  
0691     /**
0692      * 把xml 转为object
0693      *
0694      * @param xml
0695      * @return
0696      */
0697     public static Object xmlToObject(String xml) {
0698         try {
0699             ByteArrayInputStream in = new ByteArrayInputStream(xml
0700                     .getBytes("UTF8"));
0701             XMLDecoder decoder = new XMLDecoder(new BufferedInputStream(in));
0702             return decoder.readObject();
0703         } catch (Exception e) {
0704             e.printStackTrace();
0705         }
0706         return null;
0707     }
0708  
0709     /**
0710      * 按规定长度截断字符串,没有...的省略符号
0711      *
0712      * @param subject
0713      * @param size
0714      * @return
0715      */
0716     public static String subStringNoEllipsis(String subject, int size) {
0717         subject = TextUtils.htmlEncode(subject);
0718         if (subject.length() > size) {
0719             subject = subject.substring(0, size);
0720         }
0721         return subject;
0722     }
0723  
0724     public static long toLong(String s) {
0725         try {
0726             if (s != null && !"".equals(s.trim()))
0727                 return Long.parseLong(s);
0728         } catch (Exception exception) {
0729         }
0730         return 0L;
0731     }
0732  
0733     public static String simpleEncrypt(String str) {
0734         if (str != null && str.length() > 0) {
0735             // str = str.replaceAll("0","a");
0736             str = str.replaceAll("1", "b");
0737             // str = str.replaceAll("2","c");
0738             str = str.replaceAll("3", "d");
0739             // str = str.replaceAll("4","e");
0740             str = str.replaceAll("5", "f");
0741             str = str.replaceAll("6", "g");
0742             str = str.replaceAll("7", "h");
0743             str = str.replaceAll("8", "i");
0744             str = str.replaceAll("9", "j");
0745         }
0746         return str;
0747  
0748     }
0749  
0750     /**
0751      * 过滤用户输入的URL地址(防治用户广告) 目前只针对以http或www开头的URL地址
0752      * 本方法调用的正则表达式,不建议用在对性能严格的地方例如:循环及list页面等
0753      *
0754      * @author fengliang
0755      * @param str
0756      *            需要处理的字符串
0757      * @return 返回处理后的字符串
0758      */
0759     public static String removeURL(String str) {
0760         if (str != null)
0761             str = str.toLowerCase()
0762                     .replaceAll("(http|www|com|cn|org|\\.)+", "");
0763         return str;
0764     }
0765  
0766     /**
0767      * 随即生成指定位数的含数字验证码字符串
0768      *
0769      * @author Peltason
0770      * @date 2007-5-9
0771      * @param bit
0772      *            指定生成验证码位数
0773      * @return String
0774      */
0775     public static String numRandom(int bit) {
0776         if (bit == 0)
0777             bit = 6; // 默认6位
0778         String str = "";
0779         str = "0123456789";// 初始化种子
0780         return RandomStringUtils.random(bit, str);// 返回6位的字符串
0781     }
0782  
0783     /**
0784      * 随即生成指定位数的含验证码字符串
0785      *
0786      * @author Peltason
0787      * @date 2007-5-9
0788      * @param bit
0789      *            指定生成验证码位数
0790      * @return String
0791      */
0792     public static String random(int bit) {
0793         if (bit == 0)
0794             bit = 6; // 默认6位
0795         // 因为o和0,l和1很难区分,所以,去掉大小写的o和l
0796         String str = "";
0797         str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz";// 初始化种子
0798         return RandomStringUtils.random(bit, str);// 返回6位的字符串
0799     }
0800  
0801     /**
0802      * 检查字符串是否属于手机号码
0803      *
0804      * @author Peltason
0805      * @date 2007-5-9
0806      * @param str
0807      * @return boolean
0808      */
0809     public static boolean isMobile(String str) {
0810         if (str == null || str.equals(""))
0811             return false;
0812         if (str.length() != 11 || !isNumeric(str))
0813             return false;
0814         if (!str.substring(0, 2).equals("13")
0815                 && !str.substring(0, 2).equals("15")
0816                 && !str.substring(0, 2).equals("18"))
0817             return false;
0818         return true;
0819     }
0820  
0821     /**
0822      * Wap页面的非法字符检查
0823      *
0824      * @author hugh115
0825      * @date 2007-06-29
0826      * @param str
0827      * @return
0828      */
0829     public static String replaceWapStr(String str) {
0830         if (str != null) {
0831             str = str.replaceAll("<span class=\"keyword\">", "");
0832             str = str.replaceAll("</span>", "");
0833             str = str.replaceAll("<strong class=\"keyword\">", "");
0834             str = str.replaceAll("<strong>", "");
0835             str = str.replaceAll("</strong>", "");
0836  
0837             str = str.replace('$', '$');
0838  
0839             str = str.replaceAll("&amp;", "&");
0840             str = str.replace('&', '&');
0841  
0842             str = str.replace('<', '<');
0843  
0844             str = str.replace('>', '>');
0845  
0846         }
0847         return str;
0848     }
0849  
0850     /**
0851      * 字符串转float 如果异常返回0.00
0852      *
0853      * @param s
0854      *            输入的字符串
0855      * @return 转换后的float
0856      */
0857     public static Float toFloat(String s) {
0858         try {
0859             return Float.parseFloat(s);
0860         } catch (NumberFormatException e) {
0861             return new Float(0);
0862         }
0863     }
0864  
0865     /**
0866      * 页面中去除字符串中的空格、回车、换行符、制表符
0867      *
0868      * @author shazao
0869      * @date 2007-08-17
0870      * @param str
0871      * @return
0872      */
0873     public static String replaceBlank(String str) {
0874         if (str != null) {
0875             Pattern p = Pattern.compile("\\s*|\t|\r|\n");
0876             Matcher m = p.matcher(str);
0877             str = m.replaceAll("");
0878         }
0879         return str;
0880     }
0881  
0882     /**
0883      * 全角生成半角
0884      *
0885      * @author bailong
0886      * @date 2007-08-29
0887      * @param str
0888      * @return
0889      */
0890     public static String Q2B(String QJstr) {
0891         String outStr = "";
0892         String Tstr = "";
0893         byte[] b = null;
0894         for (int i = 0; i < QJstr.length(); i++) {
0895             try {
0896                 Tstr = QJstr.substring(i, i + 1);
0897                 b = Tstr.getBytes("unicode");
0898             } catch (java.io.UnsupportedEncodingException e) {
0899                 if (logger.isErrorEnabled()) {
0900                     logger.error(e);
0901                 }
0902             }
0903             if (b[3] == -1) {
0904                 b[2] = (byte) (b[2] + 32);
0905                 b[3] = 0;
0906                 try {
0907                     outStr = outStr + new String(b, "unicode");
0908                 } catch (java.io.UnsupportedEncodingException ex) {
0909                     if (logger.isErrorEnabled()) {
0910                         logger.error(ex);
0911                     }
0912                 }
0913             } else {
0914                 outStr = outStr + Tstr;
0915             }
0916         }
0917         return outStr;
0918     }
0919  
0920     /**
0921      *
0922      * 转换编码
0923      *
0924      * @param s
0925      *            源字符串
0926      * @param fencode
0927      *            源编码格式
0928      * @param bencode
0929      *            目标编码格式
0930      * @return 目标编码
0931      */
0932     public static String changCoding(String s, String fencode, String bencode) {
0933         try {
0934             String str = new String(s.getBytes(fencode), bencode);
0935             return str;
0936         } catch (UnsupportedEncodingException e) {
0937             return s;
0938         }
0939     }
0940  
0941     /**
0942      * *************************************************************************
0943      * 修改:刘黎明 修改时间:2007/3/1
0944      *
0945      * @param str
0946      * @return
0947      *************************************************************************
0948      */
0949     public static String removeHTMLLableExe(String str) {
0950         str = stringReplace(str, ">\\s*<", "><");
0951         str = stringReplace(str, "&nbsp;", " ");// 替换空格
0952         str = stringReplace(str, "<br ?/?>", "\n");// 去<br><br />
0953         str = stringReplace(str, "<([^<>]+)>", "");// 去掉<>内的字符
0954         str = stringReplace(str, "\\s\\s\\s*", " ");// 将多个空白变成一个空格
0955         str = stringReplace(str, "^\\s*", "");// 去掉头的空白
0956         str = stringReplace(str, "\\s*$", "");// 去掉尾的空白
0957         str = stringReplace(str, " +", " ");
0958         return str;
0959     }
0960  
0961     /**
0962      * 除去html标签
0963      *
0964      * @param str
0965      *            源字符串
0966      * @return 目标字符串
0967      */
0968     public static String removeHTMLLable(String str) {
0969         str = stringReplace(str, "\\s", "");// 去掉页面上看不到的字符
0970         str = stringReplace(str, "<br ?/?>", "\n");// 去<br><br />
0971         str = stringReplace(str, "<([^<>]+)>", "");// 去掉<>内的字符
0972         str = stringReplace(str, "&nbsp;", " ");// 替换空格
0973         str = stringReplace(str, "&(\\S)(\\S?)(\\S?)(\\S?);", "");// 去<br><br />
0974         return str;
0975     }
0976  
0977     /**
0978      * 去掉HTML标签之外的字符串
0979      *
0980      * @param str
0981      *            源字符串
0982      * @return 目标字符串
0983      */
0984     public static String removeOutHTMLLable(String str) {
0985         str = stringReplace(str, ">([^<>]+)<", "><");
0986         str = stringReplace(str, "^([^<>]+)<", "<");
0987         str = stringReplace(str, ">([^<>]+)$", ">");
0988         return str;
0989     }
0990  
0991     /**
0992      *
0993      * 字符串替换
0994      *
0995      * @param str
0996      *            源字符串
0997      * @param sr
0998      *            正则表达式样式
0999      * @param sd
1000      *            替换文本
1001      * @return 结果串
1002      */
1003     public static String stringReplace(String str, String sr, String sd) {
1004         String regEx = sr;
1005         Pattern p = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
1006         Matcher m = p.matcher(str);
1007         str = m.replaceAll(sd);
1008         return str;
1009     }
1010  
1011     /**
1012      *
1013      * 将html的省略写法替换成非省略写法
1014      *
1015      * @param str
1016      *            html字符串
1017      * @param pt
1018      *            标签如table
1019      * @return 结果串
1020      */
1021     public static String fomateToFullForm(String str, String pt) {
1022         String regEx = "<" + pt + "\\s+([\\S&&[^<>]]*)/>";
1023         Pattern p = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
1024         Matcher m = p.matcher(str);
1025         String[] sa = null;
1026         String sf = "";
1027         String sf2 = "";
1028         String sf3 = "";
1029         for (; m.find();) {
1030             sa = p.split(str);
1031             if (sa == null) {
1032                 break;
1033             }
1034             sf = str.substring(sa[0].length(), str
1035                     .indexOf("/>", sa[0].length()));
1036             sf2 = sf + "></" + pt + ">";
1037             sf3 = str.substring(sa[0].length() + sf.length() + 2);
1038             str = sa[0] + sf2 + sf3;
1039             sa = null;
1040         }
1041         return str;
1042     }
1043  
1044     /**
1045      *
1046      * 得到字符串的子串位置序列
1047      *
1048      * @param str
1049      *            字符串
1050      * @param sub
1051      *            子串
1052      * @param b
1053      *            true子串前端,false子串后端
1054      * @return 字符串的子串位置序列
1055      */
1056     public static int[] getSubStringPos(String str, String sub, boolean b) {
1057         // int[] i = new int[(new Integer((str.length()-stringReplace( str , sub
1058         // , "" ).length())/sub.length())).intValue()] ;
1059         String[] sp = null;
1060         int l = sub.length();
1061         sp = splitString(str, sub);
1062         if (sp == null) {
1063             return null;
1064         }
1065         int[] ip = new int[sp.length - 1];
1066         for (int i = 0; i < sp.length - 1; i++) {
1067             ip[i] = sp[i].length() + l;
1068             if (i != 0) {
1069                 ip[i] += ip[i - 1];
1070             }
1071         }
1072         if (b) {
1073             for (int j = 0; j < ip.length; j++) {
1074                 ip[j] = ip[j] - l;
1075             }
1076         }
1077         return ip;
1078     }
1079  
1080     /**
1081      *
1082      * 根据正则表达式分割字符串
1083      *
1084      * @param str
1085      *            源字符串
1086      * @param ms
1087      *            正则表达式
1088      * @return 目标字符串组
1089      */
1090     public static String[] splitString(String str, String ms) {
1091         String regEx = ms;
1092         Pattern p = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
1093         String[] sp = p.split(str);
1094         return sp;
1095     }
1096  
1097     /**
1098      * *************************************************************************
1099      * 根据正则表达式提取字符串,相同的字符串只返回一个
1100      *
1101      * @author 刘黎明
1102      * @param str
1103      *            源字符串
1104      * @param pattern
1105      *            正则表达式
1106      * @return 目标字符串数据组
1107      *************************************************************************
1108      */
1109  
1110     // ★传入一个字符串,把符合pattern格式的字符串放入字符串数组
1111     // java.util.regex是一个用正则表达式所订制的模式来对字符串进行匹配工作的类库包
1112     public static String[] getStringArrayByPattern(String str, String pattern) {
1113         Pattern p = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
1114         Matcher matcher = p.matcher(str);
1115         // 范型
1116         Set<String> result = new HashSet<String>();// 目的是:相同的字符串只返回一个。。。 不重复元素
1117         // boolean find() 尝试在目标字符串里查找下一个匹配子串。
1118         while (matcher.find()) {
1119             for (int i = 0; i < matcher.groupCount(); i++) { // int groupCount()
1120                                                                 // 返回当前查找所获得的匹配组的数量。
1121                 // System.out.println(matcher.group(i));
1122                 result.add(matcher.group(i));
1123  
1124             }
1125         }
1126         String[] resultStr = null;
1127         if (result.size() > 0) {
1128             resultStr = new String[result.size()];
1129             return result.toArray(resultStr);// 将Set result转化为String[] resultStr
1130         }
1131         return resultStr;
1132  
1133     }
1134  
1135     /**
1136      * 得到第一个b,e之间的字符串,并返回e后的子串
1137      *
1138      * @param s
1139      *            源字符串
1140      * @param b
1141      *            标志开始
1142      * @param e
1143      *            标志结束
1144      * @return b,e之间的字符串
1145      */
1146  
1147     /*
1148      * String aaa="abcdefghijklmn"; String[] bbb=StringProcessor.midString(aaa,
1149      * "b","l"); System.out.println("bbb[0]:"+bbb[0]);//cdefghijk
1150      * System.out.println("bbb[1]:"+bbb[1]);//lmn
1151      * ★这个方法是得到第二个参数和第三个参数之间的字符串,赋给元素0;然后把元素0代表的字符串之后的,赋给元素1
1152      */
1153  
1154     /*
1155      * String aaa="abcdefgllhijklmn5465"; String[]
1156      * bbb=StringProcessor.midString(aaa, "b","l"); //ab cdefg llhijklmn5465 //
1157      * 元素0 元素1
1158      */
1159     public static String[] midString(String s, String b, String e) {
1160         int i = s.indexOf(b) + b.length();
1161         int j = s.indexOf(e, i);
1162         String[] sa = new String[2];
1163         if (i < b.length() || j < i + 1 || i > j) {
1164             sa[1] = s;
1165             sa[0] = null;
1166             return sa;
1167         } else {
1168             sa[0] = s.substring(i, j);
1169             sa[1] = s.substring(j);
1170             return sa;
1171         }
1172     }
1173  
1174     /**
1175      * 带有前一次替代序列的正则表达式替代
1176      *
1177      * @param s
1178      * @param pf
1179      * @param pb
1180      * @param start
1181      * @return
1182      */
1183     public static String stringReplace(String s, String pf, String pb, int start) {
1184         Pattern pattern_hand = Pattern.compile(pf);
1185         Matcher matcher_hand = pattern_hand.matcher(s);
1186         int gc = matcher_hand.groupCount();
1187         int pos = start;
1188         String sf1 = "";
1189         String sf2 = "";
1190         String sf3 = "";
1191         int if1 = 0;
1192         String strr = "";
1193         while (matcher_hand.find(pos)) {
1194             sf1 = matcher_hand.group();
1195             if1 = s.indexOf(sf1, pos);
1196             if (if1 >= pos) {
1197                 strr += s.substring(pos, if1);
1198                 pos = if1 + sf1.length();
1199                 sf2 = pb;
1200                 for (int i = 1; i <= gc; i++) {
1201                     sf3 = "\\" + i;
1202                     sf2 = replaceAll(sf2, sf3, matcher_hand.group(i));
1203                 }
1204                 strr += sf2;
1205             } else {
1206                 return s;
1207             }
1208         }
1209         strr = s.substring(0, start) + strr;
1210         return strr;
1211     }
1212  
1213     /**
1214      * 存文本替换
1215      *
1216      * @param s
1217      *            源字符串
1218      * @param sf
1219      *            子字符串
1220      * @param sb
1221      *            替换字符串
1222      * @return 替换后的字符串
1223      */
1224     public static String replaceAll(String s, String sf, String sb) {
1225         int i = 0, j = 0;
1226         int l = sf.length();
1227         boolean b = true;
1228         boolean o = true;
1229         String str = "";
1230         do {
1231             j = i;
1232             i = s.indexOf(sf, j);
1233             if (i > j) {
1234                 str += s.substring(j, i);
1235                 str += sb;
1236                 i += l;
1237                 o = false;
1238             } else {
1239                 str += s.substring(j);
1240                 b = false;
1241             }
1242         } while (b);
1243         if (o) {
1244             str = s;
1245         }
1246         return str;
1247     }
1248  
1249     /**
1250      * 判断是否与给定字符串样式匹配
1251      *
1252      * @param str
1253      *            字符串
1254      * @param pattern
1255      *            正则表达式样式
1256      * @return 是否匹配是true,否false
1257      */
1258     public static boolean isMatch(String str, String pattern) {
1259         Pattern pattern_hand = Pattern.compile(pattern);
1260         Matcher matcher_hand = pattern_hand.matcher(str);
1261         boolean b = matcher_hand.matches();
1262         return b;
1263     }
1264  
1265     /**
1266      * 截取字符串
1267      *
1268      * @param s
1269      *            源字符串
1270      * @param jmp
1271      *            跳过jmp
1272      * @param sb
1273      *            取在sb
1274      * @param se
1275      *            于se
1276      * @return 之间的字符串
1277      */
1278     public static String subStringExe(String s, String jmp, String sb, String se) {
1279         if (isEmpty(s)) {
1280             return "";
1281         }
1282         int i = s.indexOf(jmp);
1283         if (i >= 0 && i < s.length()) {
1284             s = s.substring(i + 1);
1285         }
1286         i = s.indexOf(sb);
1287         if (i >= 0 && i < s.length()) {
1288             s = s.substring(i + 1);
1289         }
1290         if (se == "") {
1291             return s;
1292         } else {
1293             i = s.indexOf(se);
1294             if (i >= 0 && i < s.length()) {
1295                 s = s.substring(i + 1);
1296             }
1297             return s;
1298         }
1299     }
1300  
1301     /**
1302      * *************************************************************************
1303      * 用要通过URL传输的内容进行编码
1304      *
1305      * @author 刘黎明
1306      * @param 源字符串
1307      * @return 经过编码的内容
1308      *************************************************************************
1309      */
1310     public static String URLEncode(String src) {
1311         String return_value = "";
1312         try {
1313             if (src != null) {
1314                 return_value = URLEncoder.encode(src, "GBK");
1315  
1316             }
1317         } catch (UnsupportedEncodingException e) {
1318             e.printStackTrace();
1319             return_value = src;
1320         }
1321  
1322         return return_value;
1323     }
1324  
1325     /**
1326      * *************************************************************************
1327      *
1328      * @author 李锋 2007.4.18
1329      * @param 传入
1330      *            &#31119;test&#29031;&#27004;&#65288;&#21271;&#22823;&#38376;&#24635
1331      *            ;&#24215;&#65289;&#31119;
1332      * @return 经过解码的内容
1333      *************************************************************************
1334      */
1335     public static String getGBK(String str) {
1336  
1337         return transfer(str);
1338     }
1339  
1340     public static String transfer(String str) {
1341         Pattern p = Pattern.compile("&#\\d+;");
1342         Matcher m = p.matcher(str);
1343         while (m.find()) {
1344             String old = m.group();
1345             str = str.replaceAll(old, getChar(old));
1346         }
1347         return str;
1348     }
1349  
1350     public static String getChar(String str) {
1351         String dest = str.substring(2, str.length() - 1);
1352         char ch = (char) Integer.parseInt(dest);
1353         return "" + ch;
1354     }
1355  
1356     /**
1357      * yahoo首页中切割字符串.
1358      *
1359      * @author yxg
1360      * @date 2007-09-17
1361      * @param str
1362      * @return
1363      */
1364     public static String subYhooString(String subject, int size) {
1365         subject = subject.substring(1, size);
1366         return subject;
1367     }
1368  
1369     public static String subYhooStringDot(String subject, int size) {
1370         subject = subject.substring(1, size) + "...";
1371         return subject;
1372     }
1373  
1374     /**
1375      * 泛型方法(通用),把list转换成以“,”相隔的字符串 调用时注意类型初始化(申明类型) 如:List<Integer> intList =
1376      * new ArrayList<Integer>(); 调用方法:StringUtil.listTtoString(intList);
1377      * 效率:list中4条信息,1000000次调用时间为850ms左右
1378      *
1379      * @author fengliang
1380      * @serialData 2008-01-09
1381      * @param <T>
1382      *            泛型
1383      * @param list
1384      *            list列表
1385      * @return 以“,”相隔的字符串
1386      */
1387     public static <T> String listTtoString(List<T> list) {
1388         if (list == null || list.size() < 1)
1389             return "";
1390         Iterator<T> i = list.iterator();
1391         if (!i.hasNext())
1392             return "";
1393         StringBuilder sb = new StringBuilder();
1394         for (;;) {
1395             T e = i.next();
1396             sb.append(e);
1397             if (!i.hasNext())
1398                 return sb.toString();
1399             sb.append(",");
1400         }
1401     }
1402  
1403     /**
1404      * 把整形数组转换成以“,”相隔的字符串
1405      *
1406      * @author fengliang
1407      * @serialData 2008-01-08
1408      * @param a
1409      *            数组a
1410      * @return 以“,”相隔的字符串
1411      */
1412     public static String intArraytoString(int[] a) {
1413         if (a == null)
1414             return "";
1415         int iMax = a.length - 1;
1416         if (iMax == -1)
1417             return "";
1418         StringBuilder b = new StringBuilder();
1419         for (int i = 0;; i++) {
1420             b.append(a[i]);
1421             if (i == iMax)
1422                 return b.toString();
1423             b.append(",");
1424         }
1425     }
1426  
1427     /**
1428      * 判断文字内容重复
1429      *
1430      * @author 沙枣
1431      * @Date 2008-04-17
1432      */
1433     public static boolean isContentRepeat(String content) {
1434         int similarNum = 0;
1435         int forNum = 0;
1436         int subNum = 0;
1437         int thousandNum = 0;
1438         String startStr = "";
1439         String nextStr = "";
1440         boolean result = false;
1441         float endNum = (float) 0.0;
1442         if (content != null && content.length() > 0) {
1443             if (content.length() % 1000 > 0)
1444                 thousandNum = (int) Math.floor(content.length() / 1000) + 1;
1445             else
1446                 thousandNum = (int) Math.floor(content.length() / 1000);
1447             if (thousandNum < 3)
1448                 subNum = 100 * thousandNum;
1449             else if (thousandNum < 6)
1450                 subNum = 200 * thousandNum;
1451             else if (thousandNum < 9)
1452                 subNum = 300 * thousandNum;
1453             else
1454                 subNum = 3000;
1455             for (int j = 1; j < subNum; j++) {
1456                 if (content.length() % j > 0)
1457                     forNum = (int) Math.floor(content.length() / j) + 1;
1458                 else
1459                     forNum = (int) Math.floor(content.length() / j);
1460                 if (result || j >= content.length())
1461                     break;
1462                 else {
1463                     for (int m = 0; m < forNum; m++) {
1464                         if (m * j > content.length()
1465                                 || (m + 1) * j > content.length()
1466                                 || (m + 2) * j > content.length())
1467                             break;
1468                         startStr = content.substring(m * j, (m + 1) * j);
1469                         nextStr = content.substring((m + 1) * j, (m + 2) * j);
1470                         if (startStr.equals(nextStr)) {
1471                             similarNum = similarNum + 1;
1472                             endNum = (float) similarNum / forNum;
1473                             if (endNum > 0.4) {
1474                                 result = true;
1475                                 break;
1476                             }
1477                         } else
1478                             similarNum = 0;
1479                     }
1480                 }
1481             }
1482         }
1483         return result;
1484     }
1485  
1486     /**
1487      * Ascii转为Char
1488      *
1489      * @author 甜瓜
1490      * @param asc
1491      * @return
1492      */
1493     public static String AsciiToChar(int asc) {
1494         String TempStr = "A";
1495         char tempchar = (char) asc;
1496         TempStr = String.valueOf(tempchar);
1497         return TempStr;
1498     }
1499  
1500     /**
1501      * 判断是否是空字符串 null和"" null返回result,否则返回字符串
1502      *
1503      * @param s
1504      * @return
1505      */
1506     public static String isEmpty(String s, String result) {
1507         if (s != null && !s.equals("")) {
1508             return s;
1509         }
1510         return result;
1511     }
1512  
1513     /**
1514      * 将带有htmlcode代码的字符转换成<>&'"
1515      *
1516      * @param str
1517      * @return
1518      */
1519     public static String htmlcodeToSpecialchars(String str) {
1520         str = str.replaceAll("&amp;", "&");
1521         str = str.replaceAll("&quot;", "\"");
1522         str = str.replaceAll("&#039;", "'");
1523         str = str.replaceAll("&lt;", "<");
1524         str = str.replaceAll("&gt;", ">");
1525         return str;
1526     }
1527  
1528     /**
1529      * 移除html标签
1530      *
1531      * @param htmlstr
1532      * @return
1533      */
1534     public static String removeHtmlTag(String htmlstr) {
1535         Pattern pat = Pattern.compile("\\s*<.*?>\\s*", Pattern.DOTALL
1536                 | Pattern.MULTILINE | Pattern.CASE_INSENSITIVE); // \\s?[s|Sc|Cr|Ri|Ip|Pt|T]
1537         Matcher m = pat.matcher(htmlstr);
1538         String rs = m.replaceAll("");
1539         rs = rs.replaceAll("&nbsp", " ");
1540         rs = rs.replaceAll("&lt;", "<");
1541         rs = rs.replaceAll("&gt;", ">");
1542         return rs;
1543     }
1544  
1545     /**
1546      * 取从指定搜索项开始的字符串,返回的值不包含搜索项
1547      *
1548      * @param captions
1549      *            例如:"www.koubei.com"
1550      * @param regex
1551      *            分隔符,例如"."
1552      * @return 结果字符串,如:koubei.com,如未找到返回空串
1553      */
1554     public static String getStrAfterRegex(String captions, String regex) {
1555         if (!isEmpty(captions) && !isEmpty(regex)) {
1556             int pos = captions.indexOf(regex);
1557             if (pos != -1 && pos < captions.length() - 1) {
1558                 return captions.substring(pos + 1);
1559             }
1560         }
1561         return "";
1562     }
1563  
1564     /**
1565      * 把字节码转换成16进制
1566      */
1567     public static String byte2hex(byte bytes[]) {
1568         StringBuffer retString = new StringBuffer();
1569         for (int i = 0; i < bytes.length; ++i) {
1570             retString.append(Integer.toHexString(0x0100 + (bytes[i] & 0x00FF))
1571                     .substring(1).toUpperCase());
1572         }
1573         return retString.toString();
1574     }
1575  
1576     /**
1577      * 把16进制转换成字节码
1578      *
1579      * @param hex
1580      * @return
1581      */
1582     public static byte[] hex2byte(String hex) {
1583         byte[] bts = new byte[hex.length() / 2];
1584         for (int i = 0; i < bts.length; i++) {
1585             bts[i] = (byte) Integer.parseInt(hex.substring(2 * i, 2 * i + 2),
1586                     16);
1587         }
1588         return bts;
1589     }
1590  
1591     /**
1592      * 转换数字为固定长度的字符串
1593      *
1594      * @param length
1595      *            希望返回的字符串长度
1596      * @param data
1597      *            传入的数值
1598      * @return
1599      */
1600     public static String getStringByInt(int length, String data) {
1601         String s_data = "";
1602         int datalength = data.length();
1603         if (length > 0 && length >= datalength) {
1604             for (int i = 0; i < length - datalength; i++) {
1605                 s_data += "0";
1606             }
1607             s_data += data;
1608         }
1609  
1610         return s_data;
1611     }
1612  
1613     /**
1614      * 判断是否位数字,并可为空
1615      *
1616      * @param src
1617      * @return
1618      */
1619     public static boolean isNumericAndCanNull(String src) {
1620         Pattern numericPattern = Pattern.compile("^[0-9]+$");
1621         if (src == null || src.equals(""))
1622             return true;
1623         boolean return_value = false;
1624         if (src != null && src.length() > 0) {
1625             Matcher m = numericPattern.matcher(src);
1626             if (m.find()) {
1627                 return_value = true;
1628             }
1629         }
1630         return return_value;
1631     }
1632  
1633     /**
1634      *
1635      * @param src
1636      * @return
1637      */
1638     public static boolean isFloatAndCanNull(String src) {
1639         Pattern numericPattern = Pattern
1640                 .compile("^(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*))$");
1641         if (src == null || src.equals(""))
1642             return true;
1643         boolean return_value = false;
1644         if (src != null && src.length() > 0) {
1645             Matcher m = numericPattern.matcher(src);
1646             if (m.find()) {
1647                 return_value = true;
1648             }
1649         }
1650         return return_value;
1651     }
1652  
1653     public static boolean isNotEmpty(String str) {
1654         if (str != null && !str.equals(""))
1655             return true;
1656         else
1657             return false;
1658     }
1659  
1660     public static boolean isDate(String date) {
1661         String regEx = "\\d{4}-\\d{2}-\\d{2}\\s\\d{2}:\\d{2}:\\d{2}";
1662         Pattern p = Pattern.compile(regEx);
1663         Matcher m = p.matcher(date);
1664         boolean result = m.find();
1665         return result;
1666     }
1667  
1668     public static boolean isFormatDate(String date, String regEx) {
1669         Pattern p = Pattern.compile(regEx);
1670         Matcher m = p.matcher(date);
1671         boolean result = m.find();
1672         return result;
1673     }
1674  
1675     /**
1676      * 根据指定整型list 组装成为一个字符串
1677      *
1678      * @param src
1679      * @return
1680      */
1681     public static String listToString(List<Integer> list) {
1682         String str = "";
1683         if (list != null && list.size() > 0) {
1684             for (int id : list) {
1685                 str = str + id + ",";
1686             }
1687             if (!"".equals(str) && str.length() > 0)
1688                 str = str.substring(0, str.length() - 1);
1689         }
1690         return str;
1691     }
1692  
1693     /**
1694      * 页面的非法字符检查
1695      *
1696      * @author shazao
1697      * @date 2007-11-29
1698      * @param str
1699      * @return
1700      */
1701     public static String replaceStr(String str) {
1702         if (str != null && str.length() > 0) {
1703             str = str.replaceAll("~", ",");
1704             str = str.replaceAll(" ", ",");
1705             str = str.replaceAll(" ", ",");
1706             str = str.replaceAll(" ", ",");
1707             str = str.replaceAll("`", ",");
1708             str = str.replaceAll("!", ",");
1709             str = str.replaceAll("@", ",");
1710             str = str.replaceAll("#", ",");
1711             str = str.replaceAll("\\$", ",");
1712             str = str.replaceAll("%", ",");
1713             str = str.replaceAll("\\^", ",");
1714             str = str.replaceAll("&", ",");
1715             str = str.replaceAll("\\*", ",");
1716             str = str.replaceAll("\\(", ",");
1717             str = str.replaceAll("\\)", ",");
1718             str = str.replaceAll("-", ",");
1719             str = str.replaceAll("_", ",");
1720             str = str.replaceAll("=", ",");
1721             str = str.replaceAll("\\+", ",");
1722             str = str.replaceAll("\\{", ",");
1723             str = str.replaceAll("\\[", ",");
1724             str = str.replaceAll("\\}", ",");
1725             str = str.replaceAll("\\]", ",");
1726             str = str.replaceAll("\\|", ",");
1727             str = str.replaceAll("\\\\", ",");
1728             str = str.replaceAll(";", ",");
1729             str = str.replaceAll(":", ",");
1730             str = str.replaceAll("'", ",");
1731             str = str.replaceAll("\\\"", ",");
1732             str = str.replaceAll("<", ",");
1733             str = str.replaceAll(">", ",");
1734             str = str.replaceAll("\\.", ",");
1735             str = str.replaceAll("\\?", ",");
1736             str = str.replaceAll("/", ",");
1737             str = str.replaceAll("~", ",");
1738             str = str.replaceAll("`", ",");
1739             str = str.replaceAll("!", ",");
1740             str = str.replaceAll("@", ",");
1741             str = str.replaceAll("#", ",");
1742             str = str.replaceAll("$", ",");
1743             str = str.replaceAll("%", ",");
1744             str = str.replaceAll("︿", ",");
1745             str = str.replaceAll("&", ",");
1746             str = str.replaceAll("×", ",");
1747             str = str.replaceAll("(", ",");
1748             str = str.replaceAll(")", ",");
1749             str = str.replaceAll("-", ",");
1750             str = str.replaceAll("_", ",");
1751             str = str.replaceAll("+", ",");
1752             str = str.replaceAll("=", ",");
1753             str = str.replaceAll("{", ",");
1754             str = str.replaceAll("[", ",");
1755             str = str.replaceAll("}", ",");
1756             str = str.replaceAll("]", ",");
1757             str = str.replaceAll("|", ",");
1758             str = str.replaceAll("\", ",");
1759             str = str.replaceAll(":", ",");
1760             str = str.replaceAll(";", ",");
1761             str = str.replaceAll(""", ",");
1762             str = str.replaceAll("'", ",");
1763             str = str.replaceAll("<", ",");
1764             str = str.replaceAll(",", ",");
1765             str = str.replaceAll(">", ",");
1766             str = str.replaceAll(".", ",");
1767             str = str.replaceAll("?", ",");
1768             str = str.replaceAll("/", ",");
1769             str = str.replaceAll("·", ",");
1770             str = str.replaceAll("¥", ",");
1771             str = str.replaceAll("……", ",");
1772             str = str.replaceAll("(", ",");
1773             str = str.replaceAll(")", ",");
1774             str = str.replaceAll("——", ",");
1775             str = str.replaceAll("-", ",");
1776             str = str.replaceAll("【", ",");
1777             str = str.replaceAll("】", ",");
1778             str = str.replaceAll("、", ",");
1779             str = str.replaceAll("”", ",");
1780             str = str.replaceAll("’", ",");
1781             str = str.replaceAll("《", ",");
1782             str = str.replaceAll("》", ",");
1783             str = str.replaceAll("“", ",");
1784             str = str.replaceAll("。", ",");
1785         }
1786         return str;
1787     }
1788  
1789     /**
1790      * 全角字符变半角字符
1791      *
1792      * @author shazao
1793      * @date 2008-04-03
1794      * @param str
1795      * @return
1796      */
1797     public static String full2Half(String str) {
1798         if (str == null || "".equals(str))
1799             return "";
1800         StringBuffer sb = new StringBuffer();
1801  
1802         for (int i = 0; i < str.length(); i++) {
1803             char c = str.charAt(i);
1804  
1805             if (c >= 65281 && c < 65373)
1806                 sb.append((char) (c - 65248));
1807             else
1808                 sb.append(str.charAt(i));
1809         }
1810  
1811         return sb.toString();
1812  
1813     }
1814  
1815     /**
1816      * 全角括号转为半角
1817      *
1818      * @author shazao
1819      * @date 2007-11-29
1820      * @param str
1821      * @return
1822      */
1823     public static String replaceBracketStr(String str) {
1824         if (str != null && str.length() > 0) {
1825             str = str.replaceAll("(", "(");
1826             str = str.replaceAll(")", ")");
1827         }
1828         return str;
1829     }
1830  
1831     /**
1832      * 分割字符,从开始到第一个split字符串为止
1833      *
1834      * @param src
1835      *            源字符串
1836      * @param split
1837      *            截止字符串
1838      * @return
1839      */
1840     public static String subStr(String src, String split) {
1841         if (!isEmpty(src)) {
1842             int index = src.indexOf(split);
1843             if (index >= 0) {
1844                 return src.substring(0, index);
1845             }
1846         }
1847         return src;
1848     }
1849  
1850     /**
1851      * 取url里的keyword(可选择参数)参数,用于整站搜索整合
1852      *
1853      * @author huoshao
1854      * @param params
1855      * @param qString
1856      * @return
1857      */
1858     public static String getKeyWord(String params, String qString) {
1859         String keyWord = "";
1860         if (qString != null) {
1861             String param = params + "=";
1862             int i = qString.indexOf(param);
1863             if (i != -1) {
1864                 int j = qString.indexOf("&", i + param.length());
1865                 if (j > 0) {
1866                     keyWord = qString.substring(i + param.length(), j);
1867                 }
1868             }
1869         }
1870         return keyWord;
1871     }
1872  
1873     /**
1874      * 解析字符串返回map键值对(例:a=1&b=2 => a=1,b=2)
1875      *
1876      * @param query
1877      *            源参数字符串
1878      * @param split1
1879      *            键值对之间的分隔符(例:&)
1880      * @param split2
1881      *            key与value之间的分隔符(例:=)
1882      * @param dupLink
1883      *            重复参数名的参数值之间的连接符,连接后的字符串作为该参数的参数值,可为null
1884      *            null:不允许重复参数名出现,则靠后的参数值会覆盖掉靠前的参数值。
1885      * @return map
1886      * @author sky
1887      */
1888     @SuppressWarnings("unchecked")
1889     public static Map<String, String> parseQuery(String query, char split1,
1890             char split2, String dupLink) {
1891         if (!isEmpty(query) && query.indexOf(split2) > 0) {
1892             Map<String, String> result = new HashMap();
1893  
1894             String name = null;
1895             String value = null;
1896             String tempValue = "";
1897             int len = query.length();
1898             for (int i = 0; i < len; i++) {
1899                 char c = query.charAt(i);
1900                 if (c == split2) {
1901                     value = "";
1902                 } else if (c == split1) {
1903                     if (!isEmpty(name) && value != null) {
1904                         if (dupLink != null) {
1905                             tempValue = result.get(name);
1906                             if (tempValue != null) {
1907                                 value += dupLink + tempValue;
1908                             }
1909                         }
1910                         result.put(name, value);
1911                     }
1912                     name = null;
1913                     value = null;
1914                 } else if (value != null) {
1915                     value += c;
1916                 } else {
1917                     name = (name != null) ? (name + c) : "" + c;
1918                 }
1919             }
1920  
1921             if (!isEmpty(name) && value != null) {
1922                 if (dupLink != null) {
1923                     tempValue = result.get(name);
1924                     if (tempValue != null) {
1925                         value += dupLink + tempValue;
1926                     }
1927                 }
1928                 result.put(name, value);
1929             }
1930  
1931             return result;
1932         }
1933         return null;
1934     }
1935  
1936     /**
1937      * 将list 用传入的分隔符组装为String
1938      *
1939      * @param list
1940      * @param slipStr
1941      * @return String
1942      */
1943     @SuppressWarnings("unchecked")
1944     public static String listToStringSlipStr(List list, String slipStr) {
1945         StringBuffer returnStr = new StringBuffer();
1946         if (list != null && list.size() > 0) {
1947             for (int i = 0; i < list.size(); i++) {
1948                 returnStr.append(list.get(i)).append(slipStr);
1949             }
1950         }
1951         if (returnStr.toString().length() > 0)
1952             return returnStr.toString().substring(0,
1953                     returnStr.toString().lastIndexOf(slipStr));
1954         else
1955             return "";
1956     }
1957  
1958     /**
1959      * 获取从start开始用*替换len个长度后的字符串
1960      *
1961      * @param str
1962      *            要替换的字符串
1963      * @param start
1964      *            开始位置
1965      * @param len
1966      *            长度
1967      * @return 替换后的字符串
1968      */
1969     public static String getMaskStr(String str, int start, int len) {
1970         if (StringUtil.isEmpty(str)) {
1971             return str;
1972         }
1973         if (str.length() < start) {
1974             return str;
1975         }
1976  
1977         // 获取*之前的字符串
1978         String ret = str.substring(0, start);
1979  
1980         // 获取最多能打的*个数
1981         int strLen = str.length();
1982         if (strLen < start + len) {
1983             len = strLen - start;
1984         }
1985  
1986         // 替换成*
1987         for (int i = 0; i < len; i++) {
1988             ret += "*";
1989         }
1990  
1991         // 加上*之后的字符串
1992         if (strLen > start + len) {
1993             ret += str.substring(start + len);
1994         }
1995  
1996         return ret;
1997     }
1998  
1999     /**
2000      * 根据传入的分割符号,把传入的字符串分割为List字符串
2001      *
2002      * @param slipStr
2003      *            分隔的字符串
2004      * @param src
2005      *            字符串
2006      * @return 列表
2007      */
2008     public static List<String> stringToStringListBySlipStr(String slipStr,
2009             String src) {
2010  
2011         if (src == null)
2012             return null;
2013         List<String> list = new ArrayList<String>();
2014         String[] result = src.split(slipStr);
2015         for (int i = 0; i < result.length; i++) {
2016             list.add(result[i]);
2017         }
2018         return list;
2019     }
2020  
2021     /**
2022      * 截取字符串
2023      *
2024      * @param str
2025      *            原始字符串
2026      * @param len
2027      *            要截取的长度
2028      * @param tail
2029      *            结束加上的后缀
2030      * @return 截取后的字符串
2031      */
2032     public static String getHtmlSubString(String str, int len, String tail) {
2033         if (str == null || str.length() <= len) {
2034             return str;
2035         }
2036         int length = str.length();
2037         char c = ' ';
2038         String tag = null;
2039         String name = null;
2040         int size = 0;
2041         String result = "";
2042         boolean isTag = false;
2043         List<String> tags = new ArrayList<String>();
2044         int i = 0;
2045         for (int end = 0, spanEnd = 0; i < length && len > 0; i++) {
2046             c = str.charAt(i);
2047             if (c == '<') {
2048                 end = str.indexOf('>', i);
2049             }
2050  
2051             if (end > 0) {
2052                 // 截取标签
2053                 tag = str.substring(i, end + 1);
2054                 int n = tag.length();
2055                 if (tag.endsWith("/>")) {
2056                     isTag = true;
2057                 } else if (tag.startsWith("</")) { // 结束符
2058                     name = tag.substring(2, end - i);
2059                     size = tags.size() - 1;
2060                     // 堆栈取出html开始标签
2061                     if (size >= 0 && name.equals(tags.get(size))) {
2062                         isTag = true;
2063                         tags.remove(size);
2064                     }
2065                 } else { // 开始符
2066                     spanEnd = tag.indexOf(' ', 0);
2067                     spanEnd = spanEnd > 0 ? spanEnd : n;
2068                     name = tag.substring(1, spanEnd);
2069                     if (name.trim().length() > 0) {
2070                         // 如果有结束符则为html标签
2071                         spanEnd = str.indexOf("</" + name + ">", end);
2072                         if (spanEnd > 0) {
2073                             isTag = true;
2074                             tags.add(name);
2075                         }
2076                     }
2077                 }
2078                 // 非html标签字符
2079                 if (!isTag) {
2080                     if (n >= len) {
2081                         result += tag.substring(0, len);
2082                         break;
2083                     } else {
2084                         len -= n;
2085                     }
2086                 }
2087  
2088                 result += tag;
2089                 isTag = false;
2090                 i = end;
2091                 end = 0;
2092             } else { // 非html标签字符
2093                 len--;
2094                 result += c;
2095             }
2096         }
2097         // 添加未结束的html标签
2098         for (String endTag : tags) {
2099             result += "</" + endTag + ">";
2100         }
2101         if (i < length) {
2102             result += tail;
2103         }
2104         return result;
2105     }
2106  
2107 }// /:~