Guava入门~Strings
生活随笔
收集整理的這篇文章主要介紹了
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"));}
}
總結
以上是生活随笔為你收集整理的Guava入门~Strings的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Extract Interface(提炼
- 下一篇: Java并发编程实战~ReadWrite