📄 str.java
字号:
*/ public static String compressWhitespace (String s) { StringBuffer output = new StringBuffer (); int p = 0; boolean inSpace = true; for (int i = 0, len = s.length (); i < len; ++i) { if (Character.isWhitespace (s.charAt (i))) { if (!inSpace) { output.append (s.substring (p, i)); output.append (' '); inSpace = true; } } else { if (inSpace) { p = i; inSpace = false; } } } if (!inSpace) output.append (s.substring (p)); return output.toString (); } /** * Test if string contains only whitespace. * @param s String to test * @return true iff all characters in s satisfy Character.isWhitespace(). * If s is empty, returns true. */ public static boolean isWhitespace (String s) { for (int i = 0, n = s.length (); i < n; ++i) if (!Character.isWhitespace (s.charAt (i))) return false; return true; } /** * Concatenate an array of strings. * @param list Array of strings to concatenate * @param sep Separator to insert between each string * @return string consisting of list[0] + sep + list[1] + sep + ... + sep + list[list.length-1] */ public static String join (String[] list, String sep) { StringBuffer result = new StringBuffer (); for (int i=0; i < list.length; ++i) { if (i > 0) result.append (sep); result.append (list[i]); } return result.toString (); } /** * Abbreviate a string. * @param s String to abbreviate * @param max Maximum length of returned string; must be at least 5 * @returns s with linebreaks removed and enough characters removed from * the middle (replaced by "...") to make length <= max */ public static String abbreviate (String s, int max) { s = compressWhitespace (s); if (s.length() < max) return s; else { max = Math.max (max-3, 2); // for "..." int half = max/2; return s.substring (0, half) + "..." + s.substring (s.length()-half); } } /** * Abbreviate a multi-line string. * @param s String to abbreviate * @param maxLines Max number of lines in returned string; must be at least 3 * @param message Message to replace removed lines with; should end with * \n, but may be multiple lines. Occurrences of %d are replaced with * the number of lines removed. * @returns s with enough whole lines removed from * the middle (replaced by message) to make its length in lines <= max */ public static String abbreviateLines (String s, int maxLines, String message) { int nLines = countLines (s); if (nLines < maxLines) return s; else { maxLines = Math.max (maxLines-1, 2); // take out one line for "..." int half = maxLines/2; return s.substring (0, nthLine (s, half)) + replace (message, "%d", String.valueOf (nLines - half*2)) + s.substring (nthLine (s, -half)); } } static int countLines (String s) { int n = 1; int i = -1; while ((i = s.indexOf ('\n', i+1)) != -1) ++n; return n; } static int nthLine (String s, int n) { if (n >= 0) { int i = -1; while (n > 0 && (i = s.indexOf ('\n', i+1)) != -1) --n; return i+1; } else { int i = s.length (); while (n < 0 && (i = s.lastIndexOf ('\n', i-1)) != -1) ++n; return i+1; } } /** * Split string around a substring match and return prefix. * @param s String to split * @param pat Substring to search for in s * @return Prefix of s ending just before the first occurrence * of pat. If pat is not found in s, returns s itself. */ public static String before (String s, String pat) { int i = s.indexOf (pat); return (i >= 0) ? s.substring(0, i) : s; } /** * Split string around a substring match and return suffix. * @param s String to split * @param pat Substring to search for in s * @return Suffix of s starting just after the first occurrence * of pat. If pat is not found in s, returns "". */ public static String after (String s, String pat) { int i = s.indexOf (pat); return (i >= 0) ? s.substring(i + pat.length ()) : ""; } /** * Like String.startsWith, but case-insensitive. */ public static boolean startsWithIgnoreCase (String s, String prefix) { int sLen = s.length (); int prefixLen = prefix.length (); return (sLen >= prefixLen && s.substring (0, prefixLen).equalsIgnoreCase (prefix)); } /** * Like String.endsWith, but case-insensitive. */ public static boolean endsWithIgnoreCase (String s, String suffix) { int sLen = s.length (); int suffixLen = suffix.length (); return (sLen >= suffixLen && s.substring (sLen - suffixLen).equalsIgnoreCase (suffix)); } /** * Expands tabs to spaces. */ public static String untabify (String s, int tabsize) { if (s.indexOf ('\t') == -1) return s; // no tabs, don't bother int col = 0; StringBuffer result = new StringBuffer (); for (StringTokenizer tokenizer = new StringTokenizer (s, "\t\r\n", true); tokenizer.hasMoreTokens (); ) { String tok = tokenizer.nextToken (); switch (tok.charAt (0)) { case '\t': { int oldcol = col; col = (col/tabsize + 1) * tabsize; result.append (Str.repeat (" ", col - oldcol)); } break; case '\r': case '\n': col = 0; result.append (tok); break; default: col += tok.length (); result.append (tok); break; } } return result.toString (); } /** * Reverse a string. * @param s String to reverse * @return string containing characters of s in reverse order */ public static String reverse (String s) { StringBuffer t = new StringBuffer (s.length ()); for (int i = s.length () - 1; i >= 0; --i) t.append (s.charAt(i)); return t.toString (); } /** * Find longest common prefix of two strings. */ public static String longestCommonPrefix (String s, String t) { return s.substring (0, longestCommonPrefixLength (s, t)); } public static int longestCommonPrefixLength (String s, String t) { int m = Math.min (s.length (), t.length()); for (int k = 0; k < m; ++k) if (s.charAt (k) != t.charAt (k)) return k; return m; } /** * Find longest common suffix of two strings. */ public static String longestCommonSuffix (String s, String t) { return s.substring (s.length () - longestCommonSuffixLength (s, t)); } public static int longestCommonSuffixLength (String s, String t) { int i = s.length ()-1; int j = t.length ()-1; for (; i >= 0 && j >= 0; --i, --j) if (s.charAt (i) != t.charAt (j)) return s.length () - (i+1); return s.length () - (i+1); } /** * Find longest common prefix of two strings, ignoring case. */ public static String longestCommonPrefixIgnoreCase (String s, String t) { return s.substring (0, longestCommonPrefixLengthIgnoreCase (s, t)); } public static int longestCommonPrefixLengthIgnoreCase (String s, String t) { int m = Math.min (s.length (), t.length()); for (int k = 0; k < m; ++k) if (Character.toLowerCase (s.charAt (k)) != Character.toLowerCase (t.charAt (k))) return k; return m; } /** * Find longest common suffix of two strings, ignoring case. */ public static String longestCommonSuffixIgnoreCase (String s, String t) { return s.substring (s.length () - longestCommonSuffixLengthIgnoreCase (s, t)); } public static int longestCommonSuffixLengthIgnoreCase (String s, String t) { int i = s.length ()-1; int j = t.length ()-1; for (; i >= 0 && j >= 0; --i, --j) if (Character.toLowerCase (s.charAt (i)) != Character.toLowerCase (t.charAt (j))) return s.length () - (i+1); return s.length () - (i+1); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -