Guava入门~CharMatcher
生活随笔
收集整理的這篇文章主要介紹了
Guava入门~CharMatcher
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
import org.hamcrest.CoreMatchers;
import org.junit.Assert;
import com.google.common.base.CharMatcher;/*** TODO 在此寫上類的相關說明.<br>* @author gqltt<br>* @version 1.0.0 2021年11月11日<br>* @see * @since JDK 1.5.0*/
public class CharMatcherDemo {/*** @param args*/public static void main(String[] args) {replace();collapse();trimAndCollapse();retain();range();}/*** 替換.*/static void replace() {String stringWithLinebreaks = "This is an example\n"+"of a String with linebreaks\n"+"we want on one line";String expected = "This is an example of a String with linebreaks we want on one line";String scrubbed = CharMatcher.breakingWhitespace().replaceFrom(stringWithLinebreaks, ' ');Assert.assertThat(scrubbed, CoreMatchers.is(expected));}/*** 壓縮.*/static void collapse() {String tabsAndSpaces = "String with spaces and tabs";String expected = "String with spaces and tabs";String scrubbed = CharMatcher.whitespace().collapseFrom(tabsAndSpaces, ' ');Assert.assertThat(scrubbed, CoreMatchers.is(expected));}/*** 裁剪&壓縮.*/static void trimAndCollapse() {String tabsAndSpaces = " String with spaces and tabs";String expected = "String with spaces and tabs";String scrubbed = CharMatcher.whitespace().trimAndCollapseFrom(tabsAndSpaces,' ');Assert.assertThat(scrubbed, CoreMatchers.is(expected));}/*** 保留.*/static void retain() {String lettersAndNumbers = "foo989yxbar234";String expected = "989234";String retained = CharMatcher.javaDigit().retainFrom(lettersAndNumbers);Assert.assertThat(expected, CoreMatchers.is(retained));}/*** 字符范圍.*/static void range() {CharMatcher cm = CharMatcher.inRange('A','E');Assert.assertThat(cm.retainFrom("aaaABbbccCdddDEeee"), CoreMatchers.is("ABCDE"));}
}
總結
以上是生活随笔為你收集整理的Guava入门~CharMatcher的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 掌控谈话~谈价格的秘诀
- 下一篇: Spring Data JPA 从入门到