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

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

    作者:吕世昊

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

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

  • 相关阅读:
    Permutation Test 置换检验
    计算机会议排名等级
    国际顶级计算机会议
    机器学习中的范数规则化 L0、L1与L2范数 核范数与规则项参数选择
    岭回归(Ridge Regression)
    popupWindow使用timePicker时点击出现闪屏问题的解决办法
    Java:单例模式的七种写法
    JSONObject遍历获取键值方法合并两个JSONObject
    解决android studio上“com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65935”问题
    解决同时共用MOB公司的shareSDK和SMSSDK的冲突问题
  • 原文地址:https://www.cnblogs.com/lvshihao/p/15162298.html
Copyright © 2011-2022 走看看