Java 字符串操作封装

Java 对常用字符串操作的封装


[java]  view plain  copy
  1. package com.wiker;  
  2.   
  3. import java.beans.XMLDecoder;  
  4. import java.io.BufferedInputStream;  
  5. import java.io.ByteArrayInputStream;  
  6. import java.io.UnsupportedEncodingException;  
  7. import java.net.URLEncoder;  
  8. import java.text.DecimalFormat;  
  9. import java.util.ArrayList;  
  10. import java.util.HashMap;  
  11. import java.util.HashSet;  
  12. import java.util.Iterator;  
  13. import java.util.LinkedHashMap;  
  14. import java.util.List;  
  15. import java.util.Map;  
  16. import java.util.Set;  
  17. import java.util.regex.Matcher;  
  18. import java.util.regex.Pattern;  
  19.   
  20. import org.apache.commons.lang.RandomStringUtils;  
  21.   
  22. import com.opensymphony.util.TextUtils;  
  23.   
  24.   
  25. /** 
  26.  * TODO: DOCUMENT ME! 
  27.  *  
  28.  * @author Wiker 
  29.  */  
  30. public class StringUtil {  
  31.     private static Pattern numericPattern = Pattern.compile("^[0-9\\-]+$");  
  32.     private static Pattern integerPattern = Pattern.compile("^[0-9]+$");  
  33.     private static Pattern numericStringPattern = Pattern.compile("^[0-9\\-\\-]+$");  
  34.     private static Pattern floatNumericPattern = Pattern.compile("^[0-9\\.]+$");  
  35.     private static Pattern abcPattern = Pattern.compile("^[a-z|A-Z]+$");  
  36.     public static final String splitStrPattern = ",|,|;|;|、|\\.|。|-|_|\\(|\\)|\\[|\\]|\\{|\\}|\\\\|/| | |\"";  
  37.       
  38.     /** 
  39.      * 判断是否数字表示 
  40.      *  
  41.      * @param src 
  42.      *            源字符串 
  43.      * @return 是否数字的标志 
  44.      */  
  45.     public static boolean isNumeric(String src) {  
  46.         boolean return_value = false;  
  47.         if (src != null && src.length() > 0) {  
  48.             Matcher m = numericPattern.matcher(src);  
  49.             if (m.find()) {  
  50.                 return_value = true;  
  51.             }  
  52.         }  
  53.         return return_value;  
  54.     }  
  55.       
  56.     /** 
  57.      *  
  58.      * 是否是数字。整数,没有任何符号 
  59.      * 
  60.      * @param src 
  61.      * @return 
  62.      */  
  63.     public static boolean isInteger(String src){  
  64.         boolean return_value = false;  
  65.         if (src != null && src.length() > 0) {  
  66.             Matcher m = integerPattern.matcher(src);  
  67.             if (m.find()) {  
  68.                 return_value = true;  
  69.             }  
  70.         }  
  71.         return return_value;  
  72.     }  
  73.       
  74.     /** 
  75.      * 判断是否数字 
  76.      *  
  77.      * @param src 
  78.      *            源字符串 
  79.      * @return 是否数字的标志 
  80.      */  
  81.     public static boolean isNumericString(String src) {  
  82.         boolean return_value = false;  
  83.         if (src != null && src.length() > 0) {  
  84.             Matcher m = numericStringPattern.matcher(src);  
  85.             if (m.find()) {  
  86.                 return_value = true;  
  87.             }  
  88.         }  
  89.         return return_value;  
  90.     }  
  91.       
  92.     /** 
  93.      * 判断是否纯字母组合 
  94.      *  
  95.      * @param src 
  96.      *            源字符串 
  97.      * @return 是否纯字母组合的标志 
  98.      */  
  99.     public static boolean isABC(String src) {  
  100.         boolean return_value = false;  
  101.         if (src != null && src.length() > 0) {  
  102.             Matcher m = abcPattern.matcher(src);  
  103.             if (m.find()) {  
  104.                 return_value = true;  
  105.             }  
  106.         }  
  107.         return return_value;  
  108.     }  
  109.       
  110.     /** 
  111.      * 判断是否浮点数字表示 
  112.      *  
  113.      * @param src 
  114.      *            源字符串 
  115.      * @return 是否数字的标志 
  116.      */  
  117.     public static boolean isFloatNumeric(String src) {  
  118.         boolean return_value = false;  
  119.         if (src != null && src.length() > 0) {  
  120.             Matcher m = floatNumericPattern.matcher(src);  
  121.             if (m.find()) {  
  122.                 return_value = true;  
  123.             }  
  124.         }  
  125.         return return_value;  
  126.     }  
  127.       
  128.     /** 
  129.      * 把string array or list用给定的符号symbol连接成一个字符串 
  130.      *  
  131.      * @param array 
  132.      * @param symbol 
  133.      * @return 
  134.      */  
  135.     public static String joinString(List array, String symbol) {  
  136.         String result = "";  
  137.         if (array != null) {  
  138.             for (int i = 0; i < array.size(); i++) {  
  139.                 String temp = array.get(i).toString();  
  140.                 if (temp != null && temp.trim().length() > 0)  
  141.                     result += (temp + symbol);  
  142.             }  
  143.             if (result.length() > 1)  
  144.                 result = result.substring(0, result.length() - 1);  
  145.         }  
  146.         return result;  
  147.     }  
  148.       
  149.     /** 
  150.      * 截取字符串 
  151.      *  
  152.      * @param subject 
  153.      * @param size 
  154.      * @return 
  155.      */  
  156.     public static String subStringNotEncode(String subject, int size) {  
  157.         if (subject != null && subject.length() > size) {  
  158.             subject = subject.substring(0, size) + "...";  
  159.         }  
  160.         return subject;  
  161.     }  
  162.       
  163.     public static String subString(String subject, int size) {  
  164.         subject = TextUtils.htmlEncode(subject);  
  165.         if (subject.length() > size) {  
  166.             subject = subject.substring(0, size) + "...";  
  167.         }  
  168.         return subject;  
  169.     }  
  170.       
  171.     /** 
  172.      * 截取字符串 超出的字符用symbol代替    
  173.      *  
  174.      * @param len 
  175.      *             字符串长度 长度计量单位为一个GBK汉字  两个英文字母计算为一个单位长度 
  176.      * @param str 
  177.      * @param symbol 
  178.      * @return 
  179.      */  
  180.     public static String getLimitLengthString(String str, int len, String symbol) {  
  181.         int iLen = len * 2;  
  182.         int counterOfDoubleByte = 0;  
  183.         String strRet = "";  
  184.         try {  
  185.             if (str != null) {  
  186.                 byte[] b = str.getBytes("GBK");  
  187.                 if (b.length <= iLen) {  
  188.                     return str;  
  189.                 }  
  190.                 for (int i = 0; i < iLen; i++) {  
  191.                     if (b[i] < 0) {  
  192.                         counterOfDoubleByte++;  
  193.                     }  
  194.                 }  
  195.                 if (counterOfDoubleByte % 2 == 0) {  
  196.                     strRet = new String(b, 0, iLen, "GBK") + symbol;  
  197.                     return strRet;  
  198.                 } else {  
  199.                     strRet = new String(b, 0, iLen - 1"GBK") + symbol;  
  200.                     return strRet;  
  201.                 }  
  202.             } else {  
  203.                 return "";  
  204.             }  
  205.         } catch (Exception ex) {  
  206.             return str.substring(0, len);  
  207.         } finally {  
  208.             strRet = null;  
  209.         }  
  210.     }  
  211.       
  212.     /** 
  213.      * 截取字符串 超出的字符用symbol代替    
  214.      *  
  215.      * @param len 
  216.      *             字符串长度 长度计量单位为一个GBK汉字  两个英文字母计算为一个单位长度 
  217.      * @param str 
  218.      * @param symbol 
  219.      * @return12 
  220.      */  
  221.     public static String getLimitLengthString(String str, int len) {  
  222.         return getLimitLengthString(str, len, "...");  
  223.     }  
  224.       
  225.     public static String subStr(String subject, int size) {  
  226.         subject = TextUtils.htmlEncode(subject);  
  227.         if (subject.length() > size) {  
  228.             subject = subject.substring(0, size);  
  229.         }  
  230.         return subject;  
  231.     }  
  232.       
  233.     /** 
  234.      * 截取字符,不转码 
  235.      *  
  236.      * @param subject 
  237.      * @param size 
  238.      * @return 
  239.      */  
  240.     public static String subStrNotEncode(String subject, int size) {  
  241.         if (subject.length() > size) {  
  242.             subject = subject.substring(0, size);  
  243.         }  
  244.         return subject;  
  245.     }  
  246.       
  247.     /** 
  248.      * 把string array or list用给定的符号symbol连接成一个字符串 
  249.      *  
  250.      * @param array 
  251.      * @param symbol 
  252.      * @return 
  253.      */  
  254.     public static String joinString(String[] array, String symbol) {  
  255.         String result = "";  
  256.         if (array != null) {  
  257.             for (int i = 0; i < array.length; i++) {  
  258.                 String temp = array[i];  
  259.                 if (temp != null && temp.trim().length() > 0)  
  260.                     result += (temp + symbol);  
  261.             }  
  262.             if (result.length() > 1)  
  263.                 result = result.substring(0, result.length() - 1);  
  264.         }  
  265.         return result;  
  266.     }  
  267.       
  268.     /** 
  269.      * 取得字符串的实际长度(考虑了汉字的情况) 
  270.      *  
  271.      * @param SrcStr 
  272.      *            源字符串 
  273.      * @return 字符串的实际长度 
  274.      */  
  275.     public static int getStringLen(String SrcStr) {  
  276.         int return_value = 0;  
  277.         if (SrcStr != null) {  
  278.             char[] theChars = SrcStr.toCharArray();  
  279.             for (int i = 0; i < theChars.length; i++) {  
  280.                 return_value += (theChars[i] <= 255) ? 1 : 2;  
  281.             }  
  282.         }  
  283.         return return_value;  
  284.     }  
  285.       
  286.     /** 
  287.      * 检查联系人信息是否填写,电话,手机,email必须填至少一个,email填了的话检查格式 
  288.      *  
  289.      * @param phoneCity 
  290.      * @param phoneNumber 
  291.      * @param phoneExt 
  292.      * @param mobileNumber 
  293.      * @param email 
  294.      * @return 
  295.      */  
  296.     public static boolean checkContactInfo(String phoneCity, String phoneNumber, String phoneExt,  
  297.             String mobileNumber, String email) {  
  298.         String result = (phoneCity == null ? "" : phoneCity.trim())  
  299.                 + (phoneNumber == null ? "" : phoneNumber.trim())  
  300.                 + (phoneExt == null ? "" : phoneExt.trim())  
  301.                 + (mobileNumber == null ? "" : mobileNumber.trim())  
  302.                 + (email == null ? "" : email.trim());  
  303.         if (result.length() < 1)  
  304.             return false;  
  305.         if (!isEmail(email))  
  306.             return false;  
  307.           
  308.         return true;  
  309.     }  
  310.       
  311.     /** 
  312.      * 检查数据串中是否包含非法字符集 
  313.      *  
  314.      * @param str 
  315.      * @return [true]|[false] 包含|不包含 
  316.      */  
  317.     public static boolean check(String str) {  
  318.         String sIllegal = "'\"";  
  319.         int len = sIllegal.length();  
  320.         if (null == str)  
  321.             return false;  
  322.         for (int i = 0; i < len; i++) {  
  323.             if (str.indexOf(sIllegal.charAt(i)) != -1)  
  324.                 return true;  
  325.         }  
  326.           
  327.         return false;  
  328.     }  
  329.       
  330.     /** 
  331.      * getHideEmailPrefix - 隐藏邮件地址前缀。 
  332.      *  
  333.      * @param email 
  334.      *            - EMail邮箱地址 例如: linwenguo@koubei.com 等等... 
  335.      * @return 返回已隐藏前缀邮件地址, 如 *********@koubei.com. 
  336.      * @version 1.0 (2006.11.27) Wilson Lin 
  337.      */  
  338.     public static String getHideEmailPrefix(String email) {  
  339.         if (null != email) {  
  340.             int index = email.lastIndexOf('@');  
  341.             if (index > 0) {  
  342.                 email = repeat("*", index).concat(email.substring(index));  
  343.             }  
  344.         }  
  345.         return email;  
  346.     }  
  347.       
  348.     /** 
  349.      * repeat - 通过源字符串重复生成N次组成新的字符串。 
  350.      *  
  351.      * @param src 
  352.      *            - 源字符串 例如: 空格(" "), 星号("*"), "浙江" 等等... 
  353.      * @param num 
  354.      *            - 重复生成次数 
  355.      * @return 返回已生成的重复字符串 
  356.      * @version 1.0 (2006.10.10) Wilson Lin 
  357.      */  
  358.     public static String repeat(String src, int num) {  
  359.         StringBuffer s = new StringBuffer();  
  360.         for (int i = 0; i < num; i++)  
  361.             s.append(src);  
  362.         return s.toString();  
  363.     }  
  364.       
  365.     /** 
  366.      * 检查是否是email, when null or '' return true 
  367.      *  
  368.      * @param email 
  369.      * @return 
  370.      */  
  371.     public static boolean isEmail(String email) {  
  372.         if (email != null && email.trim().length() > 0) {  
  373.             if (!TextUtils.verifyEmail(email)) {  
  374.                 return false;  
  375.             } else {  
  376.                 return true;  
  377.             }  
  378.         }  
  379.         return false;  
  380.     }  
  381.       
  382.     /** 
  383.      * 根据指定的字符把源字符串分割成一个数组 
  384.      *  
  385.      * @param src 
  386.      * @return 
  387.      */  
  388.     public static List<String> parseString2ListByCustomerPattern(String pattern, String src) {  
  389.           
  390.         if (src == null)  
  391.             return null;  
  392.         List<String> list = new ArrayList<String>();  
  393.         String[] result = src.split(pattern);  
  394.         for (int i = 0; i < result.length; i++) {  
  395.             list.add(result[i]);  
  396.         }  
  397.         return list;  
  398.     }  
  399.       
  400.     /** 
  401.      * 根据指定的字符把源字符串分割成一个数组 
  402.      *  
  403.      * @param src 
  404.      * @return 
  405.      */  
  406.     public static List<String> parseString2ListByPattern(String src) {  
  407.         String pattern = ",|,|、|。";  
  408.         return parseString2ListByCustomerPattern(pattern, src);  
  409.     }  
  410.       
  411.     /** 
  412.      * 格式化一个float 
  413.      *  
  414.      * @param format 
  415.      *            要格式化成的格式 such as #.00, #.# 
  416.      */  
  417.       
  418.     public static String formatFloat(float f, String format) {  
  419.         DecimalFormat df = new DecimalFormat(format);  
  420.         return df.format(f);  
  421.     }  
  422.       
  423.     /** 
  424.      * 判断是否是空字符串 null和"" 都返回 true 
  425.      *  
  426.      * @param s 
  427.      * @return 
  428.      */  
  429.     public static boolean isEmpty(String s) {  
  430.         if (s != null && !s.equals("")) {  
  431.             return false;  
  432.         }  
  433.         return true;  
  434.     }  
  435.       
  436.     /** 
  437.      * 自定义的分隔字符串函数 例如: 1,2,3 =>[1,2,3] 3个元素 ,2,3=>[,2,3] 3个元素 ,2,3,=>[,2,3,] 
  438.      * 4个元素 ,,,=>[,,,] 4个元素 
  439.      * 5.22算法修改,为提高速度不用正则表达式 两个间隔符,,返回""元素 
  440.      *  
  441.      * @param split 
  442.      *            分割字符 默认, 
  443.      * @param src 
  444.      *            输入字符串 
  445.      * @return 分隔后的list 
  446.      */  
  447.     public static List<String> splitToList(String split, String src) {  
  448.         // 默认,  
  449.         String sp = ",";  
  450.         if (split != null && split.length() == 1) {  
  451.             sp = split;  
  452.         }  
  453.         List<String> r = new ArrayList<String>();  
  454.         int lastIndex = -1;  
  455.         int index = src.indexOf(sp);  
  456.         if (-1 == index && src != null) {  
  457.             r.add(src);  
  458.             return r;  
  459.         }  
  460.         while (index >= 0) {  
  461.             if (index > lastIndex) {  
  462.                 r.add(src.substring(lastIndex + 1, index));  
  463.             } else {  
  464.                 r.add("");  
  465.             }  
  466.               
  467.             lastIndex = index;  
  468.             index = src.indexOf(sp, index + 1);  
  469.             if (index == -1) {  
  470.                 r.add(src.substring(lastIndex + 1, src.length()));  
  471.             }  
  472.         }  
  473.         return r;  
  474.     }  
  475.       
  476.     /** 
  477.      * 把 名=值 参数表转换成字符串 (a=1,b=2 =>a=1&b=2) 
  478.      *  
  479.      * @param map 
  480.      * @return 
  481.      */  
  482.     public static String linkedHashMapToString(LinkedHashMap<String, String> map) {  
  483.         if (map != null && map.size() > 0) {  
  484.             String result = "";  
  485.             Iterator it = map.keySet().iterator();  
  486.             while (it.hasNext()) {  
  487.                 String name = (String) it.next();  
  488.                 String value = (String) map.get(name);  
  489.                 result += (result.equals("")) ? "" : "&";  
  490.                 result += String.format("%s=%s", name, value);  
  491.             }  
  492.             return result;  
  493.         }  
  494.         return null;  
  495.     }  
  496.       
  497.     /** 
  498.      * 解析字符串返回 名称=值的参数表 (a=1&b=2 => a=1,b=2) 
  499.      *  
  500.      * @see test.koubei.util.StringUtilTest#testParseStr() 
  501.      * @param str 
  502.      * @return 
  503.      */  
  504.     @SuppressWarnings("unchecked")  
  505.     public static LinkedHashMap<String, String> toLinkedHashMap(String str) {  
  506.         if (str != null && !str.equals("") && str.indexOf("=") > 0) {  
  507.             LinkedHashMap result = new LinkedHashMap();  
  508.               
  509.             String name = null;  
  510.             String value = null;  
  511.             int i = 0;  
  512.             while (i < str.length()) {  
  513.                 char c = str.charAt(i);  
  514.                 switch (c) {  
  515.                     case 61// =  
  516.                         value = "";  
  517.                         break;  
  518.                     case 38// &  
  519.                         if (name != null && value != null && !name.equals("")) {  
  520.                             result.put(name, value);  
  521.                         }  
  522.                         name = null;  
  523.                         value = null;  
  524.                         break;  
  525.                     default:  
  526.                         if (value != null) {  
  527.                             value = (value != null) ? (value + c) : "" + c;  
  528.                         } else {  
  529.                             name = (name != null) ? (name + c) : "" + c;  
  530.                         }  
  531.                 }  
  532.                 i++;  
  533.                   
  534.             }  
  535.               
  536.             if (name != null && value != null && !name.equals("")) {  
  537.                 result.put(name, value);  
  538.             }  
  539.               
  540.             return result;  
  541.               
  542.         }  
  543.         return null;  
  544.     }  
  545.       
  546.     /** 
  547.      * 根据输入的多个解释和下标返回一个值 
  548.      *  
  549.      * @param captions 
  550.      *            例如:"无,爱干净,一般,比较乱" 
  551.      * @param index 
  552.      *            1 
  553.      * @return 一般 
  554.      */  
  555.     public static String getCaption(String captions, int index) {  
  556.         if (index > 0 && captions != null && !captions.equals("")) {  
  557.             String[] ss = captions.split(",");  
  558.             if (ss != null && ss.length > 0 && index < ss.length) {  
  559.                 return ss[index];  
  560.             }  
  561.         }  
  562.         return null;  
  563.     }  
  564.       
  565.     /** 
  566.      * 数字转字符串,如果num<=0 则输出""; 
  567.      *  
  568.      * @param num 
  569.      * @return 
  570.      */  
  571.     public static String numberToString(Object num) {  
  572.         if (num == null) {  
  573.             return null;  
  574.         } else if (num instanceof Integer && (Integer) num > 0) {  
  575.             return Integer.toString((Integer) num);  
  576.         } else if (num instanceof Long && (Long) num > 0) {  
  577.             return Long.toString((Long) num);  
  578.         } else if (num instanceof Float && (Float) num > 0) {  
  579.             return Float.toString((Float) num);  
  580.         } else if (num instanceof Double && (Double) num > 0) {  
  581.             return Double.toString((Double) num);  
  582.         } else {  
  583.             return "";  
  584.         }  
  585.     }  
  586.       
  587.     /** 
  588.      * 货币转字符串 
  589.      *  
  590.      * @param money 
  591.      * @param style 
  592.      *            样式 [default]要格式化成的格式 such as #.00, #.# 
  593.      * @return 
  594.      */  
  595.       
  596.     public static String moneyToString(Object money, String style) {  
  597.         if (money != null && style != null && (money instanceof Double || money instanceof Float)) {  
  598.             Double num = (Double) money;  
  599.               
  600.             if (style.equalsIgnoreCase("default")) {  
  601.                 // 缺省样式 0 不输出 ,如果没有输出小数位则不输出.0  
  602.                 if (num == 0) {  
  603.                     // 不输出0  
  604.                     return "";  
  605.                 } else if ((num * 10 % 10) == 0) {  
  606.                     // 没有小数  
  607.                     return Integer.toString((int) num.intValue());  
  608.                 } else {  
  609.                     // 有小数  
  610.                     return num.toString();  
  611.                 }  
  612.                   
  613.             } else {  
  614.                 DecimalFormat df = new DecimalFormat(style);  
  615.                 return df.format(num);  
  616.             }  
  617.         }  
  618.         return null;  
  619.     }  
  620.       
  621.     /** 
  622.      * 在sou中是否存在finds 如果指定的finds字符串有一个在sou中找到,返回true; 
  623.      *  
  624.      * @param sou 
  625.      * @param find 
  626.      * @return 
  627.      */  
  628.     public static boolean strPos(String sou, String... finds) {  
  629.         if (sou != null && finds != null && finds.length > 0) {  
  630.             for (int i = 0; i < finds.length; i++) {  
  631.                 if (sou.indexOf(finds[i]) > -1)  
  632.                     return true;  
  633.             }  
  634.         }  
  635.         return false;  
  636.     }  
  637.       
  638.     public static boolean strPos(String sou, List<String> finds) {  
  639.         if (sou != null && finds != null && finds.size() > 0) {  
  640.             for (String s : finds) {  
  641.                 if (sou.indexOf(s) > -1)  
  642.                     return true;  
  643.             }  
  644.         }  
  645.         return false;  
  646.     }  
  647.       
  648.     public static boolean strPos(String sou, String finds) {  
  649.         List<String> t = splitToList(",", finds);  
  650.         return strPos(sou, t);  
  651.     }  
  652.       
  653.     /** 
  654.      * 判断两个字符串是否相等 如果都为null则判断为相等,一个为null另一个not null则判断不相等 否则如果s1=s2则相等 
  655.      *  
  656.      * @param s1 
  657.      * @param s2 
  658.      * @return 
  659.      */  
  660.     public static boolean equals(String s1, String s2) {  
  661.         if (StringUtil.isEmpty(s1) && StringUtil.isEmpty(s2)) {  
  662.             return true;  
  663.         } else if (!StringUtil.isEmpty(s1) && !StringUtil.isEmpty(s2)) {  
  664.             return s1.equals(s2);  
  665.         }  
  666.         return false;  
  667.     }  
  668.       
  669.     public static int toInt(String s) {  
  670.         if (s != null && !"".equals(s.trim())) {  
  671.             try {  
  672.                 return Integer.parseInt(s);  
  673.             } catch (Exception e) {  
  674.                 return 0;  
  675.             }  
  676.         }  
  677.         return 0;  
  678.     }  
  679.       
  680.     public static double toDouble(String s) {  
  681.         if (s != null && !"".equals(s.trim())) {  
  682.             return Double.parseDouble(s);  
  683.         }  
  684.         return 0;  
  685.     }  
  686.       
  687.     public static boolean isPhone(String phone) {  
  688.         if (phone == null && "".equals(phone)) {  
  689.             return false;  
  690.         }  
  691.         String[] strPhone = phone.split("-");  
  692.         try {  
  693.             for (int i = 0; i < strPhone.length; i++) {  
  694.                 Long.parseLong(strPhone[i]);  
  695.             }  
  696.               
  697.         } catch (Exception e) {  
  698.             return false;  
  699.         }  
  700.         return true;  
  701.           
  702.     }  
  703.       
  704.     /** 
  705.      * 把xml 转为object 
  706.      *  
  707.      * @param xml 
  708.      * @return 
  709.      */  
  710.     public static Object xmlToObject(String xml) {  
  711.         try {  
  712.             ByteArrayInputStream in = new ByteArrayInputStream(xml.getBytes("UTF8"));  
  713.             XMLDecoder decoder = new XMLDecoder(new BufferedInputStream(in));  
  714.             return decoder.readObject();  
  715.         } catch (Exception e) {  
  716.             e.printStackTrace();  
  717.         }  
  718.         return null;  
  719.     }  
  720.       
  721.     /** 
  722.      * 按规定长度截断字符串,没有...的省略符号 
  723.      *  
  724.      * @param subject 
  725.      * @param size 
  726.      * @return 
  727.      */  
  728.     public static String subStringNoEllipsis(String subject, int size) {  
  729.         subject = TextUtils.htmlEncode(subject);  
  730.         if (subject.length() > size) {  
  731.             subject = subject.substring(0, size);  
  732.         }  
  733.         return subject;  
  734.     }  
  735.       
  736.     public static long toLong(String s) {  
  737.         try {  
  738.             if (s != null && !"".equals(s.trim()))  
  739.                 return Long.parseLong(s);  
  740.         } catch (Exception exception) {  
  741.         }  
  742.         return 0L;  
  743.     }  
  744.       
  745.     public static String simpleEncrypt(String str) {  
  746.         if (str != null && str.length() > 0) {  
  747.             // str = str.replaceAll("0","a");  
  748.             str = str.replaceAll("1""b");  
  749.             // str = str.replaceAll("2","c");  
  750.             str = str.replaceAll("3""d");  
  751.             // str = str.replaceAll("4","e");  
  752.             str = str.replaceAll("5""f");  
  753.             str = str.replaceAll("6""g");  
  754.             str = str.replaceAll("7""h");  
  755.             str = str.replaceAll("8""i");  
  756.             str = str.replaceAll("9""j");  
  757.         }  
  758.         return str;  
  759.           
  760.     }  
  761.       
  762.     /** 
  763.      * 过滤用户输入的URL地址(防治用户广告) 目前只针对以http或www开头的URL地址 
  764.      * 本方法调用的正则表达式,不建议用在对性能严格的地方例如:循环及list页面等 
  765.      *  
  766.      * @param str 
  767.      *            需要处理的字符串 
  768.      * @return 返回处理后的字符串 
  769.      */  
  770.     public static String removeURL(String str) {  
  771.         if (str != null)  
  772.             str = str.toLowerCase().replaceAll("(http|www|com|cn|org|\\.)+""");  
  773.         return str;  
  774.     }  
  775.       
  776.     /** 
  777.      * 随即生成指定位数的含数字验证码字符串 
  778.      *  
  779.      * @param bit 
  780.      *            指定生成验证码位数 
  781.      * @return String 
  782.      */  
  783.     public static String numRandom(int bit) {  
  784.         if (bit == 0)  
  785.             bit = 6// 默认6位  
  786.         String str = "";  
  787.         str = "0123456789";// 初始化种子  
  788.         return RandomStringUtils.random(bit, str);// 返回6位的字符串  
  789.     }  
  790.       
  791.     /** 
  792.      * 随即生成指定位数的含验证码字符串 
  793.      *  
  794.      * @param bit 
  795.      *            指定生成验证码位数 
  796.      * @return String 
  797.      */  
  798.     public static String random(int bit) {  
  799.         if (bit == 0)  
  800.             bit = 6// 默认6位  
  801.         // 因为o和0,l和1很难区分,所以,去掉大小写的o和l  
  802.         String str = "";  
  803.         str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz";// 初始化种子  
  804.         return RandomStringUtils.random(bit, str);// 返回6位的字符串  
  805.     }  
  806.       
  807.     /** 
  808.      * 检查字符串是否属于手机号码 
  809.      *  
  810.      * @param str 
  811.      * @return boolean 
  812.      */  
  813.     public static boolean isMobile(String str) {  
  814.         if (str == null || str.equals(""))  
  815.             return false;  
  816.         if (str.length() != 11 || !isNumeric(str))  
  817.             return false;  
  818.         if (!str.substring(02).equals("13") && !str.substring(02).equals("15")  
  819.                 && !str.substring(02).equals("18"))  
  820.             return false;  
  821.         return true;  
  822.     }  
  823.       
  824.     /** 
  825.      * Wap页面的非法字符检查 
  826.      *  
  827.      * @param str 
  828.      * @return 
  829.      */  
  830.     public static String replaceWapStr(String str) {  
  831.         if (str != null) {  
  832.             str = str.replaceAll("<span class=\"keyword\">""");  
  833.             str = str.replaceAll("</span>""");  
  834.             str = str.replaceAll("<strong class=\"keyword\">""");  
  835.             str = str.replaceAll("<strong>""");  
  836.             str = str.replaceAll("</strong>""");  
  837.               
  838.             str = str.replace('$''$');  
  839.               
  840.             str = str.replaceAll("&""&");  
  841.             str = str.replace('&''&');  
  842.               
  843.             str = str.replace('<''<');  
  844.               
  845.             str = str.replace('>''>');  
  846.               
  847.         }  
  848.         return str;  
  849.     }  
  850.       
  851.     /** 
  852.      * 字符串转float 如果异常返回0.00 
  853.      *  
  854.      * @param s 
  855.      *            输入的字符串 
  856.      * @return 转换后的float 
  857.      */  
  858.     public static Float toFloat(String s) {  
  859.         try {  
  860.             return Float.parseFloat(s);  
  861.         } catch (NumberFormatException e) {  
  862.             return new Float(0);  
  863.         }  
  864.     }  
  865.       
  866.     /** 
  867.      * 页面中去除字符串中的空格、回车、换行符、制表符 
  868.      *  
  869.      * @param str 
  870.      * @return 
  871.      */  
  872.     public static String replaceBlank(String str) {  
  873.         if (str != null) {  
  874.             Pattern p = Pattern.compile("\\s*|\t|\r|\n");  
  875.             Matcher m = p.matcher(str);  
  876.             str = m.replaceAll("");  
  877.         }  
  878.         return str;  
  879.     }  
  880.       
  881.     /** 
  882.      * 全角生成半角 
  883.      *  
  884.      * @param str 
  885.      * @return 
  886.      */  
  887.     public static String Q2B(String QJstr) {  
  888.         String outStr = "";  
  889.         String Tstr = "";  
  890.         byte[] b = null;  
  891.         for (int i = 0; i < QJstr.length(); i++) {  
  892.             try {  
  893.                 Tstr = QJstr.substring(i, i + 1);  
  894.                 b = Tstr.getBytes("unicode");  
  895.             } catch (java.io.UnsupportedEncodingException e) {  
  896.                 e.printStackTrace();  
  897.             }  
  898.             if (b[3] == -1) {  
  899.                 b[2] = (byte) (b[2] + 32);  
  900.                 b[3] = 0;  
  901.                 try {  
  902.                     outStr = outStr + new String(b, "unicode");  
  903.                 } catch (java.io.UnsupportedEncodingException ex) {  
  904.                     ex.printStackTrace();  
  905.                 }  
  906.             } else {  
  907.                 outStr = outStr + Tstr;  
  908.             }  
  909.         }  
  910.         return outStr;  
  911.     }  
  912.       
  913.     /** 
  914.      * 转换编码 
  915.      *  
  916.      * @param s 
  917.      *            源字符串 
  918.      * @param fencode 
  919.      *            源编码格式 
  920.      * @param bencode 
  921.      *            目标编码格式 
  922.      * @return 目标编码 
  923.      */  
  924.     public static String changCoding(String s, String fencode, String bencode) {  
  925.         try {  
  926.             String str = new String(s.getBytes(fencode), bencode);  
  927.             return str;  
  928.         } catch (UnsupportedEncodingException e) {  
  929.             return s;  
  930.         }  
  931.     }  
  932.       
  933.     /** 
  934.      * 除去html标签 
  935.      *  
  936.      * @param str 
  937.      * @return 
  938.      */  
  939.     public static String removeHTMLLableExe(String str) {  
  940.         str = stringReplace(str, ">\\s*<""><");  
  941.         str = stringReplace(str, " "" ");// 替换空格  
  942.         str = stringReplace(str, "<br ?/?>""\n");// 去<br><br />  
  943.         str = stringReplace(str, "<([^<>]+)>""");// 去掉<>内的字符  
  944.         str = stringReplace(str, "\\s\\s\\s*"" ");// 将多个空白变成一个空格  
  945.         str = stringReplace(str, "^\\s*""");// 去掉头的空白  
  946.         str = stringReplace(str, "\\s*$""");// 去掉尾的空白  
  947.         str = stringReplace(str, " +"" ");  
  948.         return str;  
  949.     }  
  950.       
  951.     /** 
  952.      * 除去html标签 
  953.      *  
  954.      * @param str 
  955.      *            源字符串 
  956.      * @return 目标字符串 
  957.      */  
  958.     public static String removeHTMLLable(String str) {  
  959.         str = stringReplace(str, "\\s""");// 去掉页面上看不到的字符  
  960.         str = stringReplace(str, "<br ?/?>""\n");// 去<br><br />  
  961.         str = stringReplace(str, "<([^<>]+)>""");// 去掉<>内的字符  
  962.         str = stringReplace(str, " "" ");// 替换空格  
  963.         str = stringReplace(str, "&(\\S)(\\S?)(\\S?)(\\S?);""");// 去<br><br />  
  964.         return str;  
  965.     }  
  966.       
  967.     /** 
  968.      * 去掉HTML标签之外的字符串 
  969.      *  
  970.      * @param str 
  971.      *            源字符串 
  972.      * @return 目标字符串 
  973.      */  
  974.     public static String removeOutHTMLLable(String str) {  
  975.         str = stringReplace(str, ">([^<>]+)<""><");  
  976.         str = stringReplace(str, "^([^<>]+)<""<");  
  977.         str = stringReplace(str, ">([^<>]+)$"">");  
  978.         return str;  
  979.     }  
  980.       
  981.     /** 
  982.      * 字符串替换 
  983.      *  
  984.      * @param str 
  985.      *            源字符串 
  986.      * @param sr 
  987.      *            正则表达式样式 
  988.      * @param sd 
  989.      *            替换文本 
  990.      * @return 结果串 
  991.      */  
  992.     public static String stringReplace(String str, String sr, String sd) {  
  993.         String regEx = sr;  
  994.         Pattern p = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);  
  995.         Matcher m = p.matcher(str);  
  996.         str = m.replaceAll(sd);  
  997.         return str;  
  998.     }  
  999.       
  1000.     /** 
  1001.      * 将html的省略写法替换成非省略写法 
  1002.      *  
  1003.      * @param str 
  1004.      *            html字符串 
  1005.      * @param pt 
  1006.      *            标签如table 
  1007.      * @return 结果串 
  1008.      */  
  1009.     public static String fomateToFullForm(String str, String pt) {  
  1010.         String regEx = "<" + pt + "\\s+([\\S&&[^<>]]*)/>";  
  1011.         Pattern p = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);  
  1012.         Matcher m = p.matcher(str);  
  1013.         String[] sa = null;  
  1014.         String sf = "";  
  1015.         String sf2 = "";  
  1016.         String sf3 = "";  
  1017.         for (; m.find();) {  
  1018.             sa = p.split(str);  
  1019.             if (sa == null) {  
  1020.                 break;  
  1021.             }  
  1022.             sf = str.substring(sa[0].length(), str.indexOf("/>", sa[0].length()));  
  1023.             sf2 = sf + "></" + pt + ">";  
  1024.             sf3 = str.substring(sa[0].length() + sf.length() + 2);  
  1025.             str = sa[0] + sf2 + sf3;  
  1026.             sa = null;  
  1027.         }  
  1028.         return str;  
  1029.     }  
  1030.       
  1031.     /** 
  1032.      * 得到字符串的子串位置序列 
  1033.      *  
  1034.      * @param str 
  1035.      *            字符串 
  1036.      * @param sub 
  1037.      *            子串 
  1038.      * @param b 
  1039.      *            true子串前端,false子串后端 
  1040.      * @return 字符串的子串位置序列 
  1041.      */  
  1042.     public static int[] getSubStringPos(String str, String sub, boolean b) {  
  1043.         // int[] i = new int[(new Integer((str.length()-stringReplace( str , sub  
  1044.         // , "" ).length())/sub.length())).intValue()] ;  
  1045.         String[] sp = null;  
  1046.         int l = sub.length();  
  1047.         sp = splitString(str, sub);  
  1048.         if (sp == null) {  
  1049.             return null;  
  1050.         }  
  1051.         int[] ip = new int[sp.length - 1];  
  1052.         for (int i = 0; i < sp.length - 1; i++) {  
  1053.             ip[i] = sp[i].length() + l;  
  1054.             if (i != 0) {  
  1055.                 ip[i] += ip[i - 1];  
  1056.             }  
  1057.         }  
  1058.         if (b) {  
  1059.             for (int j = 0; j < ip.length; j++) {  
  1060.                 ip[j] = ip[j] - l;  
  1061.             }  
  1062.         }  
  1063.         return ip;  
  1064.     }  
  1065.       
  1066.     /** 
  1067.      * 根据正则表达式分割字符串 
  1068.      *  
  1069.      * @param str 
  1070.      *            源字符串 
  1071.      * @param ms 
  1072.      *            正则表达式 
  1073.      * @return 目标字符串组 
  1074.      */  
  1075.     public static String[] splitString(String str, String ms) {  
  1076.         String regEx = ms;  
  1077.         Pattern p = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);  
  1078.         String[] sp = p.split(str);  
  1079.         return sp;  
  1080.     }  
  1081.       
  1082.     /** 
  1083.      * ************************************************************************* 
  1084.      * 根据正则表达式提取字符串,相同的字符串只返回一个 
  1085.      *  
  1086.      * @param str 
  1087.      *            源字符串 
  1088.      * @param pattern 
  1089.      *            正则表达式 
  1090.      * @return 目标字符串数据组 
  1091.      */  
  1092.       
  1093.     // ★传入一个字符串,把符合pattern格式的字符串放入字符串数组  
  1094.     // java.util.regex是一个用正则表达式所订制的模式来对字符串进行匹配工作的类库包  
  1095.     public static String[] getStringArrayByPattern(String str, String pattern) {  
  1096.         Pattern p = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);  
  1097.         Matcher matcher = p.matcher(str);  
  1098.         // 范型  
  1099.         Set<String> result = new HashSet<String>();// 目的是:相同的字符串只返回一个。。。 不重复元素  
  1100.         // boolean find() 尝试在目标字符串里查找下一个匹配子串。  
  1101.         while (matcher.find()) {  
  1102.             for (int i = 0; i < matcher.groupCount(); i++) { // int groupCount()  
  1103.                                                              // 返回当前查找所获得的匹配组的数量。  
  1104.                 // System.out.println(matcher.group(i));  
  1105.                 result.add(matcher.group(i));  
  1106.                   
  1107.             }  
  1108.         }  
  1109.         String[] resultStr = null;  
  1110.         if (result.size() > 0) {  
  1111.             resultStr = new String[result.size()];  
  1112.             return result.toArray(resultStr);// 将Set result转化为String[] resultStr  
  1113.         }  
  1114.         return resultStr;  
  1115.           
  1116.     }  
  1117.       
  1118.     /** 
  1119.      * 得到第一个b,e之间的字符串,并返回e后的子串 
  1120.      *  
  1121.      * @param s 
  1122.      *            源字符串 
  1123.      * @param b 
  1124.      *            标志开始 
  1125.      * @param e 
  1126.      *            标志结束 
  1127.      * @return b,e之间的字符串 
  1128.      */  
  1129.       
  1130.     /* 
  1131.      * String aaa="abcdefghijklmn"; String[] bbb=StringProcessor.midString(aaa, 
  1132.      * "b","l"); System.out.println("bbb[0]:"+bbb[0]);//cdefghijk 
  1133.      * System.out.println("bbb[1]:"+bbb[1]);//lmn 
  1134.      * ★这个方法是得到第二个参数和第三个参数之间的字符串,赋给元素0;然后把元素0代表的字符串之后的,赋给元素1 
  1135.      */  
  1136.       
  1137.     /* 
  1138.      * String aaa="abcdefgllhijklmn5465"; String[] 
  1139.      * bbb=StringProcessor.midString(aaa, "b","l"); //ab cdefg llhijklmn5465 // 
  1140.      * 元素0 元素1 
  1141.      */  
  1142.     public static String[] midString(String s, String b, String e) {  
  1143.         int i = s.indexOf(b) + b.length();  
  1144.         int j = s.indexOf(e, i);  
  1145.         String[] sa = new String[2];  
  1146.         if (i < b.length() || j < i + 1 || i > j) {  
  1147.             sa[1] = s;  
  1148.             sa[0] = null;  
  1149.             return sa;  
  1150.         } else {  
  1151.             sa[0] = s.substring(i, j);  
  1152.             sa[1] = s.substring(j);  
  1153.             return sa;  
  1154.         }  
  1155.     }  
  1156.       
  1157.     /** 
  1158.      * 带有前一次替代序列的正则表达式替代 
  1159.      *  
  1160.      * @param s 
  1161.      * @param pf 
  1162.      * @param pb 
  1163.      * @param start 
  1164.      * @return 
  1165.      */  
  1166.     public static String stringReplace(String s, String pf, String pb, int start) {  
  1167.         Pattern pattern_hand = Pattern.compile(pf);  
  1168.         Matcher matcher_hand = pattern_hand.matcher(s);  
  1169.         int gc = matcher_hand.groupCount();  
  1170.         int pos = start;  
  1171.         String sf1 = "";  
  1172.         String sf2 = "";  
  1173.         String sf3 = "";  
  1174.         int if1 = 0;  
  1175.         String strr = "";  
  1176.         while (matcher_hand.find(pos)) {  
  1177.             sf1 = matcher_hand.group();  
  1178.             if1 = s.indexOf(sf1, pos);  
  1179.             if (if1 >= pos) {  
  1180.                 strr += s.substring(pos, if1);  
  1181.                 pos = if1 + sf1.length();  
  1182.                 sf2 = pb;  
  1183.                 for (int i = 1; i <= gc; i++) {  
  1184.                     sf3 = "\\" + i;  
  1185.                     sf2 = replaceAll(sf2, sf3, matcher_hand.group(i));  
  1186.                 }  
  1187.                 strr += sf2;  
  1188.             } else {  
  1189.                 return s;  
  1190.             }  
  1191.         }  
  1192.         strr = s.substring(0, start) + strr;  
  1193.         return strr;  
  1194.     }  
  1195.       
  1196.     /** 
  1197.      * 存文本替换 
  1198.      *  
  1199.      * @param s 
  1200.      *            源字符串 
  1201.      * @param sf 
  1202.      *            子字符串 
  1203.      * @param sb 
  1204.      *            替换字符串 
  1205.      * @return 替换后的字符串 
  1206.      */  
  1207.     public static String replaceAll(String s, String sf, String sb) {  
  1208.         int i = 0, j = 0;  
  1209.         int l = sf.length();  
  1210.         boolean b = true;  
  1211.         boolean o = true;  
  1212.         String str = "";  
  1213.         do {  
  1214.             j = i;  
  1215.             i = s.indexOf(sf, j);  
  1216.             if (i > j) {  
  1217.                 str += s.substring(j, i);  
  1218.                 str += sb;  
  1219.                 i += l;  
  1220.                 o = false;  
  1221.             } else {  
  1222.                 str += s.substring(j);  
  1223.                 b = false;  
  1224.             }  
  1225.         } while (b);  
  1226.         if (o) {  
  1227.             str = s;  
  1228.         }  
  1229.         return str;  
  1230.     }  
  1231.       
  1232.     /** 
  1233.      * 判断是否与给定字符串样式匹配 
  1234.      *  
  1235.      * @param str 
  1236.      *            字符串 
  1237.      * @param pattern 
  1238.      *            正则表达式样式 
  1239.      * @return 是否匹配是true,否false 
  1240.      */  
  1241.     public static boolean isMatch(String str, String pattern) {  
  1242.         Pattern pattern_hand = Pattern.compile(pattern);  
  1243.         Matcher matcher_hand = pattern_hand.matcher(str);  
  1244.         boolean b = matcher_hand.matches();  
  1245.         return b;  
  1246.     }  
  1247.       
  1248.     /** 
  1249.      * 截取字符串 
  1250.      *  
  1251.      * @param s 
  1252.      *            源字符串 
  1253.      * @param jmp 
  1254.      *            跳过jmp 
  1255.      * @param sb 
  1256.      *            取在sb 
  1257.      * @param se 
  1258.      *            于se 
  1259.      * @return 之间的字符串 
  1260.      */  
  1261.     public static String subStringExe(String s, String jmp, String sb, String se) {  
  1262.         if (isEmpty(s)) {  
  1263.             return "";  
  1264.         }  
  1265.         int i = s.indexOf(jmp);  
  1266.         if (i >= 0 && i < s.length()) {  
  1267.             s = s.substring(i + 1);  
  1268.         }  
  1269.         i = s.indexOf(sb);  
  1270.         if (i >= 0 && i < s.length()) {  
  1271.             s = s.substring(i + 1);  
  1272.         }  
  1273.         if (se == "") {  
  1274.             return s;  
  1275.         } else {  
  1276.             i = s.indexOf(se);  
  1277.             if (i >= 0 && i < s.length()) {  
  1278.                 s = s.substring(i + 1);  
  1279.             }  
  1280.             return s;  
  1281.         }  
  1282.     }  
  1283.       
  1284.     /** 
  1285.      * 用要通过URL传输的内容进行编码 
  1286.      *  
  1287.      * @param 源字符串 
  1288.      * @return 经过编码的内容 
  1289.      */  
  1290.     public static String URLEncode(String src) {  
  1291.         String return_value = "";  
  1292.         try {  
  1293.             if (src != null) {  
  1294.                 return_value = URLEncoder.encode(src, "GBK");  
  1295.                   
  1296.             }  
  1297.         } catch (UnsupportedEncodingException e) {  
  1298.             e.printStackTrace();  
  1299.             return_value = src;  
  1300.         }  
  1301.           
  1302.         return return_value;  
  1303.     }  
  1304.       
  1305.     /** 
  1306.      * 换成GBK的字符串 
  1307.      *  
  1308.      * @param str 
  1309.      * @return 
  1310.      */  
  1311.     public static String getGBK(String str) {  
  1312.           
  1313.         return transfer(str);  
  1314.     }  
  1315.       
  1316.     public static String transfer(String str) {  
  1317.         Pattern p = Pattern.compile("&#\\d+;");  
  1318.         Matcher m = p.matcher(str);  
  1319.         while (m.find()) {  
  1320.             String old = m.group();  
  1321.             str = str.replaceAll(old, getChar(old));  
  1322.         }  
  1323.         return str;  
  1324.     }  
  1325.       
  1326.     public static String getChar(String str) {  
  1327.         String dest = str.substring(2, str.length() - 1);  
  1328.         char ch = (char) Integer.parseInt(dest);  
  1329.         return "" + ch;  
  1330.     }  
  1331.       
  1332.     /** 
  1333.      * 首页中切割字符串. 
  1334.      *  
  1335.      * @date 2007-09-17 
  1336.      * @param str 
  1337.      * @return 
  1338.      */  
  1339.     public static String subYhooString(String subject, int size) {  
  1340.         subject = subject.substring(1, size);  
  1341.         return subject;  
  1342.     }  
  1343.       
  1344.     public static String subYhooStringDot(String subject, int size) {  
  1345.         subject = subject.substring(1, size) + "...";  
  1346.         return subject;  
  1347.     }  
  1348.       
  1349.     /** 
  1350.      * 泛型方法(通用),把list转换成以“,”相隔的字符串 调用时注意类型初始化(申明类型) 如:List<Integer> intList = 
  1351.      * new ArrayList<Integer>(); 调用方法:StringUtil.listTtoString(intList); 
  1352.      * 效率:list中4条信息,1000000次调用时间为850ms左右 
  1353.      *  
  1354.      * @param <T> 
  1355.      *            泛型 
  1356.      * @param list 
  1357.      *            list列表 
  1358.      * @return 以“,”相隔的字符串 
  1359.      */  
  1360.     public static <T> String listTtoString(List<T> list) {  
  1361.         if (list == null || list.size() < 1)  
  1362.             return "";  
  1363.         Iterator<T> i = list.iterator();  
  1364.         if (!i.hasNext())  
  1365.             return "";  
  1366.         StringBuilder sb = new StringBuilder();  
  1367.         for (;;) {  
  1368.             T e = i.next();  
  1369.             sb.append(e);  
  1370.             if (!i.hasNext())  
  1371.                 return sb.toString();  
  1372.             sb.append(",");  
  1373.         }  
  1374.     }  
  1375.       
  1376.     /** 
  1377.      * 把整形数组转换成以“,”相隔的字符串 
  1378.      *  
  1379.      * @param a 
  1380.      *            数组a 
  1381.      * @return 以“,”相隔的字符串 
  1382.      */  
  1383.     public static String intArraytoString(int[] a) {  
  1384.         if (a == null)  
  1385.             return "";  
  1386.         int iMax = a.length - 1;  
  1387.         if (iMax == -1)  
  1388.             return "";  
  1389.         StringBuilder b = new StringBuilder();  
  1390.         for (int i = 0;; i++) {  
  1391.             b.append(a[i]);  
  1392.             if (i == iMax)  
  1393.                 return b.toString();  
  1394.             b.append(",");  
  1395.         }  
  1396.     }  
  1397.       
  1398.     /** 
  1399.      * 判断文字内容重复 
  1400.      *  
  1401.      * @Date 2008-04-17 
  1402.      */  
  1403.     public static boolean isContentRepeat(String content) {  
  1404.         int similarNum = 0;  
  1405.         int forNum = 0;  
  1406.         int subNum = 0;  
  1407.         int thousandNum = 0;  
  1408.         String startStr = "";  
  1409.         String nextStr = "";  
  1410.         boolean result = false;  
  1411.         float endNum = (float0.0;  
  1412.         if (content != null && content.length() > 0) {  
  1413.             if (content.length() % 1000 > 0)  
  1414.                 thousandNum = (int) Math.floor(content.length() / 1000) + 1;  
  1415.             else  
  1416.                 thousandNum = (int) Math.floor(content.length() / 1000);  
  1417.             if (thousandNum < 3)  
  1418.                 subNum = 100 * thousandNum;  
  1419.             else if (thousandNum < 6)  
  1420.                 subNum = 200 * thousandNum;  
  1421.             else if (thousandNum < 9)  
  1422.                 subNum = 300 * thousandNum;  
  1423.             else  
  1424.                 subNum = 3000;  
  1425.             for (int j = 1; j < subNum; j++) {  
  1426.                 if (content.length() % j > 0)  
  1427.                     forNum = (int) Math.floor(content.length() / j) + 1;  
  1428.                 else  
  1429.                     forNum = (int) Math.floor(content.length() / j);  
  1430.                 if (result || j >= content.length())  
  1431.                     break;  
  1432.                 else {  
  1433.                     for (int m = 0; m < forNum; m++) {  
  1434.                         if (m * j > content.length() || (m + 1) * j > content.length()  
  1435.                                 || (m + 2) * j > content.length())  
  1436.                             break;  
  1437.                         startStr = content.substring(m * j, (m + 1) * j);  
  1438.                         nextStr = content.substring((m + 1) * j, (m + 2) * j);  
  1439.                         if (startStr.equals(nextStr)) {  
  1440.                             similarNum = similarNum + 1;  
  1441.                             endNum = (float) similarNum / forNum;  
  1442.                             if (endNum > 0.4) {  
  1443.                                 result = true;  
  1444.                                 break;  
  1445.                             }  
  1446.                         } else  
  1447.                             similarNum = 0;  
  1448.                     }  
  1449.                 }  
  1450.             }  
  1451.         }  
  1452.         return result;  
  1453.     }  
  1454.       
  1455.     /** 
  1456.      * Ascii转为Char 
  1457.      *  
  1458.      * @param asc 
  1459.      * @return 
  1460.      */  
  1461.     public static String AsciiToChar(int asc) {  
  1462.         String TempStr = "A";  
  1463.         char tempchar = (char) asc;  
  1464.         TempStr = String.valueOf(tempchar);  
  1465.         return TempStr;  
  1466.     }  
  1467.       
  1468.     /** 
  1469.      * 判断是否是空字符串 null和"" null返回result,否则返回字符串 
  1470.      *  
  1471.      * @param s 
  1472.      * @return 
  1473.      */  
  1474.     public static String isEmpty(String s, String result) {  
  1475.         if (s != null && !s.equals("")) {  
  1476.             return s;  
  1477.         }  
  1478.         return result;  
  1479.     }  
  1480.       
  1481.     /** 
  1482.      * 将带有htmlcode代码的字符转换成<>&'" 
  1483.      *  
  1484.      * @param str 
  1485.      * @return 
  1486.      */  
  1487.     public static String htmlcodeToSpecialchars(String str) {  
  1488.         str = str.replaceAll("&""&");  
  1489.         str = str.replaceAll(""", "\"");  
  1490.         str = str.replaceAll("'""'");  
  1491.         str = str.replaceAll("<""<");  
  1492.         str = str.replaceAll(">"">");  
  1493.         return str;  
  1494.     }  
  1495.       
  1496.     /** 
  1497.      * 移除html标签 
  1498.      *  
  1499.      * @param htmlstr 
  1500.      * @return 
  1501.      */  
  1502.     public static String removeHtmlTag(String htmlstr) {  
  1503.         Pattern pat = Pattern.compile("\\s*<.*?>\\s*", Pattern.DOTALL | Pattern.MULTILINE  
  1504.                 | Pattern.CASE_INSENSITIVE); // \\s?[s|Sc|Cr|Ri|Ip|Pt|T]  
  1505.         Matcher m = pat.matcher(htmlstr);  
  1506.         String rs = m.replaceAll("");  
  1507.         rs = rs.replaceAll(" "" ");  
  1508.         rs = rs.replaceAll("<""<");  
  1509.         rs = rs.replaceAll(">"">");  
  1510.         return rs;  
  1511.     }  
  1512.       
  1513.     /** 
  1514.      * 取从指定搜索项开始的字符串,返回的值不包含搜索项 
  1515.      *  
  1516.      * @param captions 
  1517.      *            例如:"www.koubei.com" 
  1518.      * @param regex 
  1519.      *            分隔符,例如"." 
  1520.      * @return 结果字符串,如:koubei.com,如未找到返回空串 
  1521.      */  
  1522.     public static String getStrAfterRegex(String captions, String regex) {  
  1523.         if (!isEmpty(captions) && !isEmpty(regex)) {  
  1524.             int pos = captions.indexOf(regex);  
  1525.             if (pos != -1 && pos < captions.length() - 1) {  
  1526.                 return captions.substring(pos + 1);  
  1527.             }  
  1528.         }  
  1529.         return "";  
  1530.     }  
  1531.       
  1532.     /** 
  1533.      * 把字节码转换成16进制 
  1534.      */  
  1535.     public static String byte2hex(byte bytes[]) {  
  1536.         StringBuffer retString = new StringBuffer();  
  1537.         for (int i = 0; i < bytes.length; ++i) {  
  1538.             retString.append(Integer.toHexString(0x0100 + (bytes[i] & 0x00FF)).substring(1).toUpperCase());  
  1539.         }  
  1540.         return retString.toString();  
  1541.     }  
  1542.       
  1543.     /** 
  1544.      * 把16进制转换成字节码 
  1545.      *  
  1546.      * @param hex 
  1547.      * @return 
  1548.      */  
  1549.     public static byte[] hex2byte(String hex) {  
  1550.         byte[] bts = new byte[hex.length() / 2];  
  1551.         for (int i = 0; i < bts.length; i++) {  
  1552.             bts[i] = (byte) Integer.parseInt(hex.substring(2 * i, 2 * i + 2), 16);  
  1553.         }  
  1554.         return bts;  
  1555.     }  
  1556.       
  1557.     /** 
  1558.      * 转换数字为固定长度的字符串 
  1559.      *  
  1560.      * @param length 
  1561.      *            希望返回的字符串长度 
  1562.      * @param data 
  1563.      *            传入的数值 
  1564.      * @return 
  1565.      */  
  1566.     public static String getStringByInt(int length, String data) {  
  1567.         String s_data = "";  
  1568.         int datalength = data.length();  
  1569.         if (length > 0 && length >= datalength) {  
  1570.             for (int i = 0; i < length - datalength; i++) {  
  1571.                 s_data += "0";  
  1572.             }  
  1573.             s_data += data;  
  1574.         }  
  1575.           
  1576.         return s_data;  
  1577.     }  
  1578.       
  1579.     /** 
  1580.      * 判断是否位数字,并可为空 
  1581.      *  
  1582.      * @param src 
  1583.      * @return 
  1584.      */  
  1585.     public static boolean isNumericAndCanNull(String src) {  
  1586.         Pattern numericPattern = Pattern.compile("^[0-9]+$");  
  1587.         if (src == null || src.equals(""))  
  1588.             return true;  
  1589.         boolean return_value = false;  
  1590.         if (src != null && src.length() > 0) {  
  1591.             Matcher m = numericPattern.matcher(src);  
  1592.             if (m.find()) {  
  1593.                 return_value = true;  
  1594.             }  
  1595.         }  
  1596.         return return_value;  
  1597.     }  
  1598.       
  1599.     /** 
  1600.      * @param src 
  1601.      * @return 
  1602.      */  
  1603.     public static boolean isFloatAndCanNull(String src) {  
  1604.         Pattern numericPattern = Pattern.compile("^(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*))$");  
  1605.         if (src == null || src.equals(""))  
  1606.             return true;  
  1607.         boolean return_value = false;  
  1608.         if (src != null && src.length() > 0) {  
  1609.             Matcher m = numericPattern.matcher(src);  
  1610.             if (m.find()) {  
  1611.                 return_value = true;  
  1612.             }  
  1613.         }  
  1614.         return return_value;  
  1615.     }  
  1616.       
  1617.     public static boolean isNotEmpty(String str) {  
  1618.         if (str != null && !str.equals(""))  
  1619.             return true;  
  1620.         else  
  1621.             return false;  
  1622.     }  
  1623.       
  1624.     public static boolean isDate(String date) {  
  1625.         String regEx = "\\d{4}-\\d{2}-\\d{2}\\s\\d{2}:\\d{2}:\\d{2}";  
  1626.         Pattern p = Pattern.compile(regEx);  
  1627.         Matcher m = p.matcher(date);  
  1628.         boolean result = m.find();  
  1629.         return result;  
  1630.     }  
  1631.       
  1632.     public static boolean isFormatDate(String date, String regEx) {  
  1633.         Pattern p = Pattern.compile(regEx);  
  1634.         Matcher m = p.matcher(date);  
  1635.         boolean result = m.find();  
  1636.         return result;  
  1637.     }  
  1638.       
  1639.     /** 
  1640.      * 根据指定整型list 组装成为一个字符串 
  1641.      *  
  1642.      * @param src 
  1643.      * @return 
  1644.      */  
  1645.     public static String listToString(List<Integer> list) {  
  1646.         String str = "";  
  1647.         if (list != null && list.size() > 0) {  
  1648.             for (int id : list) {  
  1649.                 str = str + id + ",";  
  1650.             }  
  1651.             if (!"".equals(str) && str.length() > 0)  
  1652.                 str = str.substring(0, str.length() - 1);  
  1653.         }  
  1654.         return str;  
  1655.     }  
  1656.       
  1657.     /** 
  1658.      * 页面的非法字符检查 
  1659.      *  
  1660.      * @param str 
  1661.      * @return 
  1662.      */  
  1663.     public static String replaceStr(String str) {  
  1664.         if (str != null && str.length() > 0) {  
  1665.             str = str.replaceAll("~"",");  
  1666.             str = str.replaceAll(" "",");  
  1667.             str = str.replaceAll(" "",");  
  1668.             str = str.replaceAll(" "",");  
  1669.             str = str.replaceAll("`"",");  
  1670.             str = str.replaceAll("!"",");  
  1671.             str = str.replaceAll("@"",");  
  1672.             str = str.replaceAll("#"",");  
  1673.             str = str.replaceAll("\\$"",");  
  1674.             str = str.replaceAll("%"",");  
  1675.             str = str.replaceAll("\\^"",");  
  1676.             str = str.replaceAll("&"",");  
  1677.             str = str.replaceAll("\\*"",");  
  1678.             str = str.replaceAll("\\("",");  
  1679.             str = str.replaceAll("\\)"",");  
  1680.             str = str.replaceAll("-"",");  
  1681.             str = str.replaceAll("_"",");  
  1682.             str = str.replaceAll("="",");  
  1683.             str = str.replaceAll("\\+"",");  
  1684.             str = str.replaceAll("\\{"",");  
  1685.             str = str.replaceAll("\\["",");  
  1686.             str = str.replaceAll("\\}"",");  
  1687.             str = str.replaceAll("\\]"",");  
  1688.             str = str.replaceAll("\\|"",");  
  1689.             str = str.replaceAll("\\\\", ",");  
  1690.             str = str.replaceAll(";"",");  
  1691.             str = str.replaceAll(":"",");  
  1692.             str = str.replaceAll("'"",");  
  1693.             str = str.replaceAll("\\\""",");  
  1694.             str = str.replaceAll("<"",");  
  1695.             str = str.replaceAll(">"",");  
  1696.             str = str.replaceAll("\\."",");  
  1697.             str = str.replaceAll("\\?"",");  
  1698.             str = str.replaceAll("/"",");  
  1699.             str = str.replaceAll("~"",");  
  1700.             str = str.replaceAll("`"",");  
  1701.             str = str.replaceAll("!"",");  
  1702.             str = str.replaceAll("@"",");  
  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.         }  
  1748.         return str;  
  1749.     }  
  1750.       
  1751.     /** 
  1752.      * 全角字符变半角字符 
  1753.      *  
  1754.      * @param str 
  1755.      * @return 
  1756.      */  
  1757.     public static String full2Half(String str) {  
  1758.         if (str == null || "".equals(str))  
  1759.             return "";  
  1760.         StringBuffer sb = new StringBuffer();  
  1761.           
  1762.         for (int i = 0; i < str.length(); i++) {  
  1763.             char c = str.charAt(i);  
  1764.               
  1765.             if (c >= 65281 && c < 65373)  
  1766.                 sb.append((char) (c - 65248));  
  1767.             else  
  1768.                 sb.append(str.charAt(i));  
  1769.         }  
  1770.           
  1771.         return sb.toString();  
  1772.           
  1773.     }  
  1774.       
  1775.     /** 
  1776.      * 全角括号转为半角 
  1777.      *  
  1778.      * @param str 
  1779.      * @return 
  1780.      */  
  1781.     public static String replaceBracketStr(String str) {  
  1782.         if (str != null && str.length() > 0) {  
  1783.             str = str.replaceAll("(""(");  
  1784.             str = str.replaceAll(")"")");  
  1785.         }  
  1786.         return str;  
  1787.     }  
  1788.       
  1789.     /** 
  1790.      * 分割字符,从开始到第一个split字符串为止 
  1791.      *  
  1792.      * @param src 
  1793.      *            源字符串 
  1794.      * @param split 
  1795.      *            截止字符串 
  1796.      * @return 
  1797.      */  
  1798.     public static String subStr(String src, String split) {  
  1799.         if (!isEmpty(src)) {  
  1800.             int index = src.indexOf(split);  
  1801.             if (index >= 0) {  
  1802.                 return src.substring(0, index);  
  1803.             }  
  1804.         }  
  1805.         return src;  
  1806.     }  
  1807.       
  1808.     /** 
  1809.      * 取url里的keyword(可选择参数)参数,用于整站搜索整合 
  1810.      *  
  1811.      * @param params 
  1812.      * @param qString 
  1813.      * @return 
  1814.      */  
  1815.     public static String getKeyWord(String params, String qString) {  
  1816.         String keyWord = "";  
  1817.         if (qString != null) {  
  1818.             String param = params + "=";  
  1819.             int i = qString.indexOf(param);  
  1820.             if (i != -1) {  
  1821.                 int j = qString.indexOf("&", i + param.length());  
  1822.                 if (j > 0) {  
  1823.                     keyWord = qString.substring(i + param.length(), j);  
  1824.                 }  
  1825.             }  
  1826.         }  
  1827.         return keyWord;  
  1828.     }  
  1829.       
  1830.     /** 
  1831.      * 解析字符串返回map键值对(例:a=1&b=2 => a=1,b=2) 
  1832.      *  
  1833.      * @param query 
  1834.      *            源参数字符串 
  1835.      * @param split1 
  1836.      *            键值对之间的分隔符(例:&) 
  1837.      * @param split2 
  1838.      *            key与value之间的分隔符(例:=) 
  1839.      * @param dupLink 
  1840.      *            重复参数名的参数值之间的连接符,连接后的字符串作为该参数的参数值,可为null 
  1841.      *            null:不允许重复参数名出现,则靠后的参数值会覆盖掉靠前的参数值。 
  1842.      * @return map 
  1843.      */  
  1844.     @SuppressWarnings("unchecked")  
  1845.     public static Map<String, String> parseQuery(String query, char split1, char split2,  
  1846.             String dupLink) {  
  1847.         if (!isEmpty(query) && query.indexOf(split2) > 0) {  
  1848.             Map<String, String> result = new HashMap();  
  1849.               
  1850.             String name = null;  
  1851.             String value = null;  
  1852.             String tempValue = "";  
  1853.             int len = query.length();  
  1854.             for (int i = 0; i < len; i++) {  
  1855.                 char c = query.charAt(i);  
  1856.                 if (c == split2) {  
  1857.                     value = "";  
  1858.                 } else if (c == split1) {  
  1859.                     if (!isEmpty(name) && value != null) {  
  1860.                         if (dupLink != null) {  
  1861.                             tempValue = result.get(name);  
  1862.                             if (tempValue != null) {  
  1863.                                 value += dupLink + tempValue;  
  1864.                             }  
  1865.                         }  
  1866.                         result.put(name, value);  
  1867.                     }  
  1868.                     name = null;  
  1869.                     value = null;  
  1870.                 } else if (value != null) {  
  1871.                     value += c;  
  1872.                 } else {  
  1873.                     name = (name != null) ? (name + c) : "" + c;  
  1874.                 }  
  1875.             }  
  1876.               
  1877.             if (!isEmpty(name) && value != null) {  
  1878.                 if (dupLink != null) {  
  1879.                     tempValue = result.get(name);  
  1880.                     if (tempValue != null) {  
  1881.                         value += dupLink + tempValue;  
  1882.                     }  
  1883.                 }  
  1884.                 result.put(name, value);  
  1885.             }  
  1886.               
  1887.             return result;  
  1888.         }  
  1889.         return null;  
  1890.     }  
  1891.       
  1892.     /** 
  1893.      * 将list 用传入的分隔符组装为String 
  1894.      *  
  1895.      * @param list 
  1896.      * @param slipStr 
  1897.      * @return String 
  1898.      */  
  1899.     @SuppressWarnings("unchecked")  
  1900.     public static String listToStringSlipStr(List list, String slipStr) {  
  1901.         StringBuffer returnStr = new StringBuffer();  
  1902.         if (list != null && list.size() > 0) {  
  1903.             for (int i = 0; i < list.size(); i++) {  
  1904.                 returnStr.append(list.get(i)).append(slipStr);  
  1905.             }  
  1906.         }  
  1907.         if (returnStr.toString().length() > 0)  
  1908.             return returnStr.toString().substring(0, returnStr.toString().lastIndexOf(slipStr));  
  1909.         else  
  1910.             return "";  
  1911.     }  
  1912.       
  1913.     /** 
  1914.      * 获取从start开始用*替换len个长度后的字符串 
  1915.      *  
  1916.      * @param str 
  1917.      *            要替换的字符串 
  1918.      * @param start 
  1919.      *            开始位置 
  1920.      * @param len 
  1921.      *            长度 
  1922.      * @return 替换后的字符串 
  1923.      */  
  1924.     public static String getMaskStr(String str, int start, int len) {  
  1925.         if (StringUtil.isEmpty(str)) {  
  1926.             return str;  
  1927.         }  
  1928.         if (str.length() < start) {  
  1929.             return str;  
  1930.         }  
  1931.           
  1932.         // 获取*之前的字符串  
  1933.         String ret = str.substring(0, start);  
  1934.           
  1935.         // 获取最多能打的*个数  
  1936.         int strLen = str.length();  
  1937.         if (strLen < start + len) {  
  1938.             len = strLen - start;  
  1939.         }  
  1940.           
  1941.         // 替换成*  
  1942.         for (int i = 0; i < len; i++) {  
  1943.             ret += "*";  
  1944.         }  
  1945.           
  1946.         // 加上*之后的字符串  
  1947.         if (strLen > start + len) {  
  1948.             ret += str.substring(start + len);  
  1949.         }  
  1950.           
  1951.         return ret;  
  1952.     }  
  1953.       
  1954.     /** 
  1955.      * 根据传入的分割符号,把传入的字符串分割为List字符串 
  1956.      *  
  1957.      * @param slipStr 
  1958.      *            分隔的字符串 
  1959.      * @param src 
  1960.      *            字符串 
  1961.      * @return 列表 
  1962.      */  
  1963.     public static List<String> stringToStringListBySlipStr(String slipStr, String src) {  
  1964.           
  1965.         if (src == null)  
  1966.             return null;  
  1967.         List<String> list = new ArrayList<String>();  
  1968.         String[] result = src.split(slipStr);  
  1969.         for (int i = 0; i < result.length; i++) {  
  1970.             list.add(result[i]);  
  1971.         }  
  1972.         return list;  
  1973.     }  
  1974.       
  1975.     /** 
  1976.      * 截取字符串 
  1977.      *  
  1978.      * @param str 
  1979.      *            原始字符串 
  1980.      * @param len 
  1981.      *            要截取的长度 
  1982.      * @param tail 
  1983.      *            结束加上的后缀 
  1984.      * @return 截取后的字符串 
  1985.      */  
  1986.     public static String getHtmlSubString(String str, int len, String tail) {  
  1987.         if (str == null || str.length() <= len) {  
  1988.             return str;  
  1989.         }  
  1990.         int length = str.length();  
  1991.         char c = ' ';  
  1992.         String tag = null;  
  1993.         String name = null;  
  1994.         int size = 0;  
  1995.         String result = "";  
  1996.         boolean isTag = false;  
  1997.         List<String> tags = new ArrayList<String>();  
  1998.         int i = 0;  
  1999.         for (int end = 0, spanEnd = 0; i < length && len > 0; i++) {  
  2000.             c = str.charAt(i);  
  2001.             if (c == '<') {  
  2002.                 end = str.indexOf('>', i);  
  2003.             }  
  2004.               
  2005.             if (end > 0) {  
  2006.                 // 截取标签  
  2007.                 tag = str.substring(i, end + 1);  
  2008.                 int n = tag.length();  
  2009.                 if (tag.endsWith("/>")) {  
  2010.                     isTag = true;  
  2011.                 } else if (tag.startsWith("</")) { // 结束符  
  2012.                     name = tag.substring(2, end - i);  
  2013.                     size = tags.size() - 1;  
  2014.                     // 堆栈取出html开始标签  
  2015.                     if (size >= 0 && name.equals(tags.get(size))) {  
  2016.                         isTag = true;  
  2017.                         tags.remove(size);  
  2018.                     }  
  2019.                 } else { // 开始符  
  2020.                     spanEnd = tag.indexOf(' '0);  
  2021.                     spanEnd = spanEnd > 0 ? spanEnd : n;  
  2022.                     name = tag.substring(1, spanEnd);  
  2023.                     if (name.trim().length() > 0) {  
  2024.                         // 如果有结束符则为html标签  
  2025.                         spanEnd = str.indexOf("</" + name + ">", end);  
  2026.                         if (spanEnd > 0) {  
  2027.                             isTag = true;  
  2028.                             tags.add(name);  
  2029.                         }  
  2030.                     }  
  2031.                 }  
  2032.                 // 非html标签字符  
  2033.                 if (!isTag) {  
  2034.                     if (n >= len) {  
  2035.                         result += tag.substring(0, len);  
  2036.                         break;  
  2037.                     } else {  
  2038.                         len -= n;  
  2039.                     }  
  2040.                 }  
  2041.                   
  2042.                 result += tag;  
  2043.                 isTag = false;  
  2044.                 i = end;  
  2045.                 end = 0;  
  2046.             } else { // 非html标签字符  
  2047.                 len--;  
  2048.                 result += c;  
  2049.             }  
  2050.         }  
  2051.         // 添加未结束的html标签  
  2052.         for (String endTag : tags) {  
  2053.             result += "</" + endTag + ">";  
  2054.         }  
  2055.         if (i < length) {  
  2056.             result += tail;  
  2057.         }  
  2058.         return result;  
  2059.     }  
  2060.       
  2061.       
  2062. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值