package cn.api.media.utils;

public class StringUtil
{
	/**
	 * 判断某字符串是否为空或长度为0或由空白符(whitespace) 构成
	 *
	 * @param str
	 * @return
	 */
	public static boolean isBlank(String str) {
		int length;
		if (str == null) {
			return true;
		}

		if ((length = str.length()) == 0) {
			return true;
		}
		for (int i = 0; i < length; ++i) {
			if (Character.isWhitespace(str.charAt(i))) {
				return true;
			}
		}
		return false;
	}

	public static boolean isEmpty(String str) {
		if (null == str || str.length() == 0) {
			return true;
		}
		return false;
	}

	/**
	 * html转文本
	 *
	 * @param htm
	 * @return
	 */
	public static String htm2txt(String htm) {
		if (StringUtil.isBlank(htm)) {
			return htm;
		}
		return htm.replaceAll("&amp;", "&").replaceAll("&lt;", "<").replaceAll("&gt;", ">").replaceAll("&quot;", "\"").replaceAll("&nbsp;", " ").replaceAll("<br/>", "\n").replaceAll("&#39;", "\'");
	}

	/**
	 * html代码转义
	 *
	 * @param txt
	 * @return
	 */
	public static String txt2htm(String txt) {
		if (StringUtil.isBlank(txt)) {
			return txt;
		}
		return txt.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;").replaceAll("\"", "&quot;").replaceAll(" ", "&nbsp;").replaceAll("\n", "<br/>").replaceAll("\'", "&#39;");
	}

	public static boolean contains(String[] strs, String str) {
		if (isEmpty(str) || strs.length == 0) {
			return false;
		}
		for (String s : strs) {
			if (s.equals(str)) {
				return true;
			}
		}
		return false;
	}
}
