月亮的太陽(yáng)

          小乖的BLOG
          posts - 114, comments - 41, trackbacks - 0, articles - 27
            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理
          檢查字符串是否為空或null或僅僅包含空格
            String test = "";
            String test1=" ";
            String test2 = "\n\n\t";
            String test3 = null;
            System.out.println( "test blank? " + StringUtils.isBlank( test ) );
            System.out.println( "test1 blank? " + StringUtils.isBlank( test1 ) );
            System.out.println( "test2 blank? " + StringUtils.isBlank( test2 ) );
            System.out.println( "test3 blank? " + StringUtils.isBlank( test3 ) );
            運(yùn)行結(jié)果:
            test blank? true
            test1 blank? true
            test2 blank? true
            test3 blank? true
            相對(duì)應(yīng)的還有一個(gè)StringUtils.isNotBlank(String str)
            StringUtils.isEmpty(String str)則檢查字符串是否為空或null(不檢查是否僅僅包含空格)
           
            分解字符串
            StringUtils.split(null, *, *)            = null
            StringUtils.split("", *, *)              = []
            StringUtils.split("ab de fg", null, 0)   = ["ab", "cd", "ef"]
            StringUtils.split("ab   de fg", null, 0) = ["ab", "cd", "ef"]
            StringUtils.split("ab:cd:ef", ":", 0)    = ["ab", "cd", "ef"]
            StringUtils.split("ab:cd:ef", ":", 1)    = ["ab:cd:ef"]
            StringUtils.split("ab:cd:ef", ":", 2)    = ["ab", "cd:ef"]
            StringUtils.split(String str,String separatorChars,int max) str為null時(shí)返回null
            separatorChars為null時(shí)默認(rèn)為按空格分解,max為0或負(fù)數(shù)時(shí)分解沒(méi)有限制,max為1時(shí)返回整個(gè)字符串,max為分解成的個(gè)數(shù)(大于實(shí)際則無(wú)效)
           
            去除字符串前后指定的字符
            StringUtils.strip(null, *)          = null
            StringUtils.strip("", *)            = ""
            StringUtils.strip("abc", null)      = "abc"
            StringUtils.strip(" abc ", null)    = "abc"
            StringUtils.strip("  abcyx", "xyz") = "  abc"
            StringUtils.strip(String str,String stripChars) str為null時(shí)返回null,stripChars為null時(shí)默認(rèn)為空格

            創(chuàng)建醒目的Header(調(diào)試時(shí)用)
            public String createHeader( String title ) {
              int width = 30;
              String stars = StringUtils.repeat( "*", width);
              String centered = StringUtils.center( title, width, "*" );
              String heading = StringUtils.join(new Object[]{stars, centered, stars}, "\n");
              return heading;
            }
            調(diào)用createHeader("TEST")的輸出結(jié)果:
            ******************************
            ************ TEST ************
            ******************************

            字符的全部反轉(zhuǎn)及以單個(gè)詞為單位的反轉(zhuǎn)
            String original = "In time, I grew tired of his babbling nonsense.";
            StringUtils.reverse( original )   = ".esnesnon gnilbbab sih fo derit werg I ,emit nI"
            以單個(gè)詞為單位的反轉(zhuǎn)
            public Sentence reverseSentence(String sentence) {
              String reversed = StringUtils.chomp( sentence, "." );
              reversed = StringUtils.reverseDelimited( reversed, ' ' );
              reversed = reversed + ".";
              return reversed;
            }
            String sentence = "I am Susan."
            reverseSentence( sentence ) )   = "Susan am I."

            檢查字符串是否僅僅包含數(shù)字、字母或數(shù)字和字母的混合
            String test1 = "ORANGE";
            String test2 = "ICE9";
            String test3 = "ICE CREAM";
            String test4 = "820B Judson Avenue";
            String test5 = "1976";
            結(jié)果:
            boolean t1val = StringUtils.isAlpha( test1 ); // returns true
            boolean t2val = StringUtils.isAlphanumeric( test2 ); // returns true
            boolean t3val = StringUtils.isAlphaSpace( test3 ); // returns true
            boolean t4val = StringUtils.isAlphanumericSpace( test4 ); // returns true
            boolean t5val = StringUtils.isNumeric( test5 ); // returns true

          主站蜘蛛池模板: 海兴县| 灵宝市| 马鞍山市| 开平市| 信丰县| 华蓥市| 苗栗县| 平果县| 武山县| 凉山| 油尖旺区| 山东| 宾川县| 毕节市| 龙门县| 蚌埠市| 赤峰市| 金沙县| 五常市| 民丰县| 慈溪市| 深水埗区| 安义县| 新晃| 武乡县| 宁海县| 旌德县| 阳东县| 白银市| 鲜城| 澎湖县| 喀什市| 前郭尔| 高清| 阿图什市| 旬邑县| 十堰市| 莆田市| 尤溪县| 天镇县| 堆龙德庆县|