Guava入门~Strings

import org.hamcrest.CoreMatchers;
import org.junit.Assert;
import com.google.common.base.Strings;

/**
 * TODO 在此写上类的相关说明.<br>
 * @author gqltt<br>
 * @version 1.0.0 2021年11月11日<br>
 * @see 
 * @since JDK 1.5.0
 */
public class StringsDemo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		nullToEmpty();
		tesEmptyToNull();
		isNullOrEmpty();
		padStart();
		padEnd();
		repeat();
		commonPrefix();
		commonSuffix();
	}

	/**
	 * null转空串.
	 */
    static void nullToEmpty() {
    	Assert.assertThat(Strings.nullToEmpty("foo"), CoreMatchers.is("foo"));
    	Assert.assertThat(Strings.nullToEmpty(null), CoreMatchers.is(""));
    }
    
    /**
     * 空串转null.
     */
    static void emptyToNull() {
    	Assert.assertThat(Strings.emptyToNull("foo"), CoreMatchers.is("foo"));
    	Assert.assertThat(Strings.emptyToNull(""), CoreMatchers.is(CoreMatchers.nullValue()));
    	Assert.assertThat(Strings.emptyToNull("  "), CoreMatchers.is("  "));
    }
    
    /**
     * 是否null或空串.
     */
    static void isNullOrEmpty() {
    	Assert.assertThat(Strings.isNullOrEmpty(""), CoreMatchers.is(true));
    	Assert.assertThat(Strings.isNullOrEmpty("  "), CoreMatchers.is(false));
    	Assert.assertThat(Strings.isNullOrEmpty(null), CoreMatchers.is(true));
    	Assert.assertThat(Strings.isNullOrEmpty("foo"), CoreMatchers.is(false));
    }
    
    /**
     * 左填充.
     */
    static void padStart() {
        String expected = "001";
        String returned = Strings.padStart("1", 3, '0');
        Assert.assertThat(returned, CoreMatchers.is(expected));
    }
    
    /**
     * 右填充.
     */
    static void padEnd() {
        String expected = "boom!!";
        String returned = Strings.padEnd("boom", 6, '!');
        Assert.assertThat(returned, CoreMatchers.is(expected));
    }
    
    /**
     * 重复.
     */
    static void repeat() {
    	String result = Strings.repeat("abc", 3);
    	Assert.assertThat(result, CoreMatchers.is("abcabcabc"));
    }
    
    /**
     * 公共前缀.
     */
    static void commonPrefix() {
    	String result = Strings.commonPrefix("abc12345", "abc44544");
    	Assert.assertThat(result, CoreMatchers.is("abc"));
    }
    
    /**
     * 公共后缀.
     */
    static void commonSuffix() {
    	String result = Strings.commonSuffix("12345abc", "44544abc");
    	Assert.assertThat(result, CoreMatchers.is("abc"));
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值