<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    鷹翔宇空

    學(xué)習(xí)和生活

    BlogJava 首頁(yè) 新隨筆 聯(lián)系 聚合 管理
      110 Posts :: 141 Stories :: 315 Comments :: 1 Trackbacks

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

    StringUtils類(lèi)使用

    檢查字符串是否為空或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

    ArrayUtils類(lèi)使用

    primitive 數(shù)組克隆及反轉(zhuǎn)排序
    ??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 ) );
    ??
    ??對(duì)象數(shù)組克隆及反轉(zhuǎn)排序
    ??Long[] array = new Long[] { new Long(3), new Long(56), new Long(233) };
    ??Long[] reversed = ArrayUtils.clone( array );
    ??ArrayUtils.reverse( reversed );
    ??
    ??primitive 數(shù)組與對(duì)象數(shù)組之間的轉(zhuǎn)換
    ??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 );
    ??注意:對(duì)象數(shù)組可以含有null元素,primitive 數(shù)組則不容許含有null元素,所以對(duì)象數(shù)組轉(zhuǎn)換為primitive 數(shù)組時(shí),可以添入第二個(gè)參數(shù),當(dāng)碰到為null的元素時(shí)用其代替(如下,Double.NaN)。如果不添入第二個(gè)參數(shù),當(dāng)碰到為null的元素時(shí),則會(huì)拋出NullPointerException 。
    ??double[] result = ArrayUtils.toPrimitive( resultObjArray, Double.NaN??);
    ??
    ??查找一個(gè)數(shù)組中是否含有特定的元素(查找對(duì)象數(shù)組時(shí),比較的是對(duì)象的equals()方法),及特定元素的第一次出現(xiàn)位置和最后一次出現(xiàn)位置
    ??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" );??
    ??
    ??由二維對(duì)象數(shù)組創(chuàng)建一個(gè) 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" );
    ??注意:當(dāng)二維對(duì)象數(shù)組"key"值重復(fù)時(shí),創(chuàng)建的Map,后面的鍵-值對(duì)會(huì)把前面的覆蓋掉

    posted on 2006-05-30 14:09 TrampEagle 閱讀(2152) 評(píng)論(1)  編輯  收藏 所屬分類(lèi): java

    Feedback

    # re: StringUtils類(lèi)使用[未登錄](méi) 2008-08-02 11:14 moonandsun
    沒(méi)用過(guò)。  回復(fù)  更多評(píng)論
      

    主站蜘蛛池模板: 久久国产免费观看精品| 亚洲s色大片在线观看| 最近中文字幕无免费| 黄色一级视频免费| 亚洲人成综合在线播放| 国产亚洲精品a在线观看app | 亚洲AV无码久久精品蜜桃| 永久免费观看的毛片的网站| 3d成人免费动漫在线观看| 成人免费777777被爆出| 三级片免费观看久久| 亚洲AV成人一区二区三区在线看| 亚洲AV无码国产在丝袜线观看| 亚洲男人的天堂在线va拉文| 日本免费一二区在线电影 | 亚洲免费二区三区| 久久噜噜噜久久亚洲va久| 久久亚洲精品无码观看不卡| 国产hs免费高清在线观看| 性色av免费观看| 最新中文字幕电影免费观看| 男女做羞羞的事视频免费观看无遮挡| 99免费在线观看视频| 免费91最新地址永久入口 | 又黄又爽一线毛片免费观看 | 亚洲人成未满十八禁网站| 亚洲av永久综合在线观看尤物| 97se亚洲综合在线| 亚洲精选在线观看| 亚洲视频在线一区| 亚洲国产精品线在线观看| 亚洲AV无码一区二区二三区入口 | 色天使色婷婷在线影院亚洲| 色综合久久精品亚洲国产| 国产精品亚洲а∨天堂2021| 香蕉视频亚洲一级| 无码日韩人妻AV一区免费l| 国产免费人成视频尤勿视频| 东北美女野外bbwbbw免费| 久久免费观看国产精品| 99精品视频在线观看免费播放|