package com.accphr.util;


/**
 * 字符串处理工具类,提供了一些对字符串进行处理的静态方法
 */
public final class StringUtils {
	/* 私有的构造方法,保证此类不能外部被实例化 */
	private StringUtils() {
	}

	/**
	 * 如果字符串等于null、空白字符(“”)、空格(“ ”)则返回true,否则返回false
	 * 
	 * @param str
	 *            String 要比较的字符串
	 * @return boolean
	 */
	public static boolean isBlank(String str) {
		boolean b = true;
		if (null == str) {
			return b;
		}

		str = str.trim(); // 去掉空格
		if (!str.equals("")) { // 如果不等于“”空字符则返回值为false
			b = false;
		}
		return b;
	}

	/**
	 * 如果字符串不等于null、空白字符(“”)、空格(“ ”)则返回true,否则返回false
	 * 
	 * @param str
	 *            String 要比较的字符串
	 * @return boolean
	 */
	public static boolean isNotBlank(String str) {
		return (!isBlank(str));
	}

	/**
	 * 如果字符串等于null、空白字符("")、空格(" ")则返回空白字符(""), 否则返回一个将字符串的前后空格去掉的字符串
	 * 
	 * @param str
	 *            String 要处理的字符串
	 * @return String
	 */
	public static String trimToBlank(String str) {
		String s = "";
		if (isBlank(str)) {
			return s;
		}
		s = str.trim();
		return s;
	}

	/**
	 * 如果字符串等于null、空白字符("")、空格(" ")则返回null, 否则返回一个将字符串的前后空格去掉的字符串
	 * 
	 * @param str
	 *            String 要处理的字符串
	 * @return String
	 */
	public static String trimToNull(String str) {
		String s = null;
		if (isBlank(str)) {
			return s;
		}
		s = str.trim();
		return s;
	}

	/**
	 * 将对象使用指定的分隔符转换成一个字符串,
	 * 
	 * @param delimiter
	 *            分隔符
	 * @param ignore
	 *            为true忽略null值
	 * @param objs
	 *            分隔对象
	 * @return
	 */
	public static String join(String delimiter, boolean ignore, Object... objs) {
		if (objs == null || 0 == objs.length) {
			return "";
		}
		StringBuffer bf = new StringBuffer();
		Object obj = null;
		int ind = 0;
		for (int i = 0; i < objs.length; i++) {
			obj = objs[i];
			if (null == obj && true == ignore) {
				continue;
			} else {
				if (0 == ind) {
					bf.append(obj);
				} else {
					bf.append(delimiter).append(obj);
				}
				ind++;
			}
		}
		return bf.toString();
	}

	/**
	 * 使用指定的分隔符将字符串分割成一个字符串数组
	 * 
	 * @param input
	 *            字符串
	 * @param delimiter
	 *            分隔符
	 * @return
	 */
	public static String[] split(String input, String delimiter) {
		String[] values = new String[] { input };
		if (null != input && null != delimiter
				&& -1 != input.indexOf(delimiter)) {
			values = input.split(delimiter);
		}
		return values;
	}

	/**
	 * 将特殊字符(<、>、"、'等)转换成对应的实体
	 * 
	 * @param s
	 *            需要转换的字符串
	 * @return
	 */
	public static final String htmlEncode(String s) {
		return htmlEncode(s, true);
	}

	/**
	 * 将特殊字符(<、>、"、'等)转换成对应的实体
	 * 
	 * @param s
	 *            需要转换的字符串
	 * @param encodeSpecialChars
	 *            对特殊字符进行编码
	 * @return
	 */
	public static final String htmlEncode(String s, boolean encodeSpecialChars) {
		s = trimToBlank(s);
		StringBuffer str = new StringBuffer();
		for (int j = 0; j < s.length(); j++) {
			char c = s.charAt(j);
			if (c < '\200') {
				switch (c) {
				case 34: // '"'
					str.append("&quot;");
					break;

				case 38: // '&'
					str.append("&amp;");
					break;

				case 60: // '<'
					str.append("&lt;");
					break;

				case 62: // '>'
					str.append("&gt;");
					break;

				default:
					str.append(c);
					break;
				}
				continue;
			}
			if (encodeSpecialChars && c < '\377') {
				String hexChars = "0123456789ABCDEF";
				int a = c % 16;
				int b = (c - a) / 16;
				String hex = (new StringBuilder()).append("").append(
						hexChars.charAt(b)).append(hexChars.charAt(a))
						.toString();
				str.append((new StringBuilder()).append("&#x").append(hex)
						.append(";").toString());
			} else {
				str.append(c);
			}
		}

		return str.toString();
	}

	/**
	 * 在每一个类里面写一个main方法,可以很方便的 对这个类进行测试
	 * 
	 * @param args
	 *            String[]
	 */
	public static void main(String[] args) {
		// String s = null;
		// System.out.println(isBlank(s));
		// System.out.println(isNotBlank(s));
		//
		// s = "";
		// System.out.println(isBlank(s));
		// System.out.println(isNotBlank(s));
		//
		// s = " ";
		// System.out.println(isBlank(s));
		// System.out.println(isNotBlank(s));
		//
		// s = " ";
		// System.out.println("[" + trimToNull(s) + "]");
		// System.out.println("[" + trimToBlank(s) + "]");

		// System.out.println(StringUtils.join(",", "ddd", null, "fff"));

		// String input = "aa , bb, ccc";
		// String[] arr = StringUtils.split(null, "z");
		// System.out.println(StringUtils.join("-", arr));
		// System.out.println(arr.length);
		// for (int i = 0; i < arr.length; i++) {
		// System.out.println(arr[i]);
		// }
		//
		// System.out.println(StringUtils.join("-", (Object[]) new Integer[] {
		// 11,
		// 22 }));

		// System.out.println(StringUtils.join(",", false, new Object[] { null,
		// "a", 2, 3, null }));

		// String str = "<br>?";
		// System.out.println(htmlEncode(str, true));
		// System.out.println('\377');
	}
}
最近下载更多
wangchengjun  LV9 2018年3月14日
aihui523  LV34 2017年10月10日
沉默的羔羊  LV13 2017年1月1日
kjisbdmd  LV4 2014年12月13日
最代码官方  LV167 2012年8月22日
最近浏览更多
nurmamat001  LV2 2022年11月1日
tangjj7260  LV18 2021年11月12日
dashenshi 2021年1月30日
暂无贡献等级
smiledog  LV2 2021年1月29日
1358849392  LV21 2020年11月2日
那时年少_1  LV4 2020年9月4日
13063925092  LV1 2020年5月15日
tyyeng  LV18 2020年1月31日
xuyongff  LV24 2019年11月30日
maozexi  LV7 2019年9月26日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友