Java 經常使用字符串操做 StringUtil 封裝

/**
 * all rights reserved by zhanqiong, 2005
 */
package com.koubei.util;

import java.beans.XMLDecoder;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.opensymphony.util.TextUtils;

/**
 * @author chen
 * 
 */
public class StringUtil {
	private static Pattern numericPattern = Pattern.compile("^[0-9\\-]+$");
	private static Pattern numericStringPattern = Pattern
			.compile("^[0-9\\-\\-]+$");
	private static Pattern floatNumericPattern = Pattern
			.compile("^[0-9\\-\\.]+$");
	private static Pattern abcPattern = Pattern.compile("^[a-z|A-Z]+$");
	public static final String splitStrPattern = ",|,|;|;|、|\\.|。|-|_|\\(|\\)|\\[|\\]|\\{|\\}|\\\\|/| | |\"";
	private static Log logger = LogFactory.getLog(StringUtil.class);

	/**
	 * 判斷是否數字表示
	 * 
	 * @param src
	 *            源字符串
	 * @return 是否數字的標誌
	 */
	public static boolean isNumeric(String src) {
		boolean return_value = false;
		if (src != null && src.length() > 0) {
			Matcher m = numericPattern.matcher(src);
			if (m.find()) {
				return_value = true;
			}
		}
		return return_value;
	}

	/**
	 * 判斷是否數字表示
	 * 
	 * @param src
	 *            源字符串
	 * @return 是否數字的標誌
	 */
	public static boolean isNumericString(String src) {
		boolean return_value = false;
		if (src != null && src.length() > 0) {
			Matcher m = numericStringPattern.matcher(src);
			if (m.find()) {
				return_value = true;
			}
		}
		return return_value;
	}

	/**
	 * 判斷是否純字母組合
	 * 
	 * @param src
	 *            源字符串
	 * @return 是否純字母組合的標誌
	 */
	public static boolean isABC(String src) {
		boolean return_value = false;
		if (src != null && src.length() > 0) {
			Matcher m = abcPattern.matcher(src);
			if (m.find()) {
				return_value = true;
			}
		}
		return return_value;
	}

	/**
	 * 判斷是否浮點數字表示
	 * 
	 * @param src
	 *            源字符串
	 * @return 是否數字的標誌
	 */
	public static boolean isFloatNumeric(String src) {
		boolean return_value = false;
		if (src != null && src.length() > 0) {
			Matcher m = floatNumericPattern.matcher(src);
			if (m.find()) {
				return_value = true;
			}
		}
		return return_value;
	}

	/**
	 * 把string array or list用給定的符號symbol鏈接成一個字符串
	 * 
	 * @param array
	 * @param symbol
	 * @return
	 */
	public static String joinString(List array, String symbol) {
		String result = "";
		if (array != null) {
			for (int i = 0; i < array.size(); i++) {
				String temp = array.get(i).toString();
				if (temp != null && temp.trim().length() > 0)
					result += (temp + symbol);
			}
			if (result.length() > 1)
				result = result.substring(0, result.length() - 1);
		}
		return result;
	}

	public static String subStringNotEncode(String subject, int size) {
		if (subject != null && subject.length() > size) {
			subject = subject.substring(0, size) + "...";
		}
		return subject;
	}

	public static String subString(String subject, int size) {
		subject = TextUtils.htmlEncode(subject);
		if (subject.length() > size) {
			subject = subject.substring(0, size) + "...";
		}
		return subject;
	}

	/**
	 * 截取字符串 超出的字符用symbol代替   
	 * 
	 * @param len
	 *             字符串長度 長度計量單位爲一個GBK漢字  兩個英文字母計算爲一個單位長度
	 * @param str
	 * @param symbol
	 * @return
	 */
	public static String getLimitLengthString(String str, int len, String symbol) {
		int iLen = len * 2;
		int counterOfDoubleByte = 0;
		String strRet = "";
		try {
			if (str != null) {
				byte[] b = str.getBytes("GBK");
				if (b.length <= iLen) {
					return str;
				}
				for (int i = 0; i < iLen; i++) {
					if (b[i] < 0) {
						counterOfDoubleByte++;
					}
				}
				if (counterOfDoubleByte % 2 == 0) {
					strRet = new String(b, 0, iLen, "GBK") + symbol;
					return strRet;
				} else {
					strRet = new String(b, 0, iLen - 1, "GBK") + symbol;
					return strRet;
				}
			} else {
				return "";
			}
		} catch (Exception ex) {
			return str.substring(0, len);
		} finally {
			strRet = null;
		}
	}

	/**
	 * 截取字符串 超出的字符用symbol代替   
	 * 
	 * @param len
	 *             字符串長度 長度計量單位爲一個GBK漢字  兩個英文字母計算爲一個單位長度
	 * @param str
	 * @param symbol
	 * @return12
	 */
	public static String getLimitLengthString(String str, int len) {
		return getLimitLengthString(str, len, "...");
	}

	public static String subStr(String subject, int size) {
		subject = TextUtils.htmlEncode(subject);
		if (subject.length() > size) {
			subject = subject.substring(0, size);
		}
		return subject;
	}

	/**
	 * 
	 * 截取字符,不轉碼
	 * 
	 * @param subject
	 * @param size
	 * @return
	 */
	public static String subStrNotEncode(String subject, int size) {
		if (subject.length() > size) {
			subject = subject.substring(0, size);
		}
		return subject;
	}

	/**
	 * 把string array or list用給定的符號symbol鏈接成一個字符串
	 * 
	 * @param array
	 * @param symbol
	 * @return
	 */
	public static String joinString(String[] array, String symbol) {
		String result = "";
		if (array != null) {
			for (int i = 0; i < array.length; i++) {
				String temp = array[i];
				if (temp != null && temp.trim().length() > 0)
					result += (temp + symbol);
			}
			if (result.length() > 1)
				result = result.substring(0, result.length() - 1);
		}
		return result;
	}

	/**
	 * 取得字符串的實際長度(考慮了漢字的狀況)
	 * 
	 * @param SrcStr
	 *            源字符串
	 * @return 字符串的實際長度
	 */
	public static int getStringLen(String SrcStr) {
		int return_value = 0;
		if (SrcStr != null) {
			char[] theChars = SrcStr.toCharArray();
			for (int i = 0; i < theChars.length; i++) {
				return_value += (theChars[i] <= 255) ? 1 : 2;
			}
		}
		return return_value;
	}

	/**
	 * 檢查聯繫人信息是否填寫,電話,手機,email必須填至少一個,email填了的話檢查格式
	 * 
	 * @param phoneCity
	 * @param phoneNumber
	 * @param phoneExt
	 * @param mobileNumber
	 * @param email
	 * @return
	 */
	public static boolean checkContactInfo(String phoneCity,
			String phoneNumber, String phoneExt, String mobileNumber,
			String email) {
		String result = (phoneCity == null ? "" : phoneCity.trim())
				+ (phoneNumber == null ? "" : phoneNumber.trim())
				+ (phoneExt == null ? "" : phoneExt.trim())
				+ (mobileNumber == null ? "" : mobileNumber.trim())
				+ (email == null ? "" : email.trim());
		if (result.length() < 1)
			return false;
		if (!isEmail(email))
			return false;

		return true;
	}

	/**
	 * 檢查數據串中是否包含非法字符集
	 * 
	 * @param str
	 * @return [true]|[false] 包含|不包含
	 */
	public static boolean check(String str) {
		String sIllegal = "'\"";
		int len = sIllegal.length();
		if (null == str)
			return false;
		for (int i = 0; i < len; i++) {
			if (str.indexOf(sIllegal.charAt(i)) != -1)
				return true;
		}

		return false;
	}

	/***************************************************************************
	 * getHideEmailPrefix - 隱藏郵件地址前綴。
	 * 
	 * @param email
	 *            - EMail郵箱地址 例如: linwenguo@koubei.com 等等...
	 * @return 返回已隱藏前綴郵件地址, 如 *********@koubei.com.
	 * @version 1.0 (2006.11.27) Wilson Lin
	 **************************************************************************/
	public static String getHideEmailPrefix(String email) {
		if (null != email) {
			int index = email.lastIndexOf('@');
			if (index > 0) {
				email = repeat("*", index).concat(email.substring(index));
			}
		}
		return email;
	}

	/***************************************************************************
	 * repeat - 經過源字符串重複生成N次組成新的字符串。
	 * 
	 * @param src
	 *            - 源字符串 例如: 空格(" "), 星號("*"), "浙江" 等等...
	 * @param num
	 *            - 重複生成次數
	 * @return 返回已生成的重複字符串
	 * @version 1.0 (2006.10.10) Wilson Lin
	 **************************************************************************/
	public static String repeat(String src, int num) {
		StringBuffer s = new StringBuffer();
		for (int i = 0; i < num; i++)
			s.append(src);
		return s.toString();
	}

	/**
	 * 檢查是不是email, when null or '' return true
	 * 
	 * @param email
	 * @return
	 */
	public static boolean isEmail(String email) {
		if (email != null && email.trim().length() > 0) {
			if (!TextUtils.verifyEmail(email)) {
				return false;
			} else {
				return true;
			}
		}
		return false;
	}

	/**
	 * 根據指定的字符把源字符串分割成一個數組
	 * 
	 * @param src
	 * @return
	 */
	public static List<String> parseString2ListByCustomerPattern(
			String pattern, String src) {

		if (src == null)
			return null;
		List<String> list = new ArrayList<String>();
		String[] result = src.split(pattern);
		for (int i = 0; i < result.length; i++) {
			list.add(result[i]);
		}
		return list;
	}

	/**
	 * 根據指定的字符把源字符串分割成一個數組
	 * 
	 * @param src
	 * @return
	 */
	public static List<String> parseString2ListByPattern(String src) {
		String pattern = ",|,|、|。";
		return parseString2ListByCustomerPattern(pattern, src);
	}

	/**
	 * 格式化一個float
	 * 
	 * @param format
	 *            要格式化成的格式 such as #.00, #.#
	 */

	public static String formatFloat(float f, String format) {
		DecimalFormat df = new DecimalFormat(format);
		return df.format(f);
	}

	/**
	 * 判斷是不是空字符串 null和"" 都返回 true
	 * 
	 * @author Robin Chang
	 * @param s
	 * @return
	 */
	public static boolean isEmpty(String s) {
		if (s != null && !s.equals("")) {
			return false;
		}
		return true;
	}

	/**
	 * 自定義的分隔字符串函數 例如: 1,2,3 =>[1,2,3] 3個元素 ,2,3=>[,2,3] 3個元素 ,2,3,=>[,2,3,]
	 * 4個元素 ,,,=>[,,,] 4個元素
	 * 
	 * 5.22算法修改,爲提升速度不用正則表達式 兩個間隔符,,返回""元素
	 * 
	 * @param split
	 *            分割字符 默認,
	 * @param src
	 *            輸入字符串
	 * @return 分隔後的list
	 * @author Robin
	 */
	public static List<String> splitToList(String split, String src) {
		// 默認,
		String sp = ",";
		if (split != null && split.length() == 1) {
			sp = split;
		}
		List<String> r = new ArrayList<String>();
		int lastIndex = -1;
		int index = src.indexOf(sp);
		if (-1 == index && src != null) {
			r.add(src);
			return r;
		}
		while (index >= 0) {
			if (index > lastIndex) {
				r.add(src.substring(lastIndex + 1, index));
			} else {
				r.add("");
			}

			lastIndex = index;
			index = src.indexOf(sp, index + 1);
			if (index == -1) {
				r.add(src.substring(lastIndex + 1, src.length()));
			}
		}
		return r;
	}

	/**
	 * 把 名=值 參數錶轉換成字符串 (a=1,b=2 =>a=1&b=2)
	 * 
	 * @param map
	 * @return
	 */
	public static String linkedHashMapToString(LinkedHashMap<String, String> map) {
		if (map != null && map.size() > 0) {
			String result = "";
			Iterator it = map.keySet().iterator();
			while (it.hasNext()) {
				String name = (String) it.next();
				String value = (String) map.get(name);
				result += (result.equals("")) ? "" : "&";
				result += String.format("%s=%s", name, value);
			}
			return result;
		}
		return null;
	}

	/**
	 * 解析字符串返回 名稱=值的參數表 (a=1&b=2 => a=1,b=2)
	 * 
	 * @see test.koubei.util.StringUtilTest#testParseStr()
	 * @param str
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static LinkedHashMap<String, String> toLinkedHashMap(String str) {
		if (str != null && !str.equals("") && str.indexOf("=") > 0) {
			LinkedHashMap result = new LinkedHashMap();

			String name = null;
			String value = null;
			int i = 0;
			while (i < str.length()) {
				char c = str.charAt(i);
				switch (c) {
				case 61: // =
					value = "";
					break;
				case 38: // &
					if (name != null && value != null && !name.equals("")) {
						result.put(name, value);
					}
					name = null;
					value = null;
					break;
				default:
					if (value != null) {
						value = (value != null) ? (value + c) : "" + c;
					} else {
						name = (name != null) ? (name + c) : "" + c;
					}
				}
				i++;

			}

			if (name != null && value != null && !name.equals("")) {
				result.put(name, value);
			}

			return result;

		}
		return null;
	}

	/**
	 * 根據輸入的多個解釋和下標返回一個值
	 * 
	 * @param captions
	 *            例如:"無,愛乾淨,通常,比較亂"
	 * @param index
	 *            1
	 * @return 通常
	 */
	public static String getCaption(String captions, int index) {
		if (index > 0 && captions != null && !captions.equals("")) {
			String[] ss = captions.split(",");
			if (ss != null && ss.length > 0 && index < ss.length) {
				return ss[index];
			}
		}
		return null;
	}

	/**
	 * 數字轉字符串,若是num<=0 則輸出"";
	 * 
	 * @param num
	 * @return
	 */
	public static String numberToString(Object num) {
		if (num == null) {
			return null;
		} else if (num instanceof Integer && (Integer) num > 0) {
			return Integer.toString((Integer) num);
		} else if (num instanceof Long && (Long) num > 0) {
			return Long.toString((Long) num);
		} else if (num instanceof Float && (Float) num > 0) {
			return Float.toString((Float) num);
		} else if (num instanceof Double && (Double) num > 0) {
			return Double.toString((Double) num);
		} else {
			return "";
		}
	}

	/**
	 * 貨幣轉字符串
	 * 
	 * @param money
	 * @param style
	 *            樣式 [default]要格式化成的格式 such as #.00, #.#
	 * @return
	 */

	public static String moneyToString(Object money, String style) {
		if (money != null && style != null
				&& (money instanceof Double || money instanceof Float)) {
			Double num = (Double) money;

			if (style.equalsIgnoreCase("default")) {
				// 缺省樣式 0 不輸出 ,若是沒有輸出小數位則不輸出.0
				if (num == 0) {
					// 不輸出0
					return "";
				} else if ((num * 10 % 10) == 0) {
					// 沒有小數
					return Integer.toString((int) num.intValue());
				} else {
					// 有小數
					return num.toString();
				}

			} else {
				DecimalFormat df = new DecimalFormat(style);
				return df.format(num);
			}
		}
		return null;
	}

	/**
	 * 在sou中是否存在finds 若是指定的finds字符串有一個在sou中找到,返回true;
	 * 
	 * @param sou
	 * @param find
	 * @return
	 */
	public static boolean strPos(String sou, String... finds) {
		if (sou != null && finds != null && finds.length > 0) {
			for (int i = 0; i < finds.length; i++) {
				if (sou.indexOf(finds[i]) > -1)
					return true;
			}
		}
		return false;
	}

	public static boolean strPos(String sou, List<String> finds) {
		if (sou != null && finds != null && finds.size() > 0) {
			for (String s : finds) {
				if (sou.indexOf(s) > -1)
					return true;
			}
		}
		return false;
	}

	public static boolean strPos(String sou, String finds) {
		List<String> t = splitToList(",", finds);
		return strPos(sou, t);
	}

	/**
	 * 判斷兩個字符串是否相等 若是都爲null則判斷爲相等,一個爲null另外一個not null則判斷不相等 不然若是s1=s2則相等
	 * 
	 * @param s1
	 * @param s2
	 * @return
	 */
	public static boolean equals(String s1, String s2) {
		if (StringUtil.isEmpty(s1) && StringUtil.isEmpty(s2)) {
			return true;
		} else if (!StringUtil.isEmpty(s1) && !StringUtil.isEmpty(s2)) {
			return s1.equals(s2);
		}
		return false;
	}

	public static int toInt(String s) {
		if (s != null && !"".equals(s.trim())) {
			try {
				return Integer.parseInt(s);
			} catch (Exception e) {
				return 0;
			}
		}
		return 0;
	}

	public static double toDouble(String s) {
		if (s != null && !"".equals(s.trim())) {
			return Double.parseDouble(s);
		}
		return 0;
	}

	public static boolean isPhone(String phone) {
		if (phone == null && "".equals(phone)) {
			return false;
		}
		String[] strPhone = phone.split("-");
		try {
			for (int i = 0; i < strPhone.length; i++) {
				Long.parseLong(strPhone[i]);
			}

		} catch (Exception e) {
			return false;
		}
		return true;

	}

	/**
	 * 把xml 轉爲object
	 * 
	 * @param xml
	 * @return
	 */
	public static Object xmlToObject(String xml) {
		try {
			ByteArrayInputStream in = new ByteArrayInputStream(xml
					.getBytes("UTF8"));
			XMLDecoder decoder = new XMLDecoder(new BufferedInputStream(in));
			return decoder.readObject();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 按規定長度截斷字符串,沒有...的省略符號
	 * 
	 * @param subject
	 * @param size
	 * @return
	 */
	public static String subStringNoEllipsis(String subject, int size) {
		subject = TextUtils.htmlEncode(subject);
		if (subject.length() > size) {
			subject = subject.substring(0, size);
		}
		return subject;
	}

	public static long toLong(String s) {
		try {
			if (s != null && !"".equals(s.trim()))
				return Long.parseLong(s);
		} catch (Exception exception) {
		}
		return 0L;
	}

	public static String simpleEncrypt(String str) {
		if (str != null && str.length() > 0) {
			// str = str.replaceAll("0","a");
			str = str.replaceAll("1", "b");
			// str = str.replaceAll("2","c");
			str = str.replaceAll("3", "d");
			// str = str.replaceAll("4","e");
			str = str.replaceAll("5", "f");
			str = str.replaceAll("6", "g");
			str = str.replaceAll("7", "h");
			str = str.replaceAll("8", "i");
			str = str.replaceAll("9", "j");
		}
		return str;

	}

	/**
	 * 過濾用戶輸入的URL地址(防治用戶廣告) 目前只針對以http或www開頭的URL地址
	 * 本方法調用的正則表達式,不建議用在對性能嚴格的地方例如:循環及list頁面等
	 * 
	 * @author fengliang
	 * @param str
	 *            須要處理的字符串
	 * @return 返回處理後的字符串
	 */
	public static String removeURL(String str) {
		if (str != null)
			str = str.toLowerCase()
					.replaceAll("(http|www|com|cn|org|\\.)+", "");
		return str;
	}

	/**
	 * 隨即生成指定位數的含數字驗證碼字符串
	 * 
	 * @author Peltason
	 * @date 2007-5-9
	 * @param bit
	 *            指定生成驗證碼位數
	 * @return String
	 */
	public static String numRandom(int bit) {
		if (bit == 0)
			bit = 6; // 默認6位
		String str = "";
		str = "0123456789";// 初始化種子
		return RandomStringUtils.random(bit, str);// 返回6位的字符串
	}

	/**
	 * 隨即生成指定位數的含驗證碼字符串
	 * 
	 * @author Peltason
	 * @date 2007-5-9
	 * @param bit
	 *            指定生成驗證碼位數
	 * @return String
	 */
	public static String random(int bit) {
		if (bit == 0)
			bit = 6; // 默認6位
		// 由於o和0,l和1很難區分,因此,去掉大小寫的o和l
		String str = "";
		str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz";// 初始化種子
		return RandomStringUtils.random(bit, str);// 返回6位的字符串
	}

	/**
	 * 檢查字符串是否屬於手機號碼
	 * 
	 * @author Peltason
	 * @date 2007-5-9
	 * @param str
	 * @return boolean
	 */
	public static boolean isMobile(String str) {
		if (str == null || str.equals(""))
			return false;
		if (str.length() != 11 || !isNumeric(str))
			return false;
		if (!str.substring(0, 2).equals("13")
				&& !str.substring(0, 2).equals("15")
				&& !str.substring(0, 2).equals("18"))
			return false;
		return true;
	}

	/**
	 * Wap頁面的非法字符檢查
	 * 
	 * @author hugh115
	 * @date 2007-06-29
	 * @param str
	 * @return
	 */
	public static String replaceWapStr(String str) {
		if (str != null) {
			str = str.replaceAll("<span class=\"keyword\">", "");
			str = str.replaceAll("</span>", "");
			str = str.replaceAll("<strong class=\"keyword\">", "");
			str = str.replaceAll("<strong>", "");
			str = str.replaceAll("</strong>", "");

			str = str.replace('$', '$');

			str = str.replaceAll("&", "&");
			str = str.replace('&', '&');

			str = str.replace('<', '<');

			str = str.replace('>', '>');

		}
		return str;
	}

	/**
	 * 字符串轉float 若是異常返回0.00
	 * 
	 * @param s
	 *            輸入的字符串
	 * @return 轉換後的float
	 */
	public static Float toFloat(String s) {
		try {
			return Float.parseFloat(s);
		} catch (NumberFormatException e) {
			return new Float(0);
		}
	}

	/**
	 * 頁面中去除字符串中的空格、回車、換行符、製表符
	 * 
	 * @author shazao
	 * @date 2007-08-17
	 * @param str
	 * @return
	 */
	public static String replaceBlank(String str) {
		if (str != null) {
			Pattern p = Pattern.compile("\\s*|\t|\r|\n");
			Matcher m = p.matcher(str);
			str = m.replaceAll("");
		}
		return str;
	}

	/**
	 * 全角生成半角
	 * 
	 * @author bailong
	 * @date 2007-08-29
	 * @param str
	 * @return
	 */
	public static String Q2B(String QJstr) {
		String outStr = "";
		String Tstr = "";
		byte[] b = null;
		for (int i = 0; i < QJstr.length(); i++) {
			try {
				Tstr = QJstr.substring(i, i + 1);
				b = Tstr.getBytes("unicode");
			} catch (java.io.UnsupportedEncodingException e) {
				if (logger.isErrorEnabled()) {
					logger.error(e);
				}
			}
			if (b[3] == -1) {
				b[2] = (byte) (b[2] + 32);
				b[3] = 0;
				try {
					outStr = outStr + new String(b, "unicode");
				} catch (java.io.UnsupportedEncodingException ex) {
					if (logger.isErrorEnabled()) {
						logger.error(ex);
					}
				}
			} else {
				outStr = outStr + Tstr;
			}
		}
		return outStr;
	}

	/**
	 * 
	 * 轉換編碼
	 * 
	 * @param s
	 *            源字符串
	 * @param fencode
	 *            源編碼格式
	 * @param bencode
	 *            目標編碼格式
	 * @return 目標編碼
	 */
	public static String changCoding(String s, String fencode, String bencode) {
		try {
			String str = new String(s.getBytes(fencode), bencode);
			return str;
		} catch (UnsupportedEncodingException e) {
			return s;
		}
	}

	/**
	 * *************************************************************************
	 * 修改:劉黎明 修改時間:2007/3/1
	 * 
	 * @param str
	 * @return
	 ************************************************************************* 
	 */
	public static String removeHTMLLableExe(String str) {
		str = stringReplace(str, ">\\s*<", "><");
		str = stringReplace(str, " ", " ");// 替換空格
		str = stringReplace(str, "<br ?/?>", "\n");// 去<br><br />
		str = stringReplace(str, "<([^<>]+)>", "");// 去掉<>內的字符
		str = stringReplace(str, "\\s\\s\\s*", " ");// 將多個空白變成一個空格
		str = stringReplace(str, "^\\s*", "");// 去掉頭的空白
		str = stringReplace(str, "\\s*$", "");// 去掉尾的空白
		str = stringReplace(str, " +", " ");
		return str;
	}

	/**
	 * 除去html標籤
	 * 
	 * @param str
	 *            源字符串
	 * @return 目標字符串
	 */
	public static String removeHTMLLable(String str) {
		str = stringReplace(str, "\\s", "");// 去掉頁面上看不到的字符
		str = stringReplace(str, "<br ?/?>", "\n");// 去<br><br />
		str = stringReplace(str, "<([^<>]+)>", "");// 去掉<>內的字符
		str = stringReplace(str, " ", " ");// 替換空格
		str = stringReplace(str, "&(\\S)(\\S?)(\\S?)(\\S?);", "");// 去<br><br />
		return str;
	}

	/**
	 * 去掉HTML標籤以外的字符串
	 * 
	 * @param str
	 *            源字符串
	 * @return 目標字符串
	 */
	public static String removeOutHTMLLable(String str) {
		str = stringReplace(str, ">([^<>]+)<", "><");
		str = stringReplace(str, "^([^<>]+)<", "<");
		str = stringReplace(str, ">([^<>]+)$", ">");
		return str;
	}

	/**
	 * 
	 * 字符串替換
	 * 
	 * @param str
	 *            源字符串
	 * @param sr
	 *            正則表達式樣式
	 * @param sd
	 *            替換文本
	 * @return 結果串
	 */
	public static String stringReplace(String str, String sr, String sd) {
		String regEx = sr;
		Pattern p = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
		Matcher m = p.matcher(str);
		str = m.replaceAll(sd);
		return str;
	}

	/**
	 * 
	 * 將html的省略寫法替換成非省略寫法
	 * 
	 * @param str
	 *            html字符串
	 * @param pt
	 *            標籤如table
	 * @return 結果串
	 */
	public static String fomateToFullForm(String str, String pt) {
		String regEx = "<" + pt + "\\s+([\\S&&[^<>]]*)/>";
		Pattern p = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
		Matcher m = p.matcher(str);
		String[] sa = null;
		String sf = "";
		String sf2 = "";
		String sf3 = "";
		for (; m.find();) {
			sa = p.split(str);
			if (sa == null) {
				break;
			}
			sf = str.substring(sa[0].length(), str
					.indexOf("/>", sa[0].length()));
			sf2 = sf + "></" + pt + ">";
			sf3 = str.substring(sa[0].length() + sf.length() + 2);
			str = sa[0] + sf2 + sf3;
			sa = null;
		}
		return str;
	}

	/**
	 * 
	 * 獲得字符串的子串位置序列
	 * 
	 * @param str
	 *            字符串
	 * @param sub
	 *            子串
	 * @param b
	 *            true子串前端,false子串後端
	 * @return 字符串的子串位置序列
	 */
	public static int[] getSubStringPos(String str, String sub, boolean b) {
		// int[] i = new int[(new Integer((str.length()-stringReplace( str , sub
		// , "" ).length())/sub.length())).intValue()] ;
		String[] sp = null;
		int l = sub.length();
		sp = splitString(str, sub);
		if (sp == null) {
			return null;
		}
		int[] ip = new int[sp.length - 1];
		for (int i = 0; i < sp.length - 1; i++) {
			ip[i] = sp[i].length() + l;
			if (i != 0) {
				ip[i] += ip[i - 1];
			}
		}
		if (b) {
			for (int j = 0; j < ip.length; j++) {
				ip[j] = ip[j] - l;
			}
		}
		return ip;
	}

	/**
	 * 
	 * 根據正則表達式分割字符串
	 * 
	 * @param str
	 *            源字符串
	 * @param ms
	 *            正則表達式
	 * @return 目標字符串組
	 */
	public static String[] splitString(String str, String ms) {
		String regEx = ms;
		Pattern p = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
		String[] sp = p.split(str);
		return sp;
	}

	/**
	 * *************************************************************************
	 * 根據正則表達式提取字符串,相同的字符串只返回一個
	 * 
	 * @author 劉黎明
	 * @param str
	 *            源字符串
	 * @param pattern
	 *            正則表達式
	 * @return 目標字符串數據組
	 ************************************************************************* 
	 */

	// ★傳入一個字符串,把符合pattern格式的字符串放入字符串數組
	// java.util.regex是一個用正則表達式所訂製的模式來對字符串進行匹配工做的類庫包
	public static String[] getStringArrayByPattern(String str, String pattern) {
		Pattern p = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
		Matcher matcher = p.matcher(str);
		// 範型
		Set<String> result = new HashSet<String>();// 目的是:相同的字符串只返回一個。。。 不重複元素
		// boolean find() 嘗試在目標字符串裏查找下一個匹配子串。
		while (matcher.find()) {
			for (int i = 0; i < matcher.groupCount(); i++) { // int groupCount()
																// 返回當前查找所得到的匹配組的數量。
				// System.out.println(matcher.group(i));
				result.add(matcher.group(i));

			}
		}
		String[] resultStr = null;
		if (result.size() > 0) {
			resultStr = new String[result.size()];
			return result.toArray(resultStr);// 將Set result轉化爲String[] resultStr
		}
		return resultStr;

	}

	/**
	 * 獲得第一個b,e之間的字符串,並返回e後的子串
	 * 
	 * @param s
	 *            源字符串
	 * @param b
	 *            標誌開始
	 * @param e
	 *            標誌結束
	 * @return b,e之間的字符串
	 */

	/*
	 * String aaa="abcdefghijklmn"; String[] bbb=StringProcessor.midString(aaa,
	 * "b","l"); System.out.println("bbb[0]:"+bbb[0]);//cdefghijk
	 * System.out.println("bbb[1]:"+bbb[1]);//lmn
	 * ★這個方法是獲得第二個參數和第三個參數之間的字符串,賦給元素0;而後把元素0表明的字符串以後的,賦給元素1
	 */

	/*
	 * String aaa="abcdefgllhijklmn5465"; String[]
	 * bbb=StringProcessor.midString(aaa, "b","l"); //ab cdefg llhijklmn5465 //
	 * 元素0 元素1
	 */
	public static String[] midString(String s, String b, String e) {
		int i = s.indexOf(b) + b.length();
		int j = s.indexOf(e, i);
		String[] sa = new String[2];
		if (i < b.length() || j < i + 1 || i > j) {
			sa[1] = s;
			sa[0] = null;
			return sa;
		} else {
			sa[0] = s.substring(i, j);
			sa[1] = s.substring(j);
			return sa;
		}
	}

	/**
	 * 帶有前一次替代序列的正則表達式替代
	 * 
	 * @param s
	 * @param pf
	 * @param pb
	 * @param start
	 * @return
	 */
	public static String stringReplace(String s, String pf, String pb, int start) {
		Pattern pattern_hand = Pattern.compile(pf);
		Matcher matcher_hand = pattern_hand.matcher(s);
		int gc = matcher_hand.groupCount();
		int pos = start;
		String sf1 = "";
		String sf2 = "";
		String sf3 = "";
		int if1 = 0;
		String strr = "";
		while (matcher_hand.find(pos)) {
			sf1 = matcher_hand.group();
			if1 = s.indexOf(sf1, pos);
			if (if1 >= pos) {
				strr += s.substring(pos, if1);
				pos = if1 + sf1.length();
				sf2 = pb;
				for (int i = 1; i <= gc; i++) {
					sf3 = "\\" + i;
					sf2 = replaceAll(sf2, sf3, matcher_hand.group(i));
				}
				strr += sf2;
			} else {
				return s;
			}
		}
		strr = s.substring(0, start) + strr;
		return strr;
	}

	/**
	 * 存文本替換
	 * 
	 * @param s
	 *            源字符串
	 * @param sf
	 *            子字符串
	 * @param sb
	 *            替換字符串
	 * @return 替換後的字符串
	 */
	public static String replaceAll(String s, String sf, String sb) {
		int i = 0, j = 0;
		int l = sf.length();
		boolean b = true;
		boolean o = true;
		String str = "";
		do {
			j = i;
			i = s.indexOf(sf, j);
			if (i > j) {
				str += s.substring(j, i);
				str += sb;
				i += l;
				o = false;
			} else {
				str += s.substring(j);
				b = false;
			}
		} while (b);
		if (o) {
			str = s;
		}
		return str;
	}

	/**
	 * 判斷是否與給定字符串樣式匹配
	 * 
	 * @param str
	 *            字符串
	 * @param pattern
	 *            正則表達式樣式
	 * @return 是否匹配是true,否false
	 */
	public static boolean isMatch(String str, String pattern) {
		Pattern pattern_hand = Pattern.compile(pattern);
		Matcher matcher_hand = pattern_hand.matcher(str);
		boolean b = matcher_hand.matches();
		return b;
	}

	/**
	 * 截取字符串
	 * 
	 * @param s
	 *            源字符串
	 * @param jmp
	 *            跳過jmp
	 * @param sb
	 *            取在sb
	 * @param se
	 *            於se
	 * @return 之間的字符串
	 */
	public static String subStringExe(String s, String jmp, String sb, String se) {
		if (isEmpty(s)) {
			return "";
		}
		int i = s.indexOf(jmp);
		if (i >= 0 && i < s.length()) {
			s = s.substring(i + 1);
		}
		i = s.indexOf(sb);
		if (i >= 0 && i < s.length()) {
			s = s.substring(i + 1);
		}
		if (se == "") {
			return s;
		} else {
			i = s.indexOf(se);
			if (i >= 0 && i < s.length()) {
				s = s.substring(i + 1);
			}
			return s;
		}
	}

	/**
	 * *************************************************************************
	 * 用要經過URL傳輸的內容進行編碼
	 * 
	 * @author 劉黎明
	 * @param 源字符串
	 * @return 通過編碼的內容
	 ************************************************************************* 
	 */
	public static String URLEncode(String src) {
		String return_value = "";
		try {
			if (src != null) {
				return_value = URLEncoder.encode(src, "GBK");

			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return_value = src;
		}

		return return_value;
	}

	/**
	 * *************************************************************************
	 * 
	 * @author 李鋒 2007.4.18
	 * @param 傳入
	 *            福test照樓(北大門總
	 *            ;店)福
	 * @return 通過解碼的內容
	 ************************************************************************* 
	 */
	public static String getGBK(String str) {

		return transfer(str);
	}

	public static String transfer(String str) {
		Pattern p = Pattern.compile("&#\\d+;");
		Matcher m = p.matcher(str);
		while (m.find()) {
			String old = m.group();
			str = str.replaceAll(old, getChar(old));
		}
		return str;
	}

	public static String getChar(String str) {
		String dest = str.substring(2, str.length() - 1);
		char ch = (char) Integer.parseInt(dest);
		return "" + ch;
	}

	/**
	 * yahoo首頁中切割字符串.
	 * 
	 * @author yxg
	 * @date 2007-09-17
	 * @param str
	 * @return
	 */
	public static String subYhooString(String subject, int size) {
		subject = subject.substring(1, size);
		return subject;
	}

	public static String subYhooStringDot(String subject, int size) {
		subject = subject.substring(1, size) + "...";
		return subject;
	}

	/**
	 * 泛型方法(通用),把list轉換成以「,」相隔的字符串 調用時注意類型初始化(申明類型) 如:List<Integer> intList =
	 * new ArrayList<Integer>(); 調用方法:StringUtil.listTtoString(intList);
	 * 效率:list中4條信息,1000000次調用時間爲850ms左右
	 * 
	 * @author fengliang
	 * @serialData 2008-01-09
	 * @param <T>
	 *            泛型
	 * @param list
	 *            list列表
	 * @return 以「,」相隔的字符串
	 */
	public static <T> String listTtoString(List<T> list) {
		if (list == null || list.size() < 1)
			return "";
		Iterator<T> i = list.iterator();
		if (!i.hasNext())
			return "";
		StringBuilder sb = new StringBuilder();
		for (;;) {
			T e = i.next();
			sb.append(e);
			if (!i.hasNext())
				return sb.toString();
			sb.append(",");
		}
	}

	/**
	 * 把整形數組轉換成以「,」相隔的字符串
	 * 
	 * @author fengliang
	 * @serialData 2008-01-08
	 * @param a
	 *            數組a
	 * @return 以「,」相隔的字符串
	 */
	public static String intArraytoString(int[] a) {
		if (a == null)
			return "";
		int iMax = a.length - 1;
		if (iMax == -1)
			return "";
		StringBuilder b = new StringBuilder();
		for (int i = 0;; i++) {
			b.append(a[i]);
			if (i == iMax)
				return b.toString();
			b.append(",");
		}
	}

	/**
	 * 判斷文字內容重複
	 * 
	 * @author 沙棗
	 * @Date 2008-04-17
	 */
	public static boolean isContentRepeat(String content) {
		int similarNum = 0;
		int forNum = 0;
		int subNum = 0;
		int thousandNum = 0;
		String startStr = "";
		String nextStr = "";
		boolean result = false;
		float endNum = (float) 0.0;
		if (content != null && content.length() > 0) {
			if (content.length() % 1000 > 0)
				thousandNum = (int) Math.floor(content.length() / 1000) + 1;
			else
				thousandNum = (int) Math.floor(content.length() / 1000);
			if (thousandNum < 3)
				subNum = 100 * thousandNum;
			else if (thousandNum < 6)
				subNum = 200 * thousandNum;
			else if (thousandNum < 9)
				subNum = 300 * thousandNum;
			else
				subNum = 3000;
			for (int j = 1; j < subNum; j++) {
				if (content.length() % j > 0)
					forNum = (int) Math.floor(content.length() / j) + 1;
				else
					forNum = (int) Math.floor(content.length() / j);
				if (result || j >= content.length())
					break;
				else {
					for (int m = 0; m < forNum; m++) {
						if (m * j > content.length()
								|| (m + 1) * j > content.length()
								|| (m + 2) * j > content.length())
							break;
						startStr = content.substring(m * j, (m + 1) * j);
						nextStr = content.substring((m + 1) * j, (m + 2) * j);
						if (startStr.equals(nextStr)) {
							similarNum = similarNum + 1;
							endNum = (float) similarNum / forNum;
							if (endNum > 0.4) {
								result = true;
								break;
							}
						} else
							similarNum = 0;
					}
				}
			}
		}
		return result;
	}

	/**
	 * Ascii轉爲Char
	 * 
	 * @author 甜瓜
	 * @param asc
	 * @return
	 */
	public static String AsciiToChar(int asc) {
		String TempStr = "A";
		char tempchar = (char) asc;
		TempStr = String.valueOf(tempchar);
		return TempStr;
	}

	/**
	 * 判斷是不是空字符串 null和"" null返回result,不然返回字符串
	 * 
	 * @param s
	 * @return
	 */
	public static String isEmpty(String s, String result) {
		if (s != null && !s.equals("")) {
			return s;
		}
		return result;
	}

	/**
	 * 將帶有htmlcode代碼的字符轉換成<>&'"
	 * 
	 * @param str
	 * @return
	 */
	public static String htmlcodeToSpecialchars(String str) {
		str = str.replaceAll("&", "&");
		str = str.replaceAll(""", "\"");
		str = str.replaceAll("'", "'");
		str = str.replaceAll("<", "<");
		str = str.replaceAll(">", ">");
		return str;
	}

	/**
	 * 移除html標籤
	 * 
	 * @param htmlstr
	 * @return
	 */
	public static String removeHtmlTag(String htmlstr) {
		Pattern pat = Pattern.compile("\\s*<.*?>\\s*", Pattern.DOTALL
				| Pattern.MULTILINE | Pattern.CASE_INSENSITIVE); // \\s?[s|Sc|Cr|Ri|Ip|Pt|T]
		Matcher m = pat.matcher(htmlstr);
		String rs = m.replaceAll("");
		rs = rs.replaceAll(" ", " ");
		rs = rs.replaceAll("<", "<");
		rs = rs.replaceAll(">", ">");
		return rs;
	}

	/**
	 * 取從指定搜索項開始的字符串,返回的值不包含搜索項
	 * 
	 * @param captions
	 *            例如:"www.koubei.com"
	 * @param regex
	 *            分隔符,例如"."
	 * @return 結果字符串,如:koubei.com,如未找到返回空串
	 */
	public static String getStrAfterRegex(String captions, String regex) {
		if (!isEmpty(captions) && !isEmpty(regex)) {
			int pos = captions.indexOf(regex);
			if (pos != -1 && pos < captions.length() - 1) {
				return captions.substring(pos + 1);
			}
		}
		return "";
	}

	/**
	 * 把字節碼轉換成16進制
	 */
	public static String byte2hex(byte bytes[]) {
		StringBuffer retString = new StringBuffer();
		for (int i = 0; i < bytes.length; ++i) {
			retString.append(Integer.toHexString(0x0100 + (bytes[i] & 0x00FF))
					.substring(1).toUpperCase());
		}
		return retString.toString();
	}

	/**
	 * 把16進制轉換成字節碼
	 * 
	 * @param hex
	 * @return
	 */
	public static byte[] hex2byte(String hex) {
		byte[] bts = new byte[hex.length() / 2];
		for (int i = 0; i < bts.length; i++) {
			bts[i] = (byte) Integer.parseInt(hex.substring(2 * i, 2 * i + 2),
					16);
		}
		return bts;
	}

	/**
	 * 轉換數字爲固定長度的字符串
	 * 
	 * @param length
	 *            但願返回的字符串長度
	 * @param data
	 *            傳入的數值
	 * @return
	 */
	public static String getStringByInt(int length, String data) {
		String s_data = "";
		int datalength = data.length();
		if (length > 0 && length >= datalength) {
			for (int i = 0; i < length - datalength; i++) {
				s_data += "0";
			}
			s_data += data;
		}

		return s_data;
	}

	/**
	 * 判斷是否位數字,並可爲空
	 * 
	 * @param src
	 * @return
	 */
	public static boolean isNumericAndCanNull(String src) {
		Pattern numericPattern = Pattern.compile("^[0-9]+$");
		if (src == null || src.equals(""))
			return true;
		boolean return_value = false;
		if (src != null && src.length() > 0) {
			Matcher m = numericPattern.matcher(src);
			if (m.find()) {
				return_value = true;
			}
		}
		return return_value;
	}

	/**
	 * 
	 * @param src
	 * @return
	 */
	public static boolean isFloatAndCanNull(String src) {
		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]*))$");
		if (src == null || src.equals(""))
			return true;
		boolean return_value = false;
		if (src != null && src.length() > 0) {
			Matcher m = numericPattern.matcher(src);
			if (m.find()) {
				return_value = true;
			}
		}
		return return_value;
	}

	public static boolean isNotEmpty(String str) {
		if (str != null && !str.equals(""))
			return true;
		else
			return false;
	}

	public static boolean isDate(String date) {
		String regEx = "\\d{4}-\\d{2}-\\d{2}\\s\\d{2}:\\d{2}:\\d{2}";
		Pattern p = Pattern.compile(regEx);
		Matcher m = p.matcher(date);
		boolean result = m.find();
		return result;
	}

	public static boolean isFormatDate(String date, String regEx) {
		Pattern p = Pattern.compile(regEx);
		Matcher m = p.matcher(date);
		boolean result = m.find();
		return result;
	}

	/**
	 * 根據指定整型list 組裝成爲一個字符串
	 * 
	 * @param src
	 * @return
	 */
	public static String listToString(List<Integer> list) {
		String str = "";
		if (list != null && list.size() > 0) {
			for (int id : list) {
				str = str + id + ",";
			}
			if (!"".equals(str) && str.length() > 0)
				str = str.substring(0, str.length() - 1);
		}
		return str;
	}

	/**
	 * 頁面的非法字符檢查
	 * 
	 * @author shazao
	 * @date 2007-11-29
	 * @param str
	 * @return
	 */
	public static String replaceStr(String str) {
		if (str != null && str.length() > 0) {
			str = str.replaceAll("~", ",");
			str = str.replaceAll(" ", ",");
			str = str.replaceAll(" ", ",");
			str = str.replaceAll(" ", ",");
			str = str.replaceAll("`", ",");
			str = str.replaceAll("!", ",");
			str = str.replaceAll("@", ",");
			str = str.replaceAll("#", ",");
			str = str.replaceAll("\\$", ",");
			str = str.replaceAll("%", ",");
			str = str.replaceAll("\\^", ",");
			str = str.replaceAll("&", ",");
			str = str.replaceAll("\\*", ",");
			str = str.replaceAll("\\(", ",");
			str = str.replaceAll("\\)", ",");
			str = str.replaceAll("-", ",");
			str = str.replaceAll("_", ",");
			str = str.replaceAll("=", ",");
			str = str.replaceAll("\\+", ",");
			str = str.replaceAll("\\{", ",");
			str = str.replaceAll("\\[", ",");
			str = str.replaceAll("\\}", ",");
			str = str.replaceAll("\\]", ",");
			str = str.replaceAll("\\|", ",");
			str = str.replaceAll("\\\\", ",");
			str = str.replaceAll(";", ",");
			str = str.replaceAll(":", ",");
			str = str.replaceAll("'", ",");
			str = str.replaceAll("\\\"", ",");
			str = str.replaceAll("<", ",");
			str = str.replaceAll(">", ",");
			str = str.replaceAll("\\.", ",");
			str = str.replaceAll("\\?", ",");
			str = str.replaceAll("/", ",");
			str = str.replaceAll("~", ",");
			str = str.replaceAll("`", ",");
			str = str.replaceAll("!", ",");
			str = str.replaceAll("@", ",");
			str = str.replaceAll("#", ",");
			str = str.replaceAll("$", ",");
			str = str.replaceAll("%", ",");
			str = str.replaceAll("︿", ",");
			str = str.replaceAll("&", ",");
			str = str.replaceAll("×", ",");
			str = str.replaceAll("(", ",");
			str = str.replaceAll(")", ",");
			str = str.replaceAll("-", ",");
			str = str.replaceAll("_", ",");
			str = str.replaceAll("+", ",");
			str = str.replaceAll("=", ",");
			str = str.replaceAll("{", ",");
			str = str.replaceAll("[", ",");
			str = str.replaceAll("}", ",");
			str = str.replaceAll("]", ",");
			str = str.replaceAll("|", ",");
			str = str.replaceAll("\", ",");
			str = str.replaceAll(":", ",");
			str = str.replaceAll(";", ",");
			str = str.replaceAll(""", ",");
			str = str.replaceAll("'", ",");
			str = str.replaceAll("<", ",");
			str = str.replaceAll(",", ",");
			str = str.replaceAll(">", ",");
			str = str.replaceAll(".", ",");
			str = str.replaceAll("?", ",");
			str = str.replaceAll("/", ",");
			str = str.replaceAll("·", ",");
			str = str.replaceAll("¥", ",");
			str = str.replaceAll("……", ",");
			str = str.replaceAll("(", ",");
			str = str.replaceAll(")", ",");
			str = str.replaceAll("——", ",");
			str = str.replaceAll("-", ",");
			str = str.replaceAll("【", ",");
			str = str.replaceAll("】", ",");
			str = str.replaceAll("、", ",");
			str = str.replaceAll("」", ",");
			str = str.replaceAll("’", ",");
			str = str.replaceAll("《", ",");
			str = str.replaceAll("》", ",");
			str = str.replaceAll("「", ",");
			str = str.replaceAll("。", ",");
		}
		return str;
	}

	/**
	 * 全角字符變半角字符
	 * 
	 * @author shazao
	 * @date 2008-04-03
	 * @param str
	 * @return
	 */
	public static String full2Half(String str) {
		if (str == null || "".equals(str))
			return "";
		StringBuffer sb = new StringBuffer();

		for (int i = 0; i < str.length(); i++) {
			char c = str.charAt(i);

			if (c >= 65281 && c < 65373)
				sb.append((char) (c - 65248));
			else
				sb.append(str.charAt(i));
		}

		return sb.toString();

	}

	/**
	 * 全角括號轉爲半角
	 * 
	 * @author shazao
	 * @date 2007-11-29
	 * @param str
	 * @return
	 */
	public static String replaceBracketStr(String str) {
		if (str != null && str.length() > 0) {
			str = str.replaceAll("(", "(");
			str = str.replaceAll(")", ")");
		}
		return str;
	}

	/**
	 * 分割字符,從開始到第一個split字符串爲止
	 * 
	 * @param src
	 *            源字符串
	 * @param split
	 *            截止字符串
	 * @return
	 */
	public static String subStr(String src, String split) {
		if (!isEmpty(src)) {
			int index = src.indexOf(split);
			if (index >= 0) {
				return src.substring(0, index);
			}
		}
		return src;
	}

	/**
	 * 取url裏的keyword(可選擇參數)參數,用於整站搜索整合
	 * 
	 * @author huoshao
	 * @param params
	 * @param qString
	 * @return
	 */
	public static String getKeyWord(String params, String qString) {
		String keyWord = "";
		if (qString != null) {
			String param = params + "=";
			int i = qString.indexOf(param);
			if (i != -1) {
				int j = qString.indexOf("&", i + param.length());
				if (j > 0) {
					keyWord = qString.substring(i + param.length(), j);
				}
			}
		}
		return keyWord;
	}

	/**
	 * 解析字符串返回map鍵值對(例:a=1&b=2 => a=1,b=2)
	 * 
	 * @param query
	 *            源參數字符串
	 * @param split1
	 *            鍵值對之間的分隔符(例:&)
	 * @param split2
	 *            key與value之間的分隔符(例:=)
	 * @param dupLink
	 *            重複參數名的參數值之間的鏈接符,鏈接後的字符串做爲該參數的參數值,可爲null
	 *            null:不容許重複參數名出現,則靠後的參數值會覆蓋掉靠前的參數值。
	 * @return map
	 * @author sky
	 */
	@SuppressWarnings("unchecked")
	public static Map<String, String> parseQuery(String query, char split1,
			char split2, String dupLink) {
		if (!isEmpty(query) && query.indexOf(split2) > 0) {
			Map<String, String> result = new HashMap();

			String name = null;
			String value = null;
			String tempValue = "";
			int len = query.length();
			for (int i = 0; i < len; i++) {
				char c = query.charAt(i);
				if (c == split2) {
					value = "";
				} else if (c == split1) {
					if (!isEmpty(name) && value != null) {
						if (dupLink != null) {
							tempValue = result.get(name);
							if (tempValue != null) {
								value += dupLink + tempValue;
							}
						}
						result.put(name, value);
					}
					name = null;
					value = null;
				} else if (value != null) {
					value += c;
				} else {
					name = (name != null) ? (name + c) : "" + c;
				}
			}

			if (!isEmpty(name) && value != null) {
				if (dupLink != null) {
					tempValue = result.get(name);
					if (tempValue != null) {
						value += dupLink + tempValue;
					}
				}
				result.put(name, value);
			}

			return result;
		}
		return null;
	}

	/**
	 * 將list 用傳入的分隔符組裝爲String
	 * 
	 * @param list
	 * @param slipStr
	 * @return String
	 */
	@SuppressWarnings("unchecked")
	public static String listToStringSlipStr(List list, String slipStr) {
		StringBuffer returnStr = new StringBuffer();
		if (list != null && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				returnStr.append(list.get(i)).append(slipStr);
			}
		}
		if (returnStr.toString().length() > 0)
			return returnStr.toString().substring(0,
					returnStr.toString().lastIndexOf(slipStr));
		else
			return "";
	}

	/**
	 * 獲取從start開始用*替換len個長度後的字符串
	 * 
	 * @param str
	 *            要替換的字符串
	 * @param start
	 *            開始位置
	 * @param len
	 *            長度
	 * @return 替換後的字符串
	 */
	public static String getMaskStr(String str, int start, int len) {
		if (StringUtil.isEmpty(str)) {
			return str;
		}
		if (str.length() < start) {
			return str;
		}

		// 獲取*以前的字符串
		String ret = str.substring(0, start);

		// 獲取最多能打的*個數
		int strLen = str.length();
		if (strLen < start + len) {
			len = strLen - start;
		}

		// 替換成*
		for (int i = 0; i < len; i++) {
			ret += "*";
		}

		// 加上*以後的字符串
		if (strLen > start + len) {
			ret += str.substring(start + len);
		}

		return ret;
	}

	/**
	 * 根據傳入的分割符號,把傳入的字符串分割爲List字符串
	 * 
	 * @param slipStr
	 *            分隔的字符串
	 * @param src
	 *            字符串
	 * @return 列表
	 */
	public static List<String> stringToStringListBySlipStr(String slipStr,
			String src) {

		if (src == null)
			return null;
		List<String> list = new ArrayList<String>();
		String[] result = src.split(slipStr);
		for (int i = 0; i < result.length; i++) {
			list.add(result[i]);
		}
		return list;
	}

	/**
	 * 截取字符串
	 * 
	 * @param str
	 *            原始字符串
	 * @param len
	 *            要截取的長度
	 * @param tail
	 *            結束加上的後綴
	 * @return 截取後的字符串
	 */
	public static String getHtmlSubString(String str, int len, String tail) {
		if (str == null || str.length() <= len) {
			return str;
		}
		int length = str.length();
		char c = ' ';
		String tag = null;
		String name = null;
		int size = 0;
		String result = "";
		boolean isTag = false;
		List<String> tags = new ArrayList<String>();
		int i = 0;
		for (int end = 0, spanEnd = 0; i < length && len > 0; i++) {
			c = str.charAt(i);
			if (c == '<') {
				end = str.indexOf('>', i);
			}

			if (end > 0) {
				// 截取標籤
				tag = str.substring(i, end + 1);
				int n = tag.length();
				if (tag.endsWith("/>")) {
					isTag = true;
				} else if (tag.startsWith("</")) { // 結束符
					name = tag.substring(2, end - i);
					size = tags.size() - 1;
					// 堆棧取出html開始標籤
					if (size >= 0 && name.equals(tags.get(size))) {
						isTag = true;
						tags.remove(size);
					}
				} else { // 開始符
					spanEnd = tag.indexOf(' ', 0);
					spanEnd = spanEnd > 0 ? spanEnd : n;
					name = tag.substring(1, spanEnd);
					if (name.trim().length() > 0) {
						// 若是有結束符則爲html標籤
						spanEnd = str.indexOf("</" + name + ">", end);
						if (spanEnd > 0) {
							isTag = true;
							tags.add(name);
						}
					}
				}
				// 非html標籤字符
				if (!isTag) {
					if (n >= len) {
						result += tag.substring(0, len);
						break;
					} else {
						len -= n;
					}
				}

				result += tag;
				isTag = false;
				i = end;
				end = 0;
			} else { // 非html標籤字符
				len--;
				result += c;
			}
		}
		// 添加未結束的html標籤
		for (String endTag : tags) {
			result += "</" + endTag + ">";
		}
		if (i < length) {
			result += tail;
		}
		return result;
	}

}// /:~
相關文章
相關標籤/搜索