美文网首页
Guava之Strings、Charset、CharMatche

Guava之Strings、Charset、CharMatche

作者: 神豪VS勇士赢 | 来源:发表于2020-08-23 23:44 被阅读0次
      @Test
        public void testStringsMethod() {
            assertThat(Strings.emptyToNull(""), nullValue());
            assertThat(Strings.nullToEmpty(null), equalTo(""));
            assertThat(Strings.nullToEmpty("hello"), equalTo("hello"));
            assertThat(Strings.commonPrefix("Hello", "Hit"), equalTo("H"));
            assertThat(Strings.commonPrefix("Hello", "Xit"), equalTo(""));
            assertThat(Strings.commonSuffix("Hello", "Echo"), equalTo("o"));
            assertThat(Strings.repeat("Alex", 3), equalTo("AlexAlexAlex"));
            assertThat(Strings.isNullOrEmpty(null), equalTo(true));
            assertThat(Strings.isNullOrEmpty(""), equalTo(true));
    
            assertThat(Strings.padStart("Alex", 3, 'H'), equalTo("Alex"));
            assertThat(Strings.padStart("Alex", 5, 'H'), equalTo("HAlex"));
            assertThat(Strings.padEnd("Alex", 5, 'H'), equalTo("AlexH"));
        }
     @Test
        public void testCharsets() {
            Charset charset = Charset.forName("UTF-8");
            assertThat(Charsets.UTF_8, equalTo(charset));
        }
    
        /**
         * functor
         */
        @Test
        public void testCharMatcher() {
            assertThat(CharMatcher.javaDigit().matches('5'), equalTo(true));
            assertThat(CharMatcher.javaDigit().matches('x'), equalTo(false));
    
            assertThat(CharMatcher.is('A').countIn("Alex Sharing the Google Guava to Us"), equalTo(1));
            assertThat(CharMatcher.breakingWhitespace().collapseFrom("      hello Guava     ", '*'), equalTo("*hello*Guava*"));
            assertThat(CharMatcher.javaDigit().or(CharMatcher.whitespace()).removeFrom("hello 234 world"), equalTo("helloworld"));
            assertThat(CharMatcher.javaDigit().or(CharMatcher.whitespace()).retainFrom("hello 234 world"), equalTo(" 234 "));
    
        }
    

    相关文章

      网友评论

          本文标题:Guava之Strings、Charset、CharMatche

          本文链接:https://www.haomeiwen.com/subject/hxqgjktx.html