@GwtCompatible public final class Strings { private Strings() {} /** *若string爲null,則返回空串;不然,返回自身 */ public static String nullToEmpty(@Nullable String string) { return (string == null) ? "" : string; } /** * 若string爲null或者empty,都返回null;不然,返回自身 */ @Nullable public static String emptyToNull(@Nullable String string) { return isNullOrEmpty(string) ? null : string; } /** *當string爲null或者爲空時,返回true;不然,返回false *這裏判斷空串是經過length()==0,而java6中是isEmpty() */ public static boolean isNullOrEmpty(@Nullable String string) { return string == null || string.length() == 0; // string.isEmpty() in Java 6 } /** * 字符串填補 * @param string 放在結果字符串的最後一部分 * @param minLength 返回結果串應該具備的最小長度。能夠爲0或者負數,這樣返回的是string自己 * @param padChar 用於填補的字符,插入在string的前面,插入次數直到返回串的總長度達到minLength * @return the padded string */ public static String padStart(String string, int minLength, char padChar) { checkNotNull(string); if (string.length() >= minLength) { return string; } StringBuilder sb = new StringBuilder(minLength); for (int i = string.length(); i < minLength; i++) { sb.append(padChar); } sb.append(string); return sb.toString(); } /** * 與padStart相似,只不過padChar放到了string的後面 */ public static String padEnd(String string, int minLength, char padChar) { checkNotNull(string); if (string.length() >= minLength) { return string; } StringBuilder sb = new StringBuilder(minLength); sb.append(string); for (int i = string.length(); i < minLength; i++) { sb.append(padChar); } return sb.toString(); } /** *字符串拷貝 * @param string 非空字符串 * @param count 重複次數,非負的 * @return a string containing {@code string} repeated {@code count} times * (the empty string if {@code count} is zero) * @throws IllegalArgumentException if {@code count} is negative */ public static String repeat(String string, int count) { checkNotNull(string); // eager for GWT. if (count <= 1) { checkArgument(count >= 0, "invalid count: %s", count); return (count == 0) ? "" : string; } final int len = string.length(); final long longSize = (long) len * (long) count; final int size = (int) longSize; //若是最後獲得的字符串過長,直接拋出異常 if (size != longSize) { throw new ArrayIndexOutOfBoundsException( "Required array size too large: " + longSize); } final char[] array = new char[size]; //直接調用系統的字符串拷貝函數:System.arraycopy(value, srcBegin, dst, dstBegin, srcEnd - srcBegin); string.getChars(0, len, array, 0); int n; for (n = len; n < size - n; n <<= 1) { System.arraycopy(array, 0, array, n, n); } //最後一次拷貝,拷貝長度是size-n System.arraycopy(array, 0, array, n, size - n); return new String(array); } /** *計算兩個字符串的公共前綴 */ public static String commonPrefix(CharSequence a, CharSequence b) { checkNotNull(a); checkNotNull(b); int maxPrefixLength = Math.min(a.length(), b.length()); int p = 0; while (p < maxPrefixLength && a.charAt(p) == b.charAt(p)) { p++; } if (validSurrogatePairAt(a, p - 1) || validSurrogatePairAt(b, p - 1)) { p--; } return a.subSequence(0, p).toString(); } /** * 同commonPrefix,只不過求的是公共後綴 */ public static String commonSuffix(CharSequence a, CharSequence b) { checkNotNull(a); checkNotNull(b); int maxSuffixLength = Math.min(a.length(), b.length()); int s = 0; while (s < maxSuffixLength && a.charAt(a.length() - s - 1) == b.charAt(b.length() - s - 1)) { s++; } if (validSurrogatePairAt(a, a.length() - s - 1) || validSurrogatePairAt(b, b.length() - s - 1)) { s--; } return a.subSequence(a.length() - s, a.length()).toString(); } @VisibleForTesting static boolean validSurrogatePairAt(CharSequence string, int index) { return index >= 0 && index <= (string.length() - 2) && Character.isHighSurrogate(string.charAt(index)) && Character.isLowSurrogate(string.charAt(index + 1)); } }