com.google.common.base:基本工具类库和接口。
import com.google.common.annotations.VisibleForTesting;
public final class Strings {
private Strings() {
}
public static boolean isNullOrEmpty(@Nullable String string) {
// string.isEmpty() in Java 6
// 判断 == 0 的情况时 应先判断是否为空
return string == null || string.length() == 0;
}
//重复添加单个元素到字符串开头,直到该字符串长度为minLength中,通过StringBuilder.append实现的
public static String padStart(String string, int minLength, char padChar) {
checkNotNull(string); // eager for GWT.
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();
}
//与上个方法类似,重复添加到结尾
public static String padEnd(String string, int minLength, char padChar) {
checkNotNull(string); // eager for GWT.
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();
}
//将String重复count次
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;
}
// IF YOU MODIFY THE CODE HERE, you must update StringsRepeatBenchmark
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];
string.getChars(0, len, array, 0);
int n;
//巧妙的算法设计 每次尽可能多的添加元素
for (n = len; n < size - n; n <<= 1) {
System.arraycopy(array, 0, array, n, n);
}
//当不足以在添加N个元素时跳出循环,再添加(size - n)个元素
System.arraycopy(array, 0, array, n, size - n);
return new String(array);
}
//repeat中使用的arraycopy方法上有关键词native,
//表示这个方法是使用汇编或者其他底层语言实现过的。所以肯定要比我们自己写的Java代码效率上要高
public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length);
//寻找公共前缀
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();
}
//寻找公共后缀
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));
}
}