Strings为 关于字符串和字符的静态工具方法
1.私有构造器
private Strings() {}
静态方式使用strings,所以不需要使用权限为public的构造器,设置为private更合理。
2.nullToEmpty方法
public static String nullToEmpty(@Nullable String string) {
return (string == null) ? "" : string;
}
判断字符串是否为null,若是则转换null为空串。
3.isNullEmpty
public static boolean isNullOrEmpty(@Nullable String string) {
return Platform.stringIsNullOrEmpty(string);
}
判断字符串是否为null或空字符串
4.emptyToNull
public static String emptyToNull(@Nullable String string) {
return isNullOrEmpty(string) ? null : string;
}
将空字符串转换为NULL
5.padStart
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();
}
将string 用padChar填充到最小长度minlength padChar填充在字符串前 prepend
6.padEnd
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 用padChar填充到最小长度minlength padChar填充在字符串尾 append
7.repeat
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);
}
//将之前放入字符数组中的字符拓展到原来的两倍
//例如 第一次运行 为double
第二次运行 double的double
直到n×2 > size 放入最后一个 或者 相等不放
System.arraycopy(array, 0, array, n, size - n);
return new String(array);
}
Returns a string consisting of a specific number of concatenated copies of an input string
返回一个包含有明确重复组合次数的输入字符串
需要输入一个非空字符串 重复次数
8.comonPrefix
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();
}
返回两个字符串相同的前缀 字符串从开头开始比较
commonSuffix
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();
}
返回两个字符串相同的后缀 字符串从结尾开始比较
// 判断最后两个字符是不是合法的“Java 平台增补字符”
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));
}
}
网友评论