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
最新推荐文章于 2024-01-09 17:15:44 发布