📄 stringtools.java
字号:
*** @param s The String to convert to a Unicode encoded String *** @return The Unicode encoded String **/ public static String escapeUnicode(String s) { if (s != null) { StringBuffer sb = new StringBuffer(); int len = s.length(); for (int i = 0; i < len; i++) { char ch = s.charAt(i); if ((ch == '\n') || (ch == '\r')) { sb.append(ch); } else if ((ch == '\t') || (ch == '\f')) { sb.append(ch); } else if ((ch < 0x0020) || (ch > 0x007e)) { sb.append('\\'); sb.append('u'); sb.append(StringTools.hexNybble((ch >> 12) & 0xF)); sb.append(StringTools.hexNybble((ch >> 8) & 0xF)); sb.append(StringTools.hexNybble((ch >> 4) & 0xF)); sb.append(StringTools.hexNybble( ch & 0xF)); } else { sb.append(ch); } } return sb.toString(); } else { return ""; } } /** *** Converts the specified String to a Unicode encoded String.<br> *** That is, convert unicode '\u0000' escapes characters sequences into the unicode character. *** @param u The Unicode escaped ASCII String to convert to unicode character String *** @return The Unicode encoded String **/ public static String unescapeUnicode(String u) { if (u != null) { StringBuffer sb = new StringBuffer(); int len = u.length(); for (int i = 0; i < len;) { char ch = u.charAt(i); if ((ch == '\\') && ((i + 5) < len) && (u.charAt(i+1) == 'u')) { i += 2; int val = 0; for (int x = i; i < x + 4; i++) { int hndx = hexIndex(u.charAt(i)); if (hndx < 0) { break; } val = (val << 4) | hndx; } sb.append((char)val); } else { sb.append(ch); i++; } } return sb.toString(); } else { return ""; } } // ------------------------------------------------------------------------ /** *** Trims the leading/trailing blanks from the specified String argument.<br> *** Similar to the String 'trim()' method with the addition that if the argument *** is null, a non-null empty String will be returned. *** @param s The String to trim, or null to return an empty String *** @return The trimmed String **/ public static String trim(String s) { return (s != null)? s.trim() : ""; } /** *** Returns true if the specified argument is null, or contains 0 or more whitespace characters. *** @param s The String to test for blank. *** @return True if the specified argument is blank, or null. **/ public static boolean isBlank(String s) { return ((s == null) || s.trim().equals("")); } // ------------------------------------------------------------------------ private static final char ESCAPE_CHAR = '\\'; /** *** Return the specified String as a Quoted String, using "double-quotes" *** @param s The String to quote *** @return The quoted String **/ public static String quoteString(String s) { return StringTools.quoteString(s, '\"'); } /** *** Return the specified String as a Quoted String, using the specified quote character *** @param s The String to quote *** @param q The quote character to use to quote the String *** @return The quoted String **/ public static String quoteString(String s, char q) { if (s == null) { s = ""; } int c = 0, len = s.length(); char ch[] = new char[len]; s.getChars(0, len, ch, 0); StringBuffer qsb = new StringBuffer(); qsb.append(q); for (;c < len; c++) { if (ch[c] == q) { // TODO: option should be provided to select how literal quotes are to be specified: // IE. "\\"" or "\"\"" qsb.append(ESCAPE_CHAR).append(q); // \\" } else if (ch[c] == ESCAPE_CHAR) { qsb.append(ESCAPE_CHAR).append(ESCAPE_CHAR); } else if (ch[c] == '\n') { qsb.append(ESCAPE_CHAR).append('n'); } else if (ch[c] == '\r') { qsb.append(ESCAPE_CHAR).append('r'); } else if (ch[c] == '\t') { qsb.append(ESCAPE_CHAR).append('t'); } else { qsb.append(ch[c]); } } qsb.append(q); return qsb.toString(); } // ------------------------------------------------------------------------ // From: http://rath.ca/Misc/Perl_CSV/CSV-2.0.html#csv%20specification // CSV_RECORD ::= (* FIELD DELIM *) FIELD REC_SEP // FIELD ::= QUOTED_TEXT | TEXT // DELIM ::= `,' // REC_SEP ::= `\n' // TEXT ::= LIT_STR | ["] LIT_STR [^"] | [^"] LIT_STR ["] // LIT_STR ::= (* LITERAL_CHAR *) // LITERAL_CHAR ::= NOT_COMMA_NL // NOT_COMMA_NL ::= [^,\n] // QUOTED_TEXT ::= ["] (* NOT_A_QUOTE *) ["] // NOT_A_QUOTE ::= [^"] | ESCAPED_QUOTE // ESCAPED_QUOTE ::= `""' /** *** Quote the specified String based on CSV rules *** @param s The String to quote *** @return The quotes String **/ public static String quoteCSVString(String s) { if (s == null) { s = ""; } boolean needsQuotes = true; // (s.indexOf(',') >= 0); char q = '\"'; if (s.indexOf(q) >= 0) { StringBuffer sb = new StringBuffer(); if (needsQuotes) { sb.append(q); } for (int i = 0; i < s.length(); i++) { char ch = s.charAt(i); if (ch == q) { sb.append("\"\""); } else { sb.append(ch); } } if (needsQuotes) { sb.append(q); } return sb.toString(); } else if (needsQuotes) { return "\"" + s + "\""; } else { return s; } } /** *** Encode the specified array of Strings based on CSV encoding rules *** @param d The array of Strings to encode into a CSV line *** @param checkTextQuote Set true to prefix values with a "'" tick (required by Excel?) *** @return The encoded CSV line **/ public static String encodeCSV(String d[], boolean checkTextQuote) { if (d != null) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < d.length; i++) { if (sb.length() > 0) { sb.append(","); } String v = (d[i] != null)? d[i] : ""; String t = checkTextQuote? ("'" + v) : v; sb.append(StringTools.quoteCSVString(t)); } return sb.toString(); } else { return ""; } } /** *** Encode the specified array of Strings based on CSV encoding rules *** @param d The array of Strings to encode into a CSV line *** @return The encoded CSV line **/ public static String encodeCSV(String d[]) { return StringTools.encodeCSV(d, false); } // ------------------------------------------------------------------------ /** *** Parse the specified quoted String into a non-quoted String *** @param s The quoted String to parse *** @return The non-quoted String **/ public static String parseQuote(String s) { StringBuffer sb = new StringBuffer(); StringTools.parseQuote(s.toCharArray(), 0, sb); return sb.toString(); } /** *** Parse the specified character array into a non-quoted String *** @param ch The quoted char array to parse *** @param a The starting index within the char array to begin parsing *** @param sb The destination where the parsed un-quoted String is placed *** @return The index of the next character following the parsed String **/ public static int parseQuote(char ch[], int a, StringBuffer sb) { // Note on escaped octal values: // Java supports octal values specified in Strings // MySQL dump files do not support octal values in strings // Thus, the interpretation of the value "\00" is ambiguous: // - Java == 0x00 // - MySQL == 0x0030 // 'parseOctal' currently forced to false in order to support MySQL dump files. boolean parseOctal = false; /* validate args */ int chLen = (ch != null)? ch.length : 0; if ((chLen <= 0) || (a < 0) || (a >= chLen)) { return a; } /* check first character to determine if value is quoted */ if ((ch[a] == '\"') || (ch[a] == '\'')) { // quoted string char quote = ch[a]; // save type of quote /* skip past first quote */ a++; // skip past first quote /* parse quoted string */ for (; (a < chLen) && (ch[a] != quote); a++) { /* '\' escaped character? */ if (((a + 1) < chLen) && (ch[a] == '\\')) { a++; // skip past '\\' /* parse octal values */ if (parseOctal) { // look for "\<octal>" values int n = a; for (;(n < chLen) && (n < (a + 3)) && (ch[n] >= '0') && (ch[n] <= '8'); n++); if (n > a) { String octalStr = new String(ch, a, (n - a)); try { int octal = Integer.parseInt(octalStr, 8) & 0xFF; sb.append((char)octal); } catch (NumberFormatException nfe) { // highly unlikely, since we pre-qualified the parsed value Print.logStackTrace("Unable to parse octal: " + octalStr); //sb.append("?"); } a = n - 1; // reset a to last character of octal value continue; } } /* check for specific filtered characters */ if (ch[a] == '0') { // "\0" (this is the only 'octal' value that is allowed sb.append((char)0); } else if (ch[a] == 'r') { // "\r" sb.append('\r'); // ch[a]); } else if (ch[a] == 'n') { // "\n" sb.append('\n'); // ch[a]); } else if (ch[a] == 't') { // "\t" sb.append('\t'); // ch[a]); } else { sb.append(ch[a]); } } else { /* standard unfiltered characters */ sb.append(ch[a]); } } /* skip past last quote */ if (a < chLen) { a++; } // skip past last quote } else { /* break at first whitespace */ for (;(a < chLen) && !Character.isWhitespace(ch[a]); a++) { sb.append(ch[a]); } } return a; } // ------------------------------------------------------------------------ /** *** Parse the specified object into a Number value *** @param data The object to parse *** @param dft The default Number value if unable to parse the specified object *** @return The parsed Number value **/ public static <T> Number parseNumber(Object data, Class<?> numberClass, Number dft) { if (data == null) { return dft; } else if ((numberClass == null) || !Number.class.isAssignableFrom(numberClass)) { return dft; } else if (numberClass.isAssignableFrom(data.getClass())) { return (Number)data; } else { FilterNumber num = new FilterNumber(data.toString(), numberClass);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -