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  "));
    
        }
    }
    

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

    作者:吕世昊

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

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

  • 相关阅读:
    linux/unix下 pid文件作用浅析
    gunicorn启动django时静态文件的加载
    分享30道Redis面试题,面试官能问到的我都找到了
    python利用mongodb上传图片数据 : GridFS 与 bson两种方式
    GridFS大文件的添加、获取、查看、删除
    你真的懂redis吗?
    mongoDB的复制集5----复制集安全(认证,用户,权限)
    MongoDB复制集安全认证
    MongoDB 用户名密码登录
    MongoDB 分片
  • 原文地址:https://www.cnblogs.com/lvshihao/p/15162298.html
Copyright © 2011-2022 走看看