StringUtils 工具类


public class StringUtils {
	private static int uidCount = 0;

	public static List splitStr(String str) {
		List returnList = new ArrayList();
		if (StringUtils.isNotBlank(str)) {
			String[] strList = str.split(",|\\s|;");// 使用逗号分隔或者空格或者分号分隔
			for (String s : strList) {
				if (StringUtils.isNotBlank(s)) {
					returnList.add(s);
				}
			}
		}

		return returnList;
	}

	public static List splitStr(String str, String splitChar) {
		List returnList = new ArrayList();
		if (StringUtils.isNotBlank(str)) {
			String[] strList = str.split(splitChar);// 使用逗号分隔或者空格或者分号分隔
			for (String s : strList) {
				if (StringUtils.isNotBlank(s)) {
					returnList.add(s);
				}
			}
		}

		return returnList;
	}

	/**
	 * 根据文件路径,获取文件名称
	 * @param filePath
	 * @return
	 */
	public static String getFileName(String filePath) {
		if(StringUtils.isBlank( filePath)) {
			return "";
		}
		String filePathNew = filePath.replace("/", "\\");
		String temp[] = filePathNew.split("\\\\");
		String fileName = temp[temp.length-1];  ;
		return fileName;
	}

	public static String replaceBlank(String str, String replace) {
		String dest = "";
		if (str != null) {
			Pattern p = Pattern.compile("\\s*|\t|\r|\n");
			Matcher m = p.matcher(str);
			dest = m.replaceAll(replace);
		}
		return dest;
	}

	/**
	 * 功能:字符串不以"/"结尾,则在串尾加"/"
	 *
	 * @param s
	 * @return
	 */
	public static String addSlashInEnd(String s) {
		if (s != null) {
			s = s.trim();
			if (!s.endsWith("/")) {
				s = s + "/";
			}
		} else {
			s = "";
		}
		return s;
	}

	/**
	 * 功能:字符串如果以\结尾,则去掉\
	 *
	 * @param s
	 * @return
	 */
	public static String delSlashInEnd(String s) {
		if (s != null) {
			s = s.trim();
			if (s.endsWith("/") || s.endsWith("\\")) {
				s = s.substring(0, s.length()-1);
			}
		} else {
			s = "";
		}
		return s;
	}

	/**
	 * 功能:字符串如果以/开头,则去掉第一个/
	 *
	 * @param s
	 * @return
	 */
	public static String delSlashInBefore(String s) {
		if (s != null) {
			s = s.trim();
			if (s.startsWith("/")) {
				s = s.substring(1, s.length());
			}
		} else {
			s = "";
		}
		return s;
	}

	/**
	 * 功能:字符串如果以char开头,则去掉第一个char
	 *
	 * @param s
	 * @return
	 */
	public static String delCharInBefore(String s,char c) {
		if (s != null) {
			s = s.trim();
			if (s.startsWith(c+"")) {
				s = s.substring(1, s.length());
			}
		} else {
			s = "";
		}
		return s;
	}

	/**
	 * 第一个字符串结尾加"/" 第二个字符串开头去掉"/"
	 *
	 * @param pathFirst
	 * @param pathEnd
	 * @return
	 */
	public static String slashPath(String pathFirst, String pathEnd) {
		return addSlashInEnd(pathFirst) + delSlashInBefore(pathEnd);
	}

	/**
	 * 功能:字符串不以"/"结尾,则在串尾加"/";字符串如果以/开头,则去掉第一个/
	 *
	 * @return
	 */
	public static String dealSlash(String s) {
		if (s != null) {
			s = s.trim();
			if (!s.endsWith("/")) {
				s = s + "/";
			}
			if (s.startsWith("/")) {
				s = s.substring(1, s.length());
			}
		} else {
			s = "";
		}
		return s;

	}

	/**
	 * @Description:字符数组转 字符串 “,”隔开 {"SDF","345"} == "SDF,345"
	 * @param
	 * @return
	 */
	public static String arryTOString(String[] data) {
		String returnData = "";
		if (null != data && data.length > 0) {
			for (String temp : data) {
				returnData = temp + "," + returnData;
			}
			returnData = returnData.substring(0, returnData.length() - 1);
		}
		return returnData;
	}

	public static String arryTOString(List data) {
		if (data == null) {
			return "";
		}
		return arryTOString(data.toArray(new String[data.size()]));
	}

	/**
	 * @Description:字符数组转 字符串 “,”隔开 == "'SDF','345'"
	 * @param
	 * @return
	 */
	public static String arryTODbString(String[] data) {
		return arryTODbString(data, true);
	}

	/**
	 * @Description:字符数组转 字符串 “,”隔开 == "'SDF','345'"
	 * @param
	 * @return
	 */
	public static String arryTODbString(String[] data, boolean trim) {
		StringBuffer returnData = new StringBuffer();
		if (null != data && data.length > 0) {
			for (int i = 0; i < data.length; i++) {
				if (trim) {
					if (StringUtils.isNotBlank(data[i])) {
						returnData.append("'" + data[i] + "',");
					}
				} else {
					returnData.append("'" + data[i] + "',");
				}
			}
		}
		if (returnData.length() > 0) {
			return returnData.deleteCharAt(returnData.length() - 1).toString();
		} else {
			return "";
		}
	}

	public static String arryTODbString(List data) {
		return arryTODbString(data, true);
	}

	public static String arryTODbString(List data, boolean trim) {
		if (data == null) {
			return "";
		}
		return arryTODbString(data.toArray(new String[data.size()]), trim);
	}

	/**
	 * 功能:不定长参数,其中一个参数为null或空或为空格字符串则返回true,负责返回false
	 *
	 * @param str
	 * @return boolean
	 */
	public static boolean isBlank(String... str) {
		for (String s : str) {
			if (s == null || s.trim().equals(""))
				return true;
		}
		return false;
	}

	/**
	 * 功能:不定长参数,其中一个参数为null或空则返回true,负责返回false
	 *
	 * @param str
	 * @return boolean
	 */
	public static boolean isEmpty(String... str) {
		for (String s : str) {
			if (s == null || s.equals("")) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 功能:不定长参数,其中一个参数为null或空则返回false,负责返回true
	 *
	 * @param str
	 * @return
	 */
	public static boolean isNotEmpty(String... str) {
		return !isEmpty(str);
	}

	/**
	 * 功能:不定长参数,其中一个参数为null或空或为空格字符串则返回true,负责返回false
	 *
	 * @param str
	 * @return boolean
	 */
	public static boolean isNotBlank(String... str) {
		return !isBlank(str);
	}

	/**
	 * 功能:判断字符串是否是数值. 默认允许有正负号,默认允许有小数点
	 *
	 * @param str
	 * @return
	 */
	public static boolean isNumeric(String str) {
		boolean sign = true;
		int point_bef = Integer.MAX_VALUE;// 小数点前有几位
		int point_aft = Integer.MAX_VALUE;// 小数点后有几位
		return isNumeric(str, sign, point_bef, point_aft);
	}

	/**
	 * 功能:判断字符串是否是数值
	 *
	 * @param str
	 * @param sign
	 *            是否允许有正负号
	 * @param point
	 *            是否允许有小数点
	 * @return
	 */
	public static boolean isNumeric(String str, boolean sign, boolean point) {
		int point_bef = Integer.MAX_VALUE;// 小数点前有几位
		int point_aft = Integer.MAX_VALUE;// 小数点后有几位
		if (!point)
			point_aft = 0;

		return isNumeric(str, sign, point_bef, point_aft);
	}

	/**
	 * 功能:判断字符串是否是数值
	 *
	 * @param str
	 * @param sign
	 *            是否允许有正负号
	 * @param point_bef
	 *            精度,小数点前有几位
	 * @param point_aft
	 *            精度,小数点后有几位,如果为0,则为整数
	 *
	 * @return
	 */
	public static boolean isNumeric(String str, boolean sign, int point_bef, int point_aft) {
		if (StringUtils.isBlank(str)) {
			return false;
		}
		boolean point = true;// 是否允许小数点
		if (point_aft == 0) {
			point = false;// 不允许有小数点
		} else {
			point = true;
		}
		StringBuffer pat = new StringBuffer();
		if (sign) {
			pat.append("[+|-]?");
		}
		if (point_bef == 0) {
			pat.append("[0]");
		} else {
			pat.append("[0-9]{1,");
			pat.append(point_bef);
			pat.append("}");
		}
		if (point && str.indexOf(".") != -1) {// 允许小数点,并且有小数点
			pat.append("[.]");
			pat.append("[0-9]{1,");// 小数点后必须有一位
			pat.append(point_aft);
			pat.append("}");
		}
		Pattern pattern = Pattern.compile(pat.toString());
		if (!pattern.matcher(str).matches()) {
			return false;
		} else {// 排除如00.1,返回false
			if (str.indexOf(".") != -1 && str.substring(0, str.indexOf(".")).length() > 1
					&& Integer.valueOf(str.substring(0, str.indexOf("."))) == 0) {
				return false;
			} else {
				return true;
			}
		}
	}

	/**
	 * 功能:查看字符串是否有这个子字符串
	 *
	 * @param str
	 *            主字符串
	 * @param substr
	 *            字字符串
	 * @return
	 */
	public static boolean hasSubstring(String str, String substr) {
		if (str == null || substr == null)
			return false;
		int strLen = str.length();
		int substrLen = substr.length();
		for (int i = 0; (i + substrLen) <= strLen; i++) {
			if (str.substring(i, i + substrLen).equalsIgnoreCase(substr)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 功能:验证是否是正确的手机号
	 *
	 * @param mobile
	 * @return
	 */
	public static boolean isMobile(String mobile) {
		if (StringUtils.isBlank(mobile))
			return false;
		return Pattern.matches("^(1[3|5|8])\\d{9}$", mobile);
	}

	/**
	 * 功能:传入一个数字类型的参数,返回一个小数点后两位的小数
	 *
	 * @param parm
	 */
	public static String converDouble(String parm) {
		if (isNumeric(parm, false, true)) {
			if (parm.indexOf(".") >= 0) {
				String value = parm.substring(parm.indexOf(".") + 1);
				if (value.length() == 1) {
					return parm + "0";
				} else if (value.length() > 2) {
					return parm.substring(0, parm.indexOf(".") + 1) + value.substring(0, 2);
				} else {
					return parm;
				}
			} else {
				return parm + ".00";
			}
		}
		return null;
	}

	/**
	 * @Description:将请求url路径返回字符串
	 * @param urlstr
	 * @return
	 */
	public static String urlToString(String urlstr) {
		try {
			URL url = new URL(urlstr);
			URLConnection connection = url.openConnection();
			byte[] btArr = null;
			if (connection != null && connection.getInputStream() != null) {
				ByteArrayOutputStream baos = new ByteArrayOutputStream();
				int b = 0;
				b = connection.getInputStream().read();
				while (b != -1) {
					baos.write(b);
					b = connection.getInputStream().read();
				}
				btArr = baos.toByteArray();
			}
			if (btArr != null) {
				String ret = new String(btArr, "UTF-8");
				return ret;
			} else {
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * @Description:验证用户名是否合法,数字或字母或下划线
	 * @param userId
	 * @return
	 */
	public static boolean validateUserId(String userId) {
		if (userId == null) {
			return false;
		}
		String regex = "^[a-zA-Z0-9_]{1,32}$";
		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(userId);
		return m.matches();
	}

	/*
	 * 校验过程: 1、从卡号最后一位数字开始,逆向将奇数位(1、3、5等等)相加。
	 * 2、从卡号最后一位数字开始,逆向将偶数位数字,先乘以2(如果乘积为两位数,将个位十位数字相加,即将其减去9),再求和。
	 * 3、将奇数位总和加上偶数位总和,结果应该可以被10整除。
	 */

	/**
	 * @Description:验证用户名是否合法,数字或字母或下划线, 不能包含邮箱,或者手机号,因为这样会导致占用别人的手机号而别人无法注册
	 * @param userId
	 * @return
	 */
	public static String validateUserName(String userId) {
		if (StringUtils.isMobile(userId)) {
			return "用户名不能是手机号";
		}
		String regex = "^[a-zA-Z0-9-_\u4E00-\u9FA5]{1,64}$";
		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(userId);
		boolean b = m.matches();
		if (b == true) {
			return null;
		} else {
			return "用户名1到64位字母、数字、中文、-或_";
		}
	}

	/**
	 * 校验银行卡卡号
	 */
	public static boolean validateBankCard(String bankCard) {
		if (bankCard.length() < 15 || bankCard.length() > 19) {
			return false;
		} else {
			return true;
		}
	}

	/**
	 * 从不含校验位的银行卡卡号采用 Luhm 校验算法获得校验位
	 *
	 * @param nonCheckCodeBankCard
	 * @return
	 */
	public static char getBankCardCheckCode(String nonCheckCodeBankCard) {
		if (nonCheckCodeBankCard == null || nonCheckCodeBankCard.trim().length() == 0
				|| !nonCheckCodeBankCard.matches("\\d+")) {
			// 如果传的不是数据返回N
			return 'N';
		}
		char[] chs = nonCheckCodeBankCard.trim().toCharArray();
		int luhmSum = 0;
		for (int i = chs.length - 1, j = 0; i >= 0; i--, j++) {
			int k = chs[i] - '0';
			if (j % 2 == 0) {
				k *= 2;
				k = k / 10 + k % 10;
			}
			luhmSum += k;
		}
		return (luhmSum % 10 == 0) ? '0' : (char) ((10 - luhmSum % 10) + '0');
	}

	/**
	 * @Description:验证密码,数字或字母或下划线
	 * @param password
	 * @return
	 */
	public static String validatePassword(String password) {
		String regex = "^.{6,20}$";
		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(password);
		boolean b = m.matches();
		if (b == true) {
			return null;
		} else {
			return "密码6到20个字符";
		}
	}

	/**
	 * 功能:字符串是否是类型字符串
	 *
	 * @param intString
	 * @return
	 */
	public static boolean isInt(String intString) {
		try {
			Integer.parseInt(intString);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	/**
	 * 功能:字符串转int类型
	 *
	 * @param intString
	 * @param defValue
	 * @return
	 */
	public static int toInt(String intString, int defValue) {
		try {
			return (int) Float.parseFloat(intString);
		} catch (Exception e) {
			return defValue;
		}
	}

	/**
	 * 功能:字符串转int类型
	 *
	 * @param intString
	 * @return
	 */
	public static int toInt(String intString) {
		return toInt(intString, 0);
	}

	/**
	 * 功能:转义元字符,就是把某些正则表达式识别的字符前面加\
	 *
	 * @param input
	 * @return
	 */
	public static String shiftMetaCharacters(String input) {
		input = input.replaceAll("\\\\", "\\\\\\\\");
		input = input.replaceAll("\\.", "\\\\.");
		input = input.replaceAll("\\^", "\\\\^");
		input = input.replaceAll("\\$", "\\\\\\$");
		input = input.replaceAll("\\*", "\\\\*");
		input = input.replaceAll("\\+", "\\\\+");
		input = input.replaceAll("\\?", "\\\\?");
		input = input.replaceAll("\\{", "\\\\{");
		input = input.replaceAll("\\}", "\\\\}");
		input = input.replaceAll("\\(", "\\\\(");
		input = input.replaceAll("\\)", "\\\\)");
		input = input.replaceAll("\\[", "\\\\[");
		input = input.replaceAll("\\]", "\\\\]");
		input = input.replaceAll("\\|", "\\\\|");
		return input;
	}

	/**
	 * 功能:转义路径,如将D:\a\b.txt转义成D:\\a\\b.txt
	 *
	 * @param path
	 * @return
	 */
	public static String shiftPath(String path) {
		if (isEmpty(path))
			return path;
		path = path.replaceAll("/", "\\\\");
		path = path.replaceAll("\\\\", "\\\\\\\\");
		return path;
	}

	/**
	 * 功能:转义路径,如将D:\a\b.txt转义成D:\\a\\b.txt
	 *
	 * @param replace
	 * @return
	 */
	public static String shiftReplace(String replace) {
		if (isEmpty(replace))
			return replace;
		replace = replace.replaceAll("\\\\", "\\\\\\\\");
		return replace;
	}

	/**
	 * 功能:转义$-->\$
	 *
	 * @param replace
	 * @return
	 */
	public static String shift$(String replace) {
		if (isEmpty(replace))
			return replace;
		replace = replace.replaceAll("\\$", "\\\\\\$");
		return replace;
	}

	/**
	 * 功能:替换多余字符串为...
	 *
	 * @param input
	 * @param len
	 * @return
	 */
	public static String replaceSpilth(String input, int len) {
		if (isEmpty(input))
			return input;
		if (input.length() <= len)
			return input;
		return input.substring(0, len) + "...";
	}

	/**
	 * 功能:替换多余字符串为...
	 *
	 * @param input
	 * @param len
	 * @param encoding
	 * @return
	 */
	public static String replaceSpilth(String input, int len, String encoding) {
		if (isEmpty(input))
			return input;
		String view = null;
		int counterOfDoubleByte = 0;
		byte b[];
		try {
			b = input.getBytes(encoding);
			if (b.length <= len)
				return input;
			for (int i = 0; i < len; i++) {
				if (b[i] > 0)
					counterOfDoubleByte++;
			}
			if (counterOfDoubleByte % 2 == 0)
				view = new String(b, 0, len, encoding) + "...";
			else
				view = new String(b, 0, len - 1, encoding) + "...";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return view;
	}

	/**
	 * 功能:截取某个字符串左边的部分,例 D:\a\bb.txt ==> \左边D:
	 *
	 * @param input
	 * @param leftStr
	 * @return
	 */
	public static String subLeft(String input, String leftStr) {
		if (isEmpty(input, leftStr))
			return input;
		int left = input.indexOf(leftStr);
		if (left == -1)
			return null;
		String retString = input.substring(0, left);
		return retString;
	}

	/**
	 * 功能:截取某个字符串左边更多的部分,例 D:\a\bb.txt ==> \左边D:\a
	 *
	 * @param input
	 * @param leftStr
	 * @return
	 */
	public static String subLeftMore(String input, String leftStr) {
		if (isEmpty(input, leftStr))
			return input;
		int left = input.lastIndexOf(leftStr);
		if (left == -1)
			return null;
		String retString = input.substring(0, left);
		return retString;
	}

	/**
	 * 功能:截取某个字符串右边的部分,例 D:\a\bb.txt ==> \右边bb.txt
	 *
	 * @param input
	 * @param rightStr
	 * @return
	 */
	public static String subRight(String input, String rightStr) {
		if (isEmpty(input, rightStr))
			return input;
		int right = input.lastIndexOf(rightStr);
		if (right == -1)
			return null;
		else
			right = right + rightStr.length();
		String retString = input.substring(right);
		return retString;
	}

	/**
	 * 功能:截取某个字符串右边更多的部分,例 D:\a\bb.txt ==> \右边a\bb.txt
	 *
	 * @param input
	 * @param rightStr
	 * @return
	 */
	public static String subRightMore(String input, String rightStr) {
		if (isEmpty(input, rightStr))
			return input;
		int right = input.indexOf(rightStr);
		if (right == -1)
			return null;
		else
			right = right + rightStr.length();
		String retString = input.substring(right);
		return retString;
	}

	/**
	 * 功能:截取某两个字符串中间的字符串
	 *
	 * @param input
	 * @param leftStr
	 * @param rightStr
	 * @return
	 */
	public static String subMid(String input, String leftStr, String rightStr) {
		if (isEmpty(input, leftStr, rightStr))
			return input;
		int left = input.indexOf(leftStr);
		if (left == -1)
			return input;
		else
			left += leftStr.length();
		int right = input.indexOf(rightStr, left);
		if (right == -1)
			return input;
		String retString = input.subSequence(left, right).toString();
		return retString;
	}

	/**
	 * 功能:截取某两个字符串中间的字符串,最后面一个
	 *
	 * @param input
	 * @param leftStr
	 * @param rightStr
	 * @return
	 */
	public static String subMidLast(String input, String leftStr, String rightStr) {
		if (isEmpty(input, leftStr, rightStr))
			return input;
		int right = input.lastIndexOf(rightStr);
		if (right == -1)
			return input;
		int left = input.lastIndexOf(leftStr, right);
		if (left == -1)
			return input;
		else
			left += leftStr.length();
		String retString = input.subSequence(left, right).toString();
		return retString;
	}

	/**
	 * 功能:截取某两个字符串中间的所有匹配的字符串,截成数组
	 *
	 * @param input
	 * @param leftStr
	 * @param rightStr
	 * @return
	 */
	public static String[] subMids(String input, String leftStr, String rightStr, int flags) {
		if (isEmpty(input, leftStr, rightStr))
			return null;
		String left = "(?<=" + shiftMetaCharacters(leftStr) + ")";
		String right = "(?=" + shiftMetaCharacters(rightStr) + ")";
		Pattern p = Pattern.compile(left + ".*?" + right, flags);
		Matcher m = p.matcher(input);
		List list = new ArrayList();
		while (m.find()) {
			String group = m.group();
			list.add(group);
		}
		return list.toArray(new String[list.size()]);
	}

	/**
	 * 功能:截取某两个字符串中间的所有匹配的字符串,截成数组
	 *
	 * @param input
	 * @param leftStr
	 * @param rightStr
	 * @return
	 */
	public static String[] subMids(String input, String leftStr, String rightStr) {
		return subMids(input, leftStr, rightStr, Pattern.DOTALL);
	}

	/**
	 * 功能:字符串用正则表达式快速验证
	 *
	 * @param input
	 * @param regex
	 * @param flags
	 * @return
	 */
	public static boolean isMatch(String input, String regex, int flags) {
		Pattern p = Pattern.compile(regex, flags);
		return p.matcher(input).matches();
	}

	/**
	 * 功能:字符串用正则表达式快速验证
	 *
	 * @param input
	 * @param regex
	 * @return
	 */
	public static boolean isMatch(String input, String regex) {
		Pattern p = Pattern.compile(regex, Pattern.DOTALL);
		return p.matcher(input).matches();
	}

	/**
	 * 功能:正则表达式进行快速查找
	 *
	 * @param input
	 * @param regex
	 * @param flags
	 * @return
	 */
	public static String match(String input, String regex, int flags) {
		Pattern p = Pattern.compile(regex, flags);
		Matcher matcher = p.matcher(input);
		if (matcher.find()) {
			return matcher.group();
		}
		return null;
	}

	/**
	 * 功能:正则表达式进行快速查找
	 *
	 * @param input
	 * @param regex
	 * @return
	 */
	public static String match(String input, String regex) {
		return match(input, regex, Pattern.DOTALL);
	}

	/**
	 * 功能:正则表达式进行快速查找
	 *
	 * @param input
	 * @param regex
	 * @param flags
	 * @return
	 */
	public static List matchs(String input, String regex, int flags) {
		Pattern p = Pattern.compile(regex, flags);
		Matcher matcher = p.matcher(input);
		List list = new ArrayList();
		while (matcher.find()) {
			list.add(matcher.group());
		}
		return list;
	}

	/**
	 * 功能:正则表达式进行快速查找
	 *
	 * @param input
	 * @param regex
	 * @return
	 */
	public static List matchs(String input, String regex) {
		return matchs(input, regex, Pattern.DOTALL);
	}

	/**
	 * 功能:让字符串首字母大写
	 *
	 * @param input
	 * @return
	 */
	public static String toUpperFristCase(String input) {
		if (isEmpty(input))
			return input;
		return input.replaceAll("^[a-z]", input.substring(0, 1).toUpperCase());
	}

	/**
	 * 功能:让字符串首字母小写
	 *
	 * @param input
	 * @return
	 */
	public static String toLowerFristCase(String input) {
		if (isEmpty(input))
			return input;
		return input.replaceAll("^[A-Z]", input.substring(0, 1).toLowerCase());
	}

	/**
	 * 功能:字符串数组拼接
	 *
	 * @param arrayStr
	 * @param joinStr
	 * @return
	 */
	public static String join(String[] arrayStr, String joinStr) {
		if (arrayStr == null)
			return null;
		if (joinStr == null)
			joinStr = " ";
		StringBuffer retStr = new StringBuffer();
		for (int i = 0; i < arrayStr.length - 1; i++) {
			retStr.append(arrayStr[i]);
			retStr.append(joinStr);
		}
		retStr.append(arrayStr[arrayStr.length - 1]);
		return retStr.toString();
	}

	/**
	 * 功能:过滤HTML标签元素"<",">"," ","&"等等
	 *
	 * @param input
	 * @return
	 */
	public static String filterHtml(String input) {
		if (isEmpty(input))
			return input;
		input = input.replaceAll("&", "&");
		input = input.replaceAll("<", "<");
		input = input.replaceAll(">", ">");
		input = input.replaceAll(" ", " ");
		input = input.replaceAll("'", "'");
		input = input.replaceAll("\\\\", """);
		input = input.replaceAll("\\n", "
"); return input; } /** * 格式化字符串,如format("{0}={1}","a","b") ==> a=b * * @param fmtStr * @param args * @return */ public static String format(String fmtStr, String... args) { // fmtStr = shiftMetaCharacters(fmtStr); for (int i = 0; i < args.length; i++) { fmtStr = fmtStr.replaceAll("\\{" + i + "\\}", shift$(shiftReplace(args[i]))); } return fmtStr; } /** * 功能:反转字符串 * * @param input * @return */ public static String reverse(String input) { return new StringBuffer(input).reverse().toString(); } /** * 功能:字符串转码 * * @param input * @param sourceEncoding * @param tagetEncoding * @return */ public static String changeEncoding(String input, String sourceEncoding, String tagetEncoding) { if (isEmpty(input)) return input; String retString = null; try { byte[] bs = input.getBytes(sourceEncoding); retString = new String(bs, tagetEncoding); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return retString; } /** * 功能:生成一个唯一id * * @return */ public static String genUniqueId() { long lastTime = System.currentTimeMillis(); StringBuffer result = new StringBuffer(); // result.append("UID"); Random random = new Random(); result.append(lastTime).append(uidCount++).append(random.nextInt(10000)); return result.toString(); } /** * 功能:新split字符串分割 * * @param input * @param split * @return */ public static String[] split(String input, String split) { if (input == null || split == null) { return null; } Matcher matcher = Pattern.compile(split).matcher(input); List splitList = new ArrayList(); int start = 0; while (matcher.find()) { int end = matcher.start(); // splitList splitList.add(input.substring(start, end)); start = end + matcher.group().length(); } splitList.add(input.substring(start, input.length())); return splitList.toArray(new String[splitList.size()]); } // 返还网 ('1','2') public static String getInParamStr(List list) { StringBuffer returnSb = new StringBuffer(); for (int i = 0; i < list.size(); i++) { String v = list.get(i); if (StringUtils.isNotBlank(v)) { if (i > 0) { returnSb.append(","); } returnSb.append("'" + v + "'"); } } return "(" + returnSb.toString() + ")"; } public static String getInParamStr(String[] liststr) { List list = Arrays.asList(liststr); return getInParamStr(list); } public static String subStr(String str, int length) { if (str == null) { return ""; } if (str.length() <= length) { return str; } else { return str.substring(0, length); } } /** * 取Exception 中的错误信息 * @param e * @param len * @return */ public static String getErrInfoFroException(Exception e, int len){ String retvalue = ""; if(null!=e && len >0){ retvalue = e.getMessage(); if(isNotBlank(retvalue)){ if(retvalue.length()>len) { retvalue = retvalue.substring(0, len); } } } return retvalue; } /** * @Description:(1)长度在6-18位; (2)字母、数字或者字母数字的组合。 * * @param pwd * @return */ public static boolean validatePwd(String pwd) { String regex = "^[a-zA-Z0-9]{6,18}$"; Pattern p = Pattern.compile(regex); Matcher m = p.matcher(pwd); boolean b = m.matches(); return b; } /** * * @Description: TODO * @author qiuyr * @date 2016-9-13 * @param hideColumn 需隐藏的字段 * @param len 隐藏位数 * @param star 从什么位置开始隐藏 下标从0开始 * @param replaceChar 隐藏替换的字符 默认为* * @return * @return String */ public static String hideString(String hideColumn, int len, int star, String replaceChar) { String hideColumnStr = ""; if (hideColumn != null) { hideColumnStr = hideColumn.toString(); } if (StringUtils.isNotBlank(hideColumnStr)) { if (star >= 0 && star <= hideColumnStr.length() - len && hideColumnStr.length() > len) { String hideColumn1 = hideColumnStr.substring(0, star); String hideColumn2 = hideColumnStr.substring(star + len, hideColumnStr.length()); String count = ""; for (int i = 0; i < len; i++) { count = count + (replaceChar == null ? "*" : replaceChar); } hideColumnStr = hideColumn1 + count + hideColumn2; } else if (star < 0 || star > hideColumnStr.length() - len) { //Log.print("属性 start设置错误,不格式化"); } else if (hideColumnStr.length() <= len) { //Log.print("属性 length设置错误,不格式化"); } } return hideColumnStr; } /** * 将NULL值转为空字符 * @param value * @return */ public static String nullToString(String value){ if(isBlank(value)) { return ""; }else{ return value; } } public static void main(String arg[]) { /* * // hideString 使用例子 String phone = "13800138000"; String hidephone = * hideString(phone, phone.length()-7, 3, "&"); System.out.println(phone); * System.out.println(hidephone); String cardId = "6210817200007293358"; String * hidecardId = hideString(cardId, 11, 4, null); System.out.println(cardId); * System.out.println(hidecardId); */ /* * String[] ss = cn.toruk.expand.pub.utils.StringUtils.split("a||c||", "\\|"); * System.out.println("len:"+ss.length); int i=0; for (String s : ss) { * System.out.println(i+++":"+s); } */ // File f = new File("c:/a/b/c"); // System.out.println(f.getPath()); // System.out.println(arryTODbString(new String[]{"a","b"}) ); // System.out.println(validateUserName("1234的678912345678912345678912345678912345678912345678912345678")); // System.out.println(validatePassword("aa11 22")); //System.out.println(delSlashInEnd("http://a/b\\")); System.out.println( getFileName("结婚证.jpg")); } }

 

你可能感兴趣的:(java)