com.google.common.base ** Strings**
静态实用方法,属于String或者CharSequence实例。
public static String nullToEmpty(@NullableDecl String string)
返回字符串非null,返回原字符串。否则返回空字符串。
@NullableDecl
public static String emptyToNull(@NullableDecl String string)
返回字符串非空字符串,返回原字符串。否则,返回null。
public static boolean isNullOrEmpty(@NullableDecl String string)
判断字符串是不是null或者空字符串。
public static String padStart(String string, int minLength, char padChar)
public static String padEnd(String string, int minLength, char padChar)
返回字符串,长度最少是minLength,长度不够的话用重复的padChar填充。
public static String repeat(String string, int count)
返回string重复count次。
public static String commonPrefix(CharSequence a, CharSequence b)
public static String commonSuffix(CharSequence a, CharSequence b)
返回共同的前缀/后缀字符串。
public class StringTest {
public static void main(String[] args) {
String s = "hello world";
String e = "";
String n = null;
System.out.println(Strings.nullToEmpty(s)); // "hello world"
System.out.println(Strings.nullToEmpty(e)); // ""
System.out.println(Strings.nullToEmpty(n)); // ""
System.out.println(Strings.emptyToNull(s)); // "hello world"
System.out.println(Strings.emptyToNull(e)); // null
System.out.println(Strings.emptyToNull(n)); // null
System.out.println(Strings.isNullOrEmpty(s)); // false
System.out.println(Strings.isNullOrEmpty(e)); // true
System.out.println(Strings.isNullOrEmpty(n)); // true
System.out.println(Strings.padStart("xx", 4, 'o')); // "ooxx"
System.out.println(Strings.padEnd("xx", 4, 'o')); // "xxoo"
System.out.println(Strings.repeat("xx", 3)); // "xxxxxx"
System.out.println(Strings.commonPrefix("oooo", "ooxx")); // "oo"
System.out.println(Strings.commonSuffix("xxxx", "ooxx")); // "xx"
}
}