Popular Posts
Enable SSL connection for Jsoup import org.jsoup.Connection; import org.jsoup.Jsoup; import javax.net.ssl.*; import java.io.IOException; import java.security.KeyManagement... Join related table and count rows or sum sql server 2005 count rows: SELECT * FROM Table1 LEFT JOIN     (SELECT Level1ID, COUNT(Level2ID) AS Total FROM Table2 GROUP BY Level1ID) AS... Append soap header into axis When axis generate classese of web service, it won't include the request of soap header. Here is a way to append soap header manually i...
Blog Archive
Stats
my string tool
package bruce.lib;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author NaNashi
 * 
 */
public class Strings {

    /**
     * 斷行
     */
    public static final String NEW_LINE = String.format("%n");

    /**
     * @param data
     *            文字類型物件
     * @return
     */
    public static boolean containsNullOrEmpty(CharSequence... data) {
        for (Object o : data) {
            if (o == null || o.toString().trim().length() == 0)
                return true;
        }
        return false;
    }

    /**
     * @param data
     * @return
     */
    public static boolean isNullOrEmpty(CharSequence data) {
        return data == null || data.toString().trim().length() == 0;
    }

    /**
     * @param <T>
     * @param array
     * @param seperator
     * @return
     */
    public static <T> String join(T[] array, String seperator) {
        StringBuilder sb = new StringBuilder();
        for (T t : array)
            sb.append(t.toString()).append(seperator);
        sb.delete(sb.length() - seperator.length(), sb.length());
        return sb.toString();
    }

    /**
     * @param s
     * @return
     */
    public static String MD5(CharSequence s) {
        char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
        byte[] strTemp = s.toString().getBytes();
        MessageDigest mdTemp = null;
        try {
            mdTemp = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        mdTemp.update(strTemp);
        byte[] md = mdTemp.digest();
        int j = md.length;
        char str[] = new char[j * 2];
        int k = 0;
        for (int i = 0; i < j; i++) {
            byte byte0 = md[i];
            str[k++] = hexDigits[byte0 >>> 4 & 0xf];
            str[k++] = hexDigits[byte0 & 0xf];
        }
        return new String(str);
    }

    /**
     * @param s
     *            參數字串
     * @return 處理SQL的參數,NULL時預設為空字串
     */
    public static String quote(CharSequence s) {
        return quote(s, false);
    }

    /**
     * @param s
     *            參數字串
     * @param alloweNull
     *            是否轉換NULL
     * @return
     */
    public static String quote(CharSequence s, boolean allowNull) {
        if (allowNull)
            return Strings.isNullOrEmpty(s) ? "null" : String.format("'%s'", s.toString().replaceAll("'", "''"));
        else
            return Strings.isNullOrEmpty(s) ? "''" : String.format("'%s'", s.toString().replaceAll("'", "''"));
    }

    /**
     * @param s
     * @return
     */
    public static String removeAllTags(CharSequence s) {
        return s.toString().replaceAll("<[^>]+>", "");
    }

    /**
     * @param s
     * @return
     */
    public static String trimTagSpaces(CharSequence s) {
        StringBuilder sb = new StringBuilder(s)    ;
        return s.toString().replaceAll("\n", "").replaceAll("\r", "").replaceAll(">\\s+<", "><");
    }

    /**
     * @param s
     * @return
     * @throws IOException
     */
    public static String decodeBase64(CharSequence s) throws IOException {
        return s == null ? null : new String((new sun.misc.BASE64Decoder()).decodeBuffer(s.toString()));
    }

    /**
     * @param s
     * @return
     */
    public static String encodeBase64(CharSequence s) {
        return s == null ? null : (new sun.misc.BASE64Encoder()).encode(s.toString().getBytes());
    }

    /**
     * @param s
     * @return
     */
    public static String NCR(CharSequence s) {
        StringBuilder sb = new StringBuilder();
        char[] ca = s.toString().toCharArray();
        for (char c : ca) {
            sb.append(String.format("&#x%s;", Integer.toHexString(c)));
        }
        return sb.toString();
    }

    public static String encodeURL(CharSequence s, String encoding) throws UnsupportedEncodingException {
        return URLEncoder.encode(s.toString(), encoding);
    }

    public static String encodeURL(CharSequence s) throws UnsupportedEncodingException {
        return encodeURL(s, "UTF-8");
    }

    public static String decodeURL(CharSequence s, String encoding) throws UnsupportedEncodingException {
        return URLDecoder.decode(s.toString(), encoding);
    }

    public static String decodeURL(CharSequence s) throws UnsupportedEncodingException {
        return decodeURL(s, "UTF-8");
    }
}