package ink.wgink.util.string; 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; import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination; import org.apache.commons.lang3.StringUtils; import java.util.List; import java.util.Random; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * 字符串相关方法 */ public class WStringUtil { static final Pattern LOWER_UPPER_PATTERN = Pattern.compile("[A-Z][a-z]*"); /** * 将以逗号分隔的字符串转换成字符串数组 * * @param valStr * @return String[] */ public static String[] strList(String valStr) { int i = 0; String TempStr = valStr; String[] returnStr = new String[valStr.length() + 1 - TempStr.replace(",", "").length()]; valStr = valStr + ","; while (valStr.indexOf(',') > 0) { returnStr[i] = valStr.substring(0, valStr.indexOf(',')); valStr = valStr.substring(valStr.indexOf(',') + 1, valStr.length()); i++; } return returnStr; } /** * 获取字符串编码 * * @param str * @return */ public static String getEncoding(String str) { String encode = "GB2312"; try { if (str.equals(new String(str.getBytes(encode), encode))) { String s = encode; return s; } } catch (Exception exception) { } encode = "ISO-8859-1"; try { if (str.equals(new String(str.getBytes(encode), encode))) { String s1 = encode; return s1; } } catch (Exception exception1) { } encode = "UTF-8"; try { if (str.equals(new String(str.getBytes(encode), encode))) { String s2 = encode; return s2; } } catch (Exception exception2) { } encode = "GBK"; try { if (str.equals(new String(str.getBytes(encode), encode))) { String s3 = encode; return s3; } } catch (Exception exception3) { } return ""; } /** * 第一个字母转小写,英文 * * @param str * @return */ public static String firstToLower(String str) { return str.substring(0, 1).toLowerCase() + str.substring(1); } /** * 第一个字母大写、英文 * * @param str * @return */ public static String firstToUpper(String str) { return str.substring(0, 1).toUpperCase() + str.substring(1); } /** *
* title 驼峰名称转下划线名称 *
** description 驼峰名称转小写名称用下划线名称 *
* * @param str * @return * @author WenG * @date 2018年2月28日 下午4:28:45 * @modifier WenG * @date 2018年2月28日 下午4:28:45 */ public static String lowerUpper2UnderLine(String str) { return lowerUpper2Separator(str, "-"); } /** ** title 驼峰名称转分隔符名称 *
** description 驼峰名称转小写名称用分隔符名称 *
* * @param str * @param separator * @return */ public static String lowerUpper2Separator(String str, String separator) { Matcher matcher = LOWER_UPPER_PATTERN.matcher(str); while (matcher.find()) { String group = matcher.group(); String lower = separator + group.toLowerCase(); str = str.replaceFirst(group, lower); matcher = LOWER_UPPER_PATTERN.matcher(str); } if (str.startsWith(separator)) { str = str.substring(1); } return str; } /** ** title 下划线分割转驼峰式 *
** description 下划线分割转驼峰式 *
* * @param str * @return * @author WenG * @date 2018年2月28日 下午4:49:13 * @modifier WenG * @date 2018年2月28日 下午4:49:13 */ public static String underLine2LowerUpper(String str) { return separator2LowerUpper(str, "_"); } /** ** title 分割符转驼峰式 *
** description 分割符转驼峰式 *
* * @param str 字符串 * @param separator 分隔符 * @return */ public static String separator2LowerUpper(String str, String separator) { String[] strs = str.split(separator); StringBuilder sb = new StringBuilder(); for (int i = 0; i < strs.length; i++) { String letter = strs[i].toLowerCase(); if (letter.length() == 0) { continue; } if (i == 0) { sb.append(letter); } else { int firstLetter = letter.charAt(0); firstLetter -= 32; sb.append((char) firstLetter).append(letter.substring(1)); } } if (sb.length() > 0) { return sb.toString(); } return str; } /** ** title 下划线分割转驼峰式 *
** description 下划线分割转驼峰式 *
* * @param str * @param firstLower 第一个字母小写 * @return * @author WenG * @date 2018年2月28日 下午4:56:28 * @modifier WenG * @date 2018年2月28日 下午4:56:28 */ public static String underLine2LowerUpper(String str, boolean firstLower) { String result = underLine2LowerUpper(str); if (firstLower) { int first = result.charAt(0) + 32; result = ((char) first) + result.substring(1); } return result; } /** ** title 分隔符转驼峰式 *
** description 分隔符转驼峰式 *
* * @param str 字符串 * @param separator 分隔符 * @param firstLower 首字母小写 * @return */ public static String separator2LowerUpper(String str, String separator, boolean firstLower) { String result = separator2LowerUpper(str, separator); if (firstLower) { int first = result.charAt(0) + 32; result = ((char) first) + result.substring(1); } return result; } /** * 获取拼音 * * @param src * @return */ public static String getPingYin(String src) { char[] t1 = null; t1 = src.toCharArray(); String[] t2 = new String[t1.length]; HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat(); t3.setCaseType(HanyuPinyinCaseType.LOWERCASE); t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE); t3.setVCharType(HanyuPinyinVCharType.WITH_V); String t4 = ""; int t0 = t1.length; try { for (int i = 0; i < t0; i++) { // 判断是否为汉字字符 if (Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+")) { t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3); t4 += t2[0]; } else { t4 += Character.toString(t1[i]); } } return t4; } catch (BadHanyuPinyinOutputFormatCombination e1) { e1.printStackTrace(); } return t4; } /** * 获取中文首字母 * * @param str * @return */ public static String getPinYinHeadChar(String str) { String convert = ""; for (int j = 0; j < str.length(); j++) { char word = str.charAt(j); String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word); if (pinyinArray != null) { convert += pinyinArray[0].charAt(0); } else { convert += word; } } return convert; } /** * 汉字转ASCII码 * * @param cnStr * @return */ public static String getCnASCII(String cnStr) { StringBuffer strBuf = new StringBuffer(); byte[] bGBK = cnStr.getBytes(); for (int i = 0; i < bGBK.length; i++) { // System.out.println(Integer.toHexString(bGBK[i]&0xff)); strBuf.append(Integer.toHexString(bGBK[i] & 0xff)); } return strBuf.toString(); } /** * 从字符串中随机取出新的字符串 * * @param input 输入字符串 * @param outLength 新字符串长度 * @return */ public static String randomSubStr(String input, int outLength) { if (input == null || input.isEmpty()) { return null; } if (outLength >= input.length()) { outLength = input.length(); } if (outLength <= 0) { outLength = 6; } StringBuilder subStrSB = new StringBuilder(); Random random = new Random(); for (int i = 0; i < outLength; i++) { subStrSB.append(input.charAt(random.nextInt(input.length()))); } return subStrSB.toString(); } /** * string对象转数组 * * @param strObj stirng 对象 * @param separator 分隔符 * @return 字符串数组 */ public static String[] strObjToArray(Object strObj, String separator) { if (strObj == null || separator == null) { throw new RuntimeException("参数不能为空!"); } if (!(strObj instanceof String)) { throw new RuntimeException("第一个参数只能是String类型"); } return strObj.toString().split(separator); } /** * 字符串列表转字符串 * * @param strings 字符串列表 * @param separator 分隔符 * @return */ public static String listToStr(List