import java.io.BufferedReader;import java.io.StringReader;import java.util.ArrayList;import java.util.List;import java.util.regex.Matcher;import java.util.regex.Pattern;/** * @author */public class Strings { /** * 全角转半角 * * @param sbc 全角字符 * @return String */ public static String toDBC(String sbc) { char c[] = sbc.toCharArray(); for (int i = 0; i < c.length; i++) { if (c[i] == '\u3000') c[i] = ' '; else if (c[i] > '\uFF00' && c[i] < '\uFF5F') c[i] = (char) (c[i] - 65248); } return new String(c); } /** * 判断是否为空或 null值 * * @param str String * @return true or false */ public static boolean nullOrEmpty(String str) { if (str == null) return true; String trim = trim(str); return trim.length() == 0 || "null".equals(trim.toLowerCase()); } /** * 去除String前后空格和全角空格 * * @param str 要处理的String * @param smb 自定义无效字符 * @return 去空格的String */ public static String trim(String str, char ...smb) { int len = str.length(); int st = 0; char[] val = str.toCharArray(); while ((st < len) && (val[st] <= ' ' || val[st] == ' '|| containsChar(smb, val[st]))) st++; while ((st < len) && (val[len - 1] <= ' ' || val[len - 1] == ' ')) len--; return ((st > 0) || (len < str.length())) ? str.substring(st, len) : str; } private static boolean containsChar(char[] cs, char c) { if (cs.length > 0) for (char s : cs) if (c == s) return true; return false; } /** * 封装正则表达式命中的值 * * @param str 源字符串 * @param pattern 正则 * @return Optional */ public static Listgroup(String str, String pattern) { return group(str, Pattern.compile(pattern)); } public static List group(String str, Pattern pattern) { Matcher matcher = pattern.matcher(str); List group = new ArrayList<>(); while (matcher.find()) { group.add(matcher.group()); } return group; } /** * 净长: 全角字符算2 * * @param str 字符串 * @return int */ public static int netLength(String str) { int length = 0; for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); length += charLength(c); } return length; } public static int charLength(char c) { return ((int)c) >= 256 ? 2 : 1; } public static boolean isNumber(String str) { Pattern pattern = Pattern.compile("^[-+]?[\\d]*$"); return str != null && pattern.matcher(str).matches(); } /** * 字符串转阿拉伯数字 * * @param str 字符串 * @return long类型阿拉伯数字 * @since 2018.1.26 */ public static long toArabic(String str) { long result = 0; int buffer = 0, ch = 0; StringBuilder back = new StringBuilder(); boolean backSwitch = true; for (char c : str.toCharArray()) { int i = 0; if (c > 47 && c < 58) { ch = c - 48; } else if (c > 65295 && c < 65306) { ch = c - 65296; } else { //noinspection StatementWithEmptyBody while (NUMBER_CHAR[i] != c && ++i < NUMBER_CHAR.length) ; if (i == NUMBER_CHAR.length) { int j = 0; //noinspection StatementWithEmptyBody while (UNIT_CHAR[j] != c && ++j < UNIT_CHAR.length) ; if (j == UNIT_CHAR.length) continue; backSwitch = false; if (j < 2) { result += (buffer + ch) * UNIT_NUMBER[j]; buffer = 0; } else { if (ch == 0) ch = 1; buffer += ch * UNIT_NUMBER[j]; ch = 0; } } else { ch = NUMBER[i]; } } if (backSwitch) back.append(ch); } if (nullOrEmpty(back.toString())) return 0L; return backSwitch ? Long.parseLong(back.toString()) : result + buffer + ch; } public static double similar(String str1, String str2) { /* * 先移除无意义字符,再比较长短,否则 * 如果长串中无效字符过多会出现 ArrayIndexOutOfBoundsException */ String newStrA = removeSign(str1); String newStrB = removeSign(str2); if (newStrA.length() < newStrB.length()) { String temps = newStrA; newStrA = newStrB; newStrB = temps; } return longestCommonSubstring(newStrA, newStrB); } /** * 将字符串的所有数据依次写成一行 */ private static String removeSign(String str) { StringBuilder sb = new StringBuilder(); for (char item : str.toCharArray()) { if (charReg(item)) { sb.append(item); } } return sb.toString(); } /** * 判断字符是否为汉字,数字和字母, * 因为对符号进行相似度比较没有实际意义,故符号不加入考虑范围。 */ private static boolean charReg(char charValue) { return (charValue >= 0x4E00 && charValue <= 0X9FA5) || (charValue >= 'a' && charValue <= 'z') || (charValue >= 'A' && charValue <= 'Z') || (charValue >= '0' && charValue <= '9'); } /** * 求公共子串,采用动态规划算法。 * 其不要求所求得的字符在所给的字符串中是连续的。 */ private static double longestCommonSubstring(String strA, String strB) { char[] chars_strA = strA.toCharArray(); char[] chars_strB = strB.toCharArray(); int m = chars_strA.length; int n = chars_strB.length; /* * 初始化矩阵数据,matrix[0][0]的值为0, * 如果字符数组chars_strA和chars_strB的对应位相同,则matrix[i][j]的值为左上角的值加1, * 否则,matrix[i][j]的值等于左上方最近两个位置的较大值, * 矩阵中其余各点的值为0. */ int[][] matrix = new int[m + 1][n + 1]; for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { if (chars_strA[i - 1] == chars_strB[j - 1]) { matrix[i][j] = matrix[i - 1][j - 1] + 1; } else { matrix[i][j] = Math.max(matrix[i][j - 1], matrix[i - 1][j]); } } } return matrix[m][n] * 1.0 / strA.length(); } private static final char[] NUMBER_CHAR = {'〇', '零', '一', '两', '二', '三', '四', '五', '六', '七', '八', '九'}; private static final char[] UNIT_CHAR = {'亿', '万', '千', '百', '十'}; private static final long[] UNIT_NUMBER = {100000000, 10000, 1000, 100, 10}; private static final int[] NUMBER = {0, 0, 1, 2, 2, 3, 4, 5, 6, 7, 8, 9};}