好库网
package com.vanco.abplayer.util;

import java.io.UnsupportedEncodingException;
import 链接已屏蔽.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.TimeZone;

import android.text.TextPaint;


/**
 * 瀛楃涓插伐鍏风被
 * 
 * @author tangjun
 * 
 */
public class StringUtils {

	public static final String EMPTY = "";

	private static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd";
	private static final String DEFAULT_DATETIME_PATTERN = "yyyy-MM-dd hh:mm:ss";
	/** 鐢ㄤ簬鐢熸垚鏂囦欢 */
	private static final String DEFAULT_FILE_PATTERN = "yyyy-MM-dd-HH-mm-ss";
	private static final double KB = 1024.0;
	private static final double MB = 1048576.0;
	private static final double GB = 1073741824.0;
	public static final SimpleDateFormat DATE_FORMAT_PART = new SimpleDateFormat(
			"HH:mm");

	public static String currentTimeString() {
		return DATE_FORMAT_PART.format(Calendar.getInstance().getTime());
	}

	public static char chatAt(String pinyin, int index) {
		if (pinyin != null && pinyin.length() > 0)
			return pinyin.charAt(index);
		return ' ';
	}

	/** 鑾峰彇瀛楃涓插搴?*/
	public static float GetTextWidth(String Sentence, float Size) {
		if (isEmpty(Sentence))
			return 0;
		TextPaint FontPaint = new TextPaint();
		FontPaint.setTextSize(Size);
		return FontPaint.measureText(Sentence.trim()) + (int) (Size * 0.1); // 鐣欑偣浣欏湴
	}

	/**
	 * 鏍煎紡鍖栨棩鏈熷瓧绗︿覆
	 * 
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static String formatDate(Date date, String pattern) {
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		return format.format(date);
	}

	/**
	 * 鏍煎紡鍖栨棩鏈熷瓧绗︿覆
	 * 
	 * @param date
	 * @return 渚嬪2011-3-24
	 */
	public static String formatDate(Date date) {
		return formatDate(date, DEFAULT_DATE_PATTERN);
	}

	public static String formatDate(long date) {
		return formatDate(new Date(date), DEFAULT_DATE_PATTERN);
	}

	/**
	 * 鑾峰彇褰撳墠鏃堕棿 鏍煎紡涓簓yyy-MM-dd 渚嬪2011-07-08
	 * 
	 * @return
	 */
	public static String getDate() {
		return formatDate(new Date(), DEFAULT_DATE_PATTERN);
	}

	/** 鐢熸垚涓€涓枃浠跺悕锛屼笉鍚悗缂€ */
	public static String createFileName() {
		Date date = new Date(System.currentTimeMillis());
		SimpleDateFormat format = new SimpleDateFormat(DEFAULT_FILE_PATTERN);
		return format.format(date);
	}

	/**
	 * 鑾峰彇褰撳墠鏃堕棿
	 * 
	 * @return
	 */
	public static String getDateTime() {
		return formatDate(new Date(), DEFAULT_DATETIME_PATTERN);
	}

	/**
	 * 鏍煎紡鍖栨棩鏈熸椂闂村瓧绗︿覆
	 * 
	 * @param date
	 * @return 渚嬪2011-11-30 16:06:54
	 */
	public static String formatDateTime(Date date) {
		return formatDate(date, DEFAULT_DATETIME_PATTERN);
	}

	public static String formatDateTime(long date) {
		return formatDate(new Date(date), DEFAULT_DATETIME_PATTERN);
	}
	
	/**
	 * 鏍兼灄濞佹椂闂磋浆鎹?	 * 
	 * @param gmt
	 * @return
	 */
	public static String formatGMTDate(String gmt) {
		TimeZone timeZoneLondon = TimeZone.getTimeZone(gmt);
		return formatDate(Calendar.getInstance(timeZoneLondon)
				.getTimeInMillis());
	}

	/**
	 * 鎷兼帴鏁扮粍
	 * 
	 * @param array
	 * @param separator
	 * @return
	 */
	public static String join(final ArrayList<String> array,
			final String separator) {
		StringBuffer result = new StringBuffer();
		if (array != null && array.size() > 0) {
			for (String str : array) {
				result.append(str);
				result.append(separator);
			}
			result.delete(result.length() - 1, result.length());
		}
		return result.toString();
	}

	public static String join(final Iterator<String> iter,
			final String separator) {
		StringBuffer result = new StringBuffer();
		if (iter != null) {
			while (iter.hasNext()) {
				String key = iter.next();
				result.append(key);
				result.append(separator);
			}
			if (result.length() > 0)
				result.delete(result.length() - 1, result.length());
		}
		return result.toString();
	}

	/**
	 * 鍒ゆ柇瀛楃涓叉槸鍚︿负绌?	 * 
	 * @param str
	 * @return
	 */
	public static boolean isEmpty(String str) {
		return str == null || str.length() == 0;
	}

	/**
	 * 
	 * @param str
	 * @return
	 */
	public static String trim(String str) {
		return str == null ? EMPTY : str.trim();
	}

	/**
	 * 杞崲鏃堕棿鏄剧ず
	 * 
	 * @param time
	 *            姣
	 * @return
	 */
	public static String generateTime(long time) {
		int totalSeconds = (int) (time / 1000);
		int seconds = totalSeconds % 60;
		int minutes = (totalSeconds / 60) % 60;
		int hours = totalSeconds / 3600;

		return hours > 0 ? String.format("%02d:%02d:%02d", hours, minutes,
				seconds) : String.format("%02d:%02d", minutes, seconds);
	}

	/** 鏍规嵁绉掗€熻幏鍙栨椂闂存牸寮?*/
	public static String gennerTime(int totalSeconds) {
		int seconds = totalSeconds % 60;
		int minutes = (totalSeconds / 60) % 60;
		return String.format("%02d:%02d", minutes, seconds);
	}

	/**
	 * 杞崲鏂囦欢澶у皬
	 * 
	 * @param size
	 * @return
	 */
	public static String generateFileSize(long size) {
		String fileSize;
		if (size < KB)
			fileSize = size + "B";
		else if (size < MB)
			fileSize = String.format("%.1f", size / KB) + "KB";
		else if (size < GB)
			fileSize = String.format("%.1f", size / MB) + "MB";
		else
			fileSize = String.format("%.1f", size / GB) + "GB";

		return fileSize;
	}

	public static String getTimeDiff(long time) {
		// Calendar cal = Calendar.getInstance();
		long diff = 0;
		// Date dnow = cal.getTime();
		String str = "";
		diff = System.currentTimeMillis() - time;

		if (diff > 2592000000L) {// 30 * 24 * 60 * 60 * 1000=2592000000 姣
			str = "1涓湀鍓?;
		} else if (diff > 1814400000) {// 21 * 24 * 60 * 60 * 1000=1814400000 姣
			str = "3鍛ㄥ墠";
		} else if (diff > 1209600000) {// 14 * 24 * 60 * 60 * 1000=1209600000 姣
			str = "2鍛ㄥ墠";
		} else if (diff > 604800000) {// 7 * 24 * 60 * 60 * 1000=604800000 姣
			str = "1鍛ㄥ墠";
		} else if (diff > 86400000) { // 24 * 60 * 60 * 1000=86400000 姣
			// System.out.println("X澶╁墠");
			str = (int) Math.floor(diff / 86400000f) + "澶╁墠";
		} else if (diff > 18000000) {// 5 * 60 * 60 * 1000=18000000 姣
			// System.out.println("X灏忔椂鍓?);
			str = (int) Math.floor(diff / 18000000f) + "灏忔椂鍓?;
		} else if (diff > 60000) {// 1 * 60 * 1000=60000 姣
			// System.out.println("X鍒嗛挓鍓?);
			str = (int) Math.floor(diff / 60000) + "鍒嗛挓鍓?;
		} else {
			str = (int) Math.floor(diff / 1000) + "绉掑墠";
		}
		return str;
	}

	/**
	 * 鎴彇瀛楃涓?	 * 
	 * @param search
	 *            寰呮悳绱㈢殑瀛楃涓?	 * @param start
	 *            璧峰瀛楃涓?渚嬪锛?title>
	 * @param end
	 *            缁撴潫瀛楃涓?渚嬪锛?/title>
	 * @param defaultValue
	 * @return
	 */
	public static String substring(String search, String start, String end,
			String defaultValue) {
		int start_len = start.length();
		int start_pos = StringUtils.isEmpty(start) ? 0 : search.indexOf(start);
		if (start_pos > -1) {
			int end_pos = StringUtils.isEmpty(end) ? -1 : search.indexOf(end,
					start_pos + start_len);
			if (end_pos > -1)
				return search.substring(start_pos + start.length(), end_pos);
			else
				return search.substring(start_pos + start.length());
		}
		return defaultValue;
	}

	/**
	 * 鎴彇瀛楃涓?	 * 
	 * @param search
	 *            寰呮悳绱㈢殑瀛楃涓?	 * @param start
	 *            璧峰瀛楃涓?渚嬪锛?title>
	 * @param end
	 *            缁撴潫瀛楃涓?渚嬪锛?/title>
	 * @return
	 */
	public static String substring(String search, String start, String end) {
		return substring(search, start, end, "");
	}

	/**
	 * 鎷兼帴瀛楃涓?	 * 
	 * @param strs
	 * @return
	 */
	public static String concat(String... strs) {
		StringBuffer result = new StringBuffer();
		if (strs != null) {
			for (String str : strs) {
				if (str != null)
					result.append(str);
			}
		}
		return result.toString();
	}

	/** 鑾峰彇涓枃瀛楃涓暟 */
	public static int getChineseCharCount(String str) {
		String tempStr;
		int count = 0;
		for (int i = 0; i < str.length(); i++) {
			tempStr = String.valueOf(str.charAt(i));
			if (tempStr.getBytes().length == 3) {
				count++;
			}
		}
		return count;
	}

	/** 鑾峰彇鑻辨枃瀛楃涓暟 */
	public static int getEnglishCount(String str) {
		String tempStr;
		int count = 0;
		for (int i = 0; i < str.length(); i++) {
			tempStr = String.valueOf(str.charAt(i));
			if (!(tempStr.getBytes().length == 3)) {
				count++;
			}
		}
		return count;
	}

	public static String encode(String url) {
		try {
			return URLEncoder.encode(url, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			Logger.e(e);
		}
		return url;
	}
}