Guava入门第五章(Strings)
Strings详细介绍
package com.lvshihao.guava;
import com.google.common.base.CharMatcher;
import com.google.common.base.Charsets;
import com.google.common.base.Strings;
import org.junit.Test;
import java.nio.charset.Charset;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.hamcrest.core.IsNull.nullValue;
import static org.junit.Assert.assertThat;
/**
*@author: LVSHIHAO
*@description: GUAVA Strings detailed introduction
*/
public class StringsTest {
@Test
public void testGuavaStringsMethod(){
/**
* emptyToNull() if incoming String is it Empty , is Empty Then just return null
*/
assertThat(Strings.emptyToNull(""),nullValue());
/**
* nullToEmpty() if incoming String is it null , is null Then just return ""
*/
assertThat(Strings.nullToEmpty(null),equalTo(""));
/**
* commonPrefix() compare two String Common characters for prefix conduct return
*/
assertThat(Strings.commonPrefix("lvshihao","lvxu"),equalTo("lv"));
/**
* commonSuffix() compare two String Common characters for suffix conduct return
*/
assertThat(Strings.commonSuffix("lvshihao","hello"),equalTo("o"));
/**
* isNullOrEmpty() if incoming args is null or "" but return true
*/
assertThat(Strings.isNullOrEmpty(null),equalTo(true));
assertThat(Strings.isNullOrEmpty(""),equalTo(true));
/**
* repeat() repeat append this String several times
*/
assertThat(Strings.repeat("lvshihao",2),equalTo("lvshihaolvshihao"));
/**
* padStart() String length less that minLength but prefix append Less n characters
*/
assertThat(Strings.padStart("lvshihao",10,'6'),equalTo("66lvshihao"));
/**
* padEnd() String length less that minLength but suffix append Less n characters
*/
assertThat(Strings.padEnd("lvshihao",10,'6'),equalTo("lvshihao66"));
}
@Test
public void testGuavaCharsetsMethod(){
/**
* Charsets Encoding constants can be obtained through Charsets
*/
Charset charset = Charset.forName("UTF-8");
assertThat(Charsets.UTF_8,equalTo(charset));
}
@Test
public void testCharMatcher(){
/**
* CharMatcher.javaDigit().matches()
* Through this method, you can match whether the character is a number, and return true if it is
*/
assertThat(CharMatcher.javaDigit().matches('5'),equalTo(true));
/**
* CharMatcher.is().countIn()
* Through this method, you can check how many times this character appears in the string
*/
assertThat(CharMatcher.is('A').countIn("Alex the Google Guava to Us"),equalTo(1));
/**
* CharMatcher.breakingWhitespace().collapseFrom()
* Replace spaces with specified characters
*/
assertThat(CharMatcher.breakingWhitespace().collapseFrom(" hello lvshihao",' '),equalTo(" hello lvshihao"));
/**
* .digit().or(CharMatcher.whitespace()).removeFrom()
* remove number And Spaces In The String
*/
assertThat(CharMatcher.digit().or(CharMatcher.whitespace()).removeFrom("hello 234 world "),equalTo("helloworld"));
/**
* .digit().or(CharMatcher.whitespace()).removeFrom()
* retain number And Spaces In The String
*/
assertThat(CharMatcher.digit().or(CharMatcher.whitespace()).retainFrom("hello 234 world "),equalTo(" 234 "));
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
作者:吕世昊
个性签名:学习如逆水行舟,不进则退!
如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!