📄 stringutil.java.svn-base
字号:
} } } return str; } /** Like indevOf except is case-independent */ public static int getIndexIgnoringCase(String str, String subStr) { if (str != null && subStr != null) { return (str.toUpperCase()).indexOf(subStr.toUpperCase()); } return -1; } public static boolean endsWithIgnoreCase(String str, String end) { int lend = end.length(); return str.regionMatches(true, str.length() - lend, end, 0, lend); } /** * Remove the substring beginning with the final occurrence of the * separator, if any. */ public static String upToFinal(String str, String sep) { int pos = str.lastIndexOf(sep); if (pos < 0) { return str; } return str.substring(0, pos); } /* Return the substring following the final dot */ public static String shortName(Object object) { if (object == null) { return null; } String name = object.toString(); return name.substring(name.lastIndexOf('.') + 1); } /* Return the non-qualified name of the class */ public static String shortName(Class clazz) { String className = clazz.getName(); return className.substring(className.lastIndexOf('.') + 1); } /* Return the non-qualified name of the method (Class.method) */ public static String shortName(Method method) { return shortName(method.getDeclaringClass()) + "." + method.getName(); } /** * Returns the number of instances of a particular substring in a string. * This ignores overlap, starting from the left, so 'xxxxxy' would have 2 * instances of 'xx', not 4. Empty string as a substring returns 0. */ public static int countOccurences(String str, String subStr) { int len = subStr.length(); if (len == 0) { return 0; } int pos = 0; int count = 0; while ((pos = str.indexOf(subStr, pos)) >= 0) { count++; pos += len; } return count; } /* Return a string with all the characters from a reader */ public static String fromReader(Reader r) throws IOException { char[] buf = new char[1000]; StringBuffer sb = new StringBuffer(1000); int len; while ((len = r.read(buf)) > 0) { sb.append(buf, 0, len); } return sb.toString(); } /* Return a string with all the characters from an InputStream */ public static String fromInputStream(InputStream in) throws IOException { // use our default encoding rather than system default return fromReader(new InputStreamReader(in, Constants.DEFAULT_ENCODING)); } /** Reads in the entire contents of a file into a string */ public static String fromFile(String path) throws IOException { return fromReader(new FileReader(path)); } /** * Main whether a string is null or the empty string * * @param s * the string * @return true if s is null or the empty string */ public static boolean isNullString(String s) { return s == null || "".equals(s); } /** * Compare two strings for equality or both null. * * @param s1 * string 1 * @param s2 * string 2 * @return true if strings are equal or both null */ public static boolean equalStrings(String s1, String s2) { if (s1 == null) { return s2 == null; } else { return s1.equals(s2); } } private static long gensymCtr = 0; /** * Generate a unique string. * * @param base * the initial substring * @return a string consisting of the supplied initial substring and a * unique counter value. */ public static String gensym(String base) { return base + (gensymCtr++); } /** * Trim a hostname, removing "www." from the front, if present, and the TLD * from the end. If this would result in an empty string, the entire name is * returned. * * @param a * hostname * @return the trimmed hostname */ public static String trimHostName(String hostname) { if (hostname == null) return null; int start = 0; if (hostname.regionMatches(true, 0, "www.", 0, 4)) { start = 4; } int end = hostname.lastIndexOf('.'); if (end <= start) { // if trimming www left nothing but TLD, return whole name return hostname; } return hostname.substring(start, end); } /** * Parse a string as a time interval. An interval is specified as an integer * with an optional suffix. No suffix means milliseconds, s, m, h, d, w * indicates seconds, minutes, hours, days and weeks respectively. * * @param the * interval * @return interval in milliseconds */ // tk - extend to accept combinations: xxHyyMzzS, etc. public static long parseTimeInterval(String str) { try { int len = str.length(); char suffix = str.charAt(len - 1); String numstr; long mult = 1; if (Character.isDigit(suffix)) { numstr = str; } else { numstr = str.substring(0, len - 1); switch (Character.toUpperCase(suffix)) { case 'S': mult = Constants.SECOND; break; case 'M': mult = Constants.MINUTE; break; case 'H': mult = Constants.HOUR; break; case 'D': mult = Constants.DAY; break; case 'W': mult = Constants.WEEK; break; default: throw new NumberFormatException( "Illegal time interval suffix"); } } return Long.parseLong(numstr) * mult; } catch (IndexOutOfBoundsException e) { throw new NumberFormatException("empty string"); } } /** Trim leading and trailing blank lines from a block of text */ public static String trimBlankLines(String txt) { StringBuffer buf = new StringBuffer(txt); while (buf.charAt(0) == '\n') { buf.deleteCharAt(0); } while (buf.charAt(buf.length() - 1) == '\n') { buf.deleteCharAt(buf.length() - 1); } return buf.toString(); } // Unit Descriptor private static class UD { String str; // suffix string long millis; // milliseconds in unit int threshold; // min units to output String stop; // last unit to output if this matched UD(String str, long millis) { this(str, millis, 1); } UD(String str, long millis, int threshold) { this(str, millis, threshold, null); } UD(String str, long millis, int threshold, String stop) { this.str = str; this.millis = millis; this.threshold = threshold; this.stop = stop; } } static UD units[] = { new UD("w", Constants.WEEK, 3, "h"), new UD("d", Constants.DAY, 1, "m"), new UD("h", Constants.HOUR), new UD("m", Constants.MINUTE), new UD("s", Constants.SECOND, 0), }; /** * Generate a string representing the time interval. * * @param millis * the time interval in milliseconds * @return a string in the form dDhHmMsS */ public static String timeIntervalToString(long millis) { StringBuffer sb = new StringBuffer(); if (millis < 10 * Constants.SECOND) { sb.append(millis); sb.append("ms"); } else { boolean force = false; String stop = null; for (int ix = 0; ix < units.length; ix++) { UD iu = units[ix]; long n = millis / iu.millis; if (force || n >= iu.threshold) { millis %= iu.millis; sb.append(n); sb.append(iu.str); force = true; if (stop == null) { if (iu.stop != null) { stop = iu.stop; } } else { if (stop.equals(iu.str)) { break; } } } } } return sb.toString(); } /** * Remove the first line of the stack trace, iff it duplicates the end of * the exception message */ public static String trimStackTrace(String msg, String trace) { int pos = trace.indexOf("\n"); if (pos > 0) { String l1 = trace.substring(0, pos); if (msg.endsWith(l1)) { return trace.substring(pos + 1); } } return trace; } /** * Translate an exception's stack trace to a string. */ public static String stackTraceString(Throwable th) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); th.printStackTrace(pw); return sw.toString(); } /** * Convert the first character and every character that follows a space to * uppercase. */ public static String titleCase(String txt) { StringBuffer buf = new StringBuffer(txt); int len = buf.length(); buf.setCharAt(0, Character.toUpperCase(buf.charAt(0))); for (int i = 1; i < len; i++) { if (buf.charAt(i - 1) == ' ') { buf.setCharAt(i, Character.toUpperCase(buf.charAt(i))); } } return buf.toString(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -