鷹翔宇空

          學習和生活

          BlogJava 首頁 新隨筆 聯系 聚合 管理
            110 Posts :: 141 Stories :: 315 Comments :: 1 Trackbacks

          公告

          歡迎和大家成為朋友!
          歡迎和大家就任何技術進行交流!
          QQ:77542728
          MSN:hnlyhyq@hotmail.com
          Email:hnlyhyq@163.com

          常用鏈接

          留言簿(16)

          隨筆分類(107)

          隨筆檔案(109)

          文章分類(145)

          文章檔案(141)

          收藏夾(22)

          java

          javascript

          jdbc

          web

          其它

          開源項目

          我的好友

          最新隨筆

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          原文引自:http://www.3doing.net/forums/dispbbs.asp?boardID=57&ID=1351&page=1

          StringUtils類使用

          檢查字符串是否為空或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 ) );
          ??運行結果:
          ??test blank? true
          ??test1 blank? true
          ??test2 blank? true
          ??test3 blank? true
          ??相對應的還有一個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時返回null
          ??separatorChars為null時默認為按空格分解,max為0或負數時分解沒有限制,max為1時返回整個字符串,max為分解成的個數(大于實際則無效)
          ??
          ??去除字符串前后指定的字符
          ??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時返回null,stripChars為null時默認為空格

          ??創建醒目的Header(調試時用)
          ??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;
          ??}
          ??調用createHeader("TEST")的輸出結果:
          ??******************************
          ??************ TEST ************
          ??******************************

          ??字符的全部反轉及以單個詞為單位的反轉
          ??String original = "In time, I grew tired of his babbling nonsense.";
          ??StringUtils.reverse( original )?? = ".esnesnon gnilbbab sih fo derit werg I ,emit nI"
          ??以單個詞為單位的反轉
          ??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."

          ??檢查字符串是否僅僅包含數字、字母或數字和字母的混合
          ??String test1 = "ORANGE";
          ??String test2 = "ICE9";
          ??String test3 = "ICE CREAM";
          ??String test4 = "820B Judson Avenue";
          ??String test5 = "1976";
          ??結果:
          ??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

          ArrayUtils類使用

          primitive 數組克隆及反轉排序
          ??long[] array = { 1, 3, 2, 3, 5, 6 };
          ??long[] reversed = ArrayUtils.clone( array );
          ??ArrayUtils.reverse( reversed );
          ??System.out.println( "Original: " + ArrayUtils.toString( array ) );?? //打印
          ??System.out.println( "Reversed: " + ArrayUtils.toString( reversed ) );
          ??
          ??對象數組克隆及反轉排序
          ??Long[] array = new Long[] { new Long(3), new Long(56), new Long(233) };
          ??Long[] reversed = ArrayUtils.clone( array );
          ??ArrayUtils.reverse( reversed );
          ??
          ??primitive 數組與對象數組之間的轉換
          ??long[] primitiveArray = new long[] { 12, 100, 2929, 3323 };
          ??Long[] objectArray = ArrayUtils.toObject( primitiveArray );
          ??Double[] doubleObjects = new Double[] { new Double( 3.22, 5.222, 3.221 ) };
          ??double[] doublePrimitives = ArrayUtils.toPrimitive( doubleObject );
          ??注意:對象數組可以含有null元素,primitive 數組則不容許含有null元素,所以對象數組轉換為primitive 數組時,可以添入第二個參數,當碰到為null的元素時用其代替(如下,Double.NaN)。如果不添入第二個參數,當碰到為null的元素時,則會拋出NullPointerException 。
          ??double[] result = ArrayUtils.toPrimitive( resultObjArray, Double.NaN??);
          ??
          ??查找一個數組中是否含有特定的元素(查找對象數組時,比較的是對象的equals()方法),及特定元素的第一次出現位置和最后一次出現位置
          ??String[] stringArray = { "Red", "Orange", "Blue", "Brown", "Red" };
          ??boolean containsBlue = ArrayUtils.contains( stringArray, "Blue" );
          ??int indexOfRed = ArrayUtils.indexOf( stringArray, "Red");
          ??int lastIndexOfRed = ArrayUtils.lastIndexOf( string, "Red" );??
          ??
          ??由二維對象數組創建一個 Map
          ??Object[] weightArray =
          ????new Object[][] { {"H" , new Double( 1.007)},
          ???????????????????? {"He", new Double( 4.002)},
          ???????????????????? {"Li", new Double( 6.941)},
          ???????????????????? {"Be", new Double( 9.012)},
          ???????????????????? {"B",??new Double(10.811)},
          ???????????????????? {"C",??new Double(12.010)},
          ???????????????????? {"N",??new Double(14.007)},
          ???????????????????? {"O",??new Double(15.999)},
          ???????????????????? {"F",??new Double(18.998)},
          ???????????????????? {"Ne", new Double(20.180)} };

          ??Map weights = ArrayUtils.toMap( weightArray );
          ??Double hydrogenWeight = (Double)weights.get( "H" );
          ??注意:當二維對象數組"key"值重復時,創建的Map,后面的鍵-值對會把前面的覆蓋掉

          posted on 2006-05-30 14:09 TrampEagle 閱讀(2157) 評論(1)  編輯  收藏 所屬分類: java

          Feedback

          # re: StringUtils類使用[未登錄] 2008-08-02 11:14 moonandsun
          沒用過。  回復  更多評論
            

          主站蜘蛛池模板: 内丘县| 衡阳市| 手机| 松潘县| 凌云县| 七台河市| 城口县| 本溪市| 康定县| 旺苍县| 南涧| 隆化县| 津南区| 桓仁| 略阳县| 特克斯县| 南郑县| 金乡县| 泗洪县| 大同市| 石棉县| 利川市| 运城市| 万山特区| 苍山县| 通州区| 霍山县| 包头市| 宁强县| 漠河县| 松滋市| 板桥市| 沁阳市| 晋江市| 秦安县| 涞源县| 尤溪县| 屏南县| 龙岩市| 海门市| 乌审旗|