📄 strings.java
字号:
return result; } /** * Formats and pads a specified number with the specified decimal * format pattern and pads it with leading spaces so that it is * the specified length. * * <P>For a full description Java's decimal formatting pattern * language, see {@link DecimalFormat}. * * <P>For English-style number formatting, the following patterns * are examples that allow arbitrarily long whole-number portions, * and exactly two decimal places. Examples for formatting two numbers * are given in the last two columns. * * <blockquote> * <table cellpadding='5' border='1'> * <tr><td><i>Pattern</i></td><td>Leading Zero?</td><td>Thousands Commas?</td><td>2798.389</td><td>0.391</td></tr> * <tr><td><code>"#,##0.00"</code></td><td>yes</td><td>yes</td><td>2,798.39</td><td>0.39</td></tr> * <tr><td><code>"#0.00"</code></td><td>yes</td><td>no</td><td>2798.39</td><td>0.39</tr></tr> * <tr><td><code>"#,###.00"</code></td><td>no</td><td>yes</td><td>2,798.39</td><td>.39</td></tr> * <tr><td><code>"#.00"</code></td><td>no</td><td>no</td><td>2798.39</td><td>.39</td></tr> * </table> * </blockquote> * * If a variable-length decimal portion is required, the * <code>0</code>s may be replaced with <code>#</code>s. Note * that the formatted numbers are rounded, not truncated; see * {@link java.math.BigDecimal#ROUND_HALF_EVEN} for a full description. * * @param x The number to format. * @param pattern The decimal pattern used to guide formatting. * @param length Length of result in characters. * @throws IllegalArgumentException If the length is not positive or * the pattern is not well formed. */ public static String decimalFormat(double x, String pattern, int length) { if (length < 0) { String msg = "Length must be positive." + " Found length=" + length; throw new IllegalArgumentException(msg); } DecimalFormat formatter = new DecimalFormat(pattern); try { String result = formatter.format(x); // throws IllegalArg if (result.length() > length) throw new IllegalArgumentException(""); if (result.length() == length) return result; StringBuffer sb = new StringBuffer(length); for (int i = (length-result.length()); --i >= 0; ) sb.append(' '); sb.append(result); return sb.toString(); } catch (IllegalArgumentException e) { char[] cs = new char[length]; java.util.Arrays.fill(cs,'?'); return new String(cs); } } /** * Throws an exception if the start and end plus one indices are not * in the range for the specified array of characters. * * @param cs Array of characters. * @param start Index of first character. * @param end Index of one past last character. * @throws IndexOutOfBoundsException If the specified indices are out of * bounds of the specified character array. */ public static void checkArgsStartEnd(char[] cs, int start, int end) { if (end < start) { String msg = "End must be >= start." + " Found start=" + start + " end=" + end; throw new IndexOutOfBoundsException(msg); } if (start >= 0 && end <= cs.length) return; // faster check if (start < 0 || start >= cs.length) { String msg = "Start must be greater than 0 and less than length of array." + " Found start=" + start + " Array length=" + cs.length; throw new IndexOutOfBoundsException(msg); } if (end < 0 || end > cs.length) { String msg = "End must be between 0 and the length of the array." + " Found end=" + end + " Array length=" + cs.length; throw new IndexOutOfBoundsException(msg); } } /** * Returns an array of characters corresponding to the specified * character sequence. The result is a copy, so modifying it will * not affect the argument sequence. * * @param cSeq Character sequence to convert. * @return Array of characters from the specified character sequence. */ public static char[] toCharArray(CharSequence cSeq) { char[] cs = new char[cSeq.length()]; for (int i = 0; i < cs.length; ++i) cs[i] = cSeq.charAt(i); return cs; } /** * Takes a time in milliseconds and returns an hours, minutes and * seconds representation. Fractional ms times are rounded down. * Leading zeros and all-zero slots are removed. A table of input * and output examples follows. * * <table border='1' cellpadding='5'> * <tr><td><i>Input ms</i></td><td><i>Output String</i></td></tr> * <tr><td>0</td><td><code>:00</code></td></tr> * <tr><td>999</td><td><code>:00</code></td></tr> * <tr><td>1001</td><td><code>:01</code></td></tr> * <tr><td>32,000</td><td><code>:32</code></td></tr> * <tr><td>61,000</td><td><code>1:01</code></td></tr> * <tr><td>11,523,000</td><td><code>3:12:03</code></td></tr> * </table> * * @param ms Time in milliseconds. * @return String-based representation of time in hours, minutes * and second format. */ public static String msToString(long ms) { long totalSecs = ms/1000; long hours = (totalSecs / 3600); long mins = (totalSecs / 60) % 60; long secs = totalSecs % 60; String minsString = (mins == 0) ? "00" : ((mins < 10) ? "0" + mins : "" + mins); String secsString = (secs == 0) ? "00" : ((secs < 10) ? "0" + secs : "" + secs); if (hours > 0) return hours + ":" + minsString + ":" + secsString; else if (mins > 0) return mins + ":" + secsString; else return ":" + secsString; } /** * Return <code>true</code> if the two character sequences have * the same length and the same characters. Recall that equality * is not refined in the specification of {@link CharSequence}, but * rather inherited from {@link Object#equals(Object)}. * * The related method {@link #hashCode(CharSequence)} returns * hash codes consistent with this notion of equality. * * @param cs1 First character sequence. * @param cs2 Second character sequence. * @return <code>true</code> if the character sequences yield * the same strings. */ public static boolean equalCharSequence(CharSequence cs1, CharSequence cs2) { if (cs1 == cs2) return true; int len = cs1.length(); if (len != cs2.length()) return false; for (int i = 0; i < len; ++i) if (cs1.charAt(i) != cs2.charAt(i)) return false; return true; } /** * Returns a hash code for a character sequence that is equivalent * to the hash code generated for a its string yield. Recall that * the interface {@link CharSequence} does not refine the definition * of equality beyond that of {@link Object#equals(Object)}. * * <P>The return result is the same as would be produced by: * * <pre> * hashCode(cSeq) = cSeq.toString().hashCode()</pre> * * Recall that the {@link CharSequence} interface requires its * {@link CharSequence#toString()} to return a string * corresponding to its characters as returned by * <code>charAt(0),...,charAt(length()-1)</code>. This value * can be defined directly by inspecting the hash code for strings: * * <pre> * int h = 0; * for (int i = 0; i < cSeq.length(); ++i) * h = 31*h + cSeq.charAt(i); * return h;</pre> * * @param cSeq The character sequence. * @return The hash code for the specified character sequence. */ public static int hashCode(CharSequence cSeq) { if (cSeq instanceof String) return cSeq.hashCode(); int h = 0; for (int i = 0; i < cSeq.length(); ++i) h = 31*h + cSeq.charAt(i); return h; } /** * Returns the equivalent de-accented character for characters in * the Latin-1 (ISO-8859-1) range (0000-00FF). Characters not in * the Latin-1 range are returned as-is. * * Note that Latin-1 is a superset of ASCII, and the unsigned byte * encoding of Latin-1 characters (ISO-8859-1) provides the same * code points as Unicode for characters. * * <p>The <code>unicode.org</code> site supplies a complete <a * href="http://unicode.org/charts/PDF/U0080.pdf">Latin-1 * Supplement</code>, listing the code points for each character. * * @param c Character to de-accent. * @return Equivalent character without accent. */ public static char deAccentLatin1(char c) { switch (c) { case '\u00C0': return 'A'; case '\u00C1': return 'A'; case '\u00C2': return 'A'; case '\u00C3': return 'A'; case '\u00C4': return 'A'; case '\u00C5': return 'A'; case '\u00C6': return 'A'; // capital AE ligature case '\u00C7': return 'C'; case '\u00C8': return 'E'; case '\u00C9': return 'E'; case '\u00CA': return 'E'; case '\u00CB': return 'E'; case '\u00CC': return 'I'; case '\u00CD': return 'I'; case '\u00CE': return 'I'; case '\u00CF': return 'I'; case '\u00D0': return 'D'; case '\u00D1': return 'N'; case '\u00D2': return 'O'; case '\u00D3': return 'O'; case '\u00D4': return 'O'; case '\u00D5': return 'O'; case '\u00D6': return 'O'; case '\u00D8': return 'O'; case '\u00D9': return 'U'; case '\u00DA': return 'U'; case '\u00DB': return 'U'; case '\u00DC': return 'U'; case '\u00DD': return 'Y'; case '\u00DE': return 'P'; // runic letter thorn case '\u00DF': return 's'; // upper case is SS case '\u00E0': return 'a'; case '\u00E1': return 'a'; case '\u00E2': return 'a'; case '\u00E3': return 'a'; case '\u00E4': return 'a'; case '\u00E5': return 'a'; case '\u00E6': return 'a'; // ae ligature case '\u00E7': return 'c'; case '\u00E8': return 'e'; case '\u00E9': return 'e'; case '\u00EA': return 'e'; case '\u00EB': return 'e'; case '\u00EC': return 'i'; case '\u00ED': return 'i'; case '\u00EE': return 'i'; case '\u00EF': return 'i'; case '\u00F0': return 'd'; case '\u00F1': return 'n'; case '\u00F2': return 'o'; case '\u00F3': return 'o'; case '\u00F4': return 'o'; case '\u00F5': return 'o'; case '\u00F6': return 'o'; case '\u00F8': return 'o'; case '\u00F9': return 'u'; case '\u00FA': return 'u'; case '\u00FB': return 'u'; case '\u00FC': return 'u'; case '\u00FD': return 'y'; case '\u00FE': return 'p'; // runic letter thorn case '\u00FF': return 'y'; default: return c; } } /** * Returns the string constructed from the specified character * sequence by deaccenting each of its characters. See {@link * #deAccentLatin1(char)} for details of the de-accenting. * * @param cSeq Character sequence to de accent. * @return De-accented version of input. */ public static String deAccentLatin1(CharSequence cSeq) { char[] cs = new char[cSeq.length()]; for (int i = 0; i < cs.length; ++i) cs[i] = deAccentLatin1(cSeq.charAt(i)); return new String(cs); } /** * Returns the length of the longest shared prefix of the two * input strings. * * @param a First string. * @param b Second string. * @return The length of the longest shared prefix of the two * strings. */ public static int sharedPrefixLength(String a, String b) { int end = java.lang.Math.min(a.length(),b.length()); for (int i = 0; i < end; ++i) if (a.charAt(i) != b.charAt(i)) return i; return end; } /** * The non-breakable space character. */ public static char NBSP_CHAR = (char)160; /** * The newline character. */ public static char NEWLINE_CHAR = '\n'; /** * The default separator character, a single space. */ public static char DEFAULT_SEPARATOR_CHAR = ' '; /** * The default separator string. The string is length * <code>1</code>, consisting of the default separator character * {@link #DEFAULT_SEPARATOR_CHAR}. */ public static String DEFAULT_SEPARATOR_STRING = String.valueOf(DEFAULT_SEPARATOR_CHAR); /** * A string consisting of a single space. */ public static final String SINGLE_SPACE_STRING = " "; /** * The empty string. */ public static final String EMPTY_STRING = ""; /** * The zero-length character array. */ public static final char[] EMPTY_CHAR_ARRAY = new char[0];}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -