zoukankan      html  css  js  c++  java
  • 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  "));
    
        }
    }
    

    ---------------------------------------------------------------------------------------------------------------------------------------------------------------------

    作者:吕世昊

    个性签名:学习如逆水行舟,不进则退!

    如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!

  • 相关阅读:
    idea 界面乱码问题 file was loaded in the wrong enconding:"utf-8"
    svn 下载,安装,创建库,设置用户和用户组,赋权限
    eclipse文件中的乱码问题
    Eclipse安装Spring插件springsource-tool-suite
    vue.js2.0:搭建开发环境及构建项目
    排序List集合中的元素
    Java GC机制和对象Finalize方法的一点总结
    xfire发布的Webservice中Spring注入为空的解决方案
    Http报文格式学习及Get和Post主要区别总结
    [转] tomcat组成及工作原理
  • 原文地址:https://www.cnblogs.com/lvshihao/p/15162298.html
Copyright © 2011-2022 走看看