gistfile1.txt package com.liu.util;import java.io.UnsupportedEncodingException;import java.util.HashMap;import java.util.Map;import java.util.UUID;import java.util.regex.Matcher;import java.util.regex.Pattern;import net.sourceforge.pinyin4j
package com.liu.util;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
/**
* @ClassName: StringUtil
* @Description: 字符串处理类
*/
public class StringUtil {
private static String[] chars = new String[] { "a", "b", "c", "d", "e", "f",
"g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
"t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
"6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",
"J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
"W", "X", "Y", "Z" };
/**
* @Title: isEmpty
* @Description: 判断字符串是否为空
* @param @param string
* @return boolean
* @throws
*/
public static boolean isEmpty(String string) {
boolean result = false;
if (string == null || "".equals(string.trim())) {
result = true;
}
return result;
}
/**
* 验证Object是否为空,object instanceof String
* @param object
* @return
*/
public static boolean isEmpty(Object object) {
boolean result = false;
if (object == null || "".equals(object.toString().trim())) {
result = true;
}
return result;
}
/**
* @Title: getUuid
* @Description: 获取UUID 带-标识
* @return String 返回类型
* @throws
*/
public static String getUuid() {
UUID uuid = UUID.randomUUID();
String str = uuid.toString();
return str;
}
/**
* @Title: getUUID
* @Description: 获取UUID 去掉-标识
* @return
*/
public static String getUUID() {
String str = getUuid();
str = str.replace("-", "");
return str;
}
/**
* @Title: getShortUuid
* @Description: 获取短UUID
* @return String 短UUID
*/
public static String getShortUuid() {
StringBuffer shortBuffer = new StringBuffer();
String uuid = UUID.randomUUID().toString().replace("-", "");
for (int i = 0; i < 8; i++) {
String str = uuid.substring(i * 4, i * 4 + 4);
int x = Integer.parseInt(str, 16);
shortBuffer.append(chars[x % 0x3E]);
}
return shortBuffer.toString();
}
/**
* @Title: isCheckFiledLen
* @Description: 校验字段长度
* @param @param val
* @param @param length
* @param @return 设定文件
* @return boolean 返回类型
* @throws
*/
public static boolean isCheckFiledLen(String val, int length) {
boolean result = false;
int valLen = val.length();
if (valLen > length) {
result = true;
}
return result;
}
/**
* 将字符串为"null"或空对象转化为字符串""
* @param obj
*/
public static String doNullStr(Object obj) {
String str = "";
if (obj != null) {
str = String.valueOf(obj);
if (str.equals("null")) {
str = "";
}
}
return str.trim();
}
/**
* 将字符串中的中文转化为拼音,其他字符不变
* @param inputStr
* @return
*/
public static String getPingYin(String inputStr) throws Exception {
HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
format.setVCharType(HanyuPinyinVCharType.WITH_V);
char[] input = inputStr.trim().toCharArray();
String output = "";
for (int i = 0; i < input.length; i++) {
if (Character.toString(input[i]).matches("[\\u4E00-\\u9FA5]+")) {
String[] temp = PinyinHelper.toHanyuPinyinStringArray(input[i], format);
output += temp[0];
} else
output += Character.toString(input[i]);
}
return output;
}
/**
* 格式化查询参数
* @param filter
* @return
*/
public static Map
formatParam(String filter) {
filter = InputInjectFilter.decodeInputString(filter);// HTML 反转义
Map
map = new HashMap
(); if (filter != null) { Pattern p = Pattern.compile("(\\w*)=([^&]*)"); Matcher m = p.matcher(filter); while (m.find()) { if (!isEmpty(m.group(1)) && !isEmpty(m.group(2))) { map.put(m.group(1), m.group(2)); } } } return map; } public static Map
formatParamString(String filter) { filter = InputInjectFilter.decodeInputString(filter);// HTML 反转义 Map
map = new HashMap
(); if (filter != null) { Pattern p = Pattern.compile("(\\w*)=([^&]*)"); Matcher m = p.matcher(filter); while (m.find()) { if (!isEmpty(m.group(1)) && !isEmpty(m.group(2))) { map.put(m.group(1), m.group(2)); } } } return map; } /** * @Title: isNumeric * @Description: 判断字符串是否是数字组成 * @param @param str * @param @return 参数说明 * @return boolean 返回类型 * @throws */ public static boolean isNumeric(String str) { for (int i = str.length(); --i >= 0;) { int chr = str.charAt(i); if (chr < 48 || chr > 57) return false; } return true; } /** * @Title: underlineToCamel * @Description: 下划线格式字符转为驼峰式字符规则 * @param str * @return */ public static String underlineToCamel(String str) { if (isEmpty(str)) { return ""; } int len = str.length(); StringBuilder sb = new StringBuilder(len); for (int i = 0; i < len; i++) { char c = str.charAt(i); if ('_' == c) { if (++i < len) { sb.append(Character.toUpperCase(str.charAt(i))); } } else { sb.append(c); } } return sb.toString(); } /** * @Title: camelToUnderline * @Description: 驼峰式字符转为下划线格式字符规则 * @param str * @return */ public static String camelToUnderline(String str) { if (isEmpty(str)) { return ""; } int len = str.length(); StringBuilder sb = new StringBuilder(len); for (int i = 0; i < len; i++) { char c = str.charAt(i); if (Character.isUpperCase(c)) { sb.append("_"); sb.append(Character.toLowerCase(c)); } else { sb.append(c); } } return sb.toString(); } /** * @Title: encode * @Description: 根据指定编码对字符串进行转码 * @param @param str * @param @param code * @param @return * @param @throws UnsupportedEncodingException 参数说明 * @return String 返回类型 * @throws */ public static String encode(String str, String code) throws UnsupportedEncodingException { if (isEmpty(str)) { return ""; } return java.net.URLEncoder.encode(str, code); } /** * @Title: encode * @Description: 对字符转进行UTF-8转码 * @param @param str * @param @return * @param @throws UnsupportedEncodingException 参数说明 * @return String 返回类型 * @throws */ public static String encode(String str) throws UnsupportedEncodingException { if (isEmpty(str)) { return ""; } return encode(str, "UTF-8"); } /** * @Title: decode * @Description: 根据指定编码对字符串进行解码 * @param @param str * @param @param code * @param @return * @param @throws UnsupportedEncodingException 参数说明 * @return String 返回类型 * @throws */ public static String decode(String str, String code) throws UnsupportedEncodingException { if (isEmpty(str)) { return ""; } return java.net.URLDecoder.decode(str, code); } /** * @Title: decode * @Description: 对字符转进行UTF-8解码 * @param @param str * @param @return * @param @throws UnsupportedEncodingException 参数说明 * @return String 返回类型 * @throws */ public static String decode(String str) throws UnsupportedEncodingException { if (isEmpty(str)) { return ""; } return decode(str, "UTF-8"); } }
