Guava源码阅读 - Strings

简介

静态实用方法,属于String或者CharSequence实例。

方法

nullToEmpty

public static String nullToEmpty(@NullableDecl String string)

返回字符串非null,返回原字符串。否则返回空字符串。

emptyToNull

@NullableDecl
public static String emptyToNull(@NullableDecl String string)

返回字符串非空字符串,返回原字符串。否则,返回null。

isNullOrEmpty

public static boolean isNullOrEmpty(@NullableDecl String string)

判断字符串是不是null或者空字符串。

padStart/padEnd

public static String padStart(String string, int minLength, char padChar)
public static String padEnd(String string, int minLength, char padChar)

返回字符串,长度最少是minLength,长度不够的话用重复的padChar填充。

repeat

public static String repeat(String string, int count)

返回string重复count次。

commonPrefix/commonSuffix

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"
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值