posts - 41, comments - 15, trackbacks - 0, articles - 1
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          StringUtils一般應用

          Posted on 2012-08-30 11:02 yuhaibo736 閱讀(574) 評論(0)  編輯  收藏

          package test;

          import org.apache.commons.lang3.ArrayUtils;
          import org.apache.commons.lang3.StringUtils;

          public class TestStringUtils {
           
           public static void main(String[] args) {
            // 空字符串檢查
            String t1_1 = "";
            String t1_2 = "\n\n\t";
            String t1_3 = null;
            String t1_4 = "Test";
            System.out.println("t1_1 blank? " + StringUtils.isBlank(t1_1));
            System.out.println("t1_2 blank? " + StringUtils.isBlank(t1_2));
            System.out.println("t1_3 blank? " + StringUtils.isBlank(t1_3));
            System.out.println("t1_4 blank? " + StringUtils.isBlank(t1_4));
            // 清除空白字符
            String t2_1 = "\t";
            String t2_2 = "  A  Test  ";
            String t2_3 = null;
            System.out.println("t2_1 trimToNull: " + StringUtils.trimToNull(t2_1));
            System.out.println("t2_2 trimToNull: " + StringUtils.trimToNull(t2_2));
            System.out.println("t2_3 trimToNull: " + StringUtils.trimToNull(t2_3));
            System.out.println("t2_1 trim: " + StringUtils.trim(t2_1));
            System.out.println("t2_2 trim: " + StringUtils.trim(t2_2));
            System.out.println("t2_3 trim: " + StringUtils.trim(t2_3));
            // 取得字符串的縮寫
            String t3_1 = "This is a test of the abbreviation.";
            String t3_2 = "Test";
            System.out.println(StringUtils.abbreviate(t3_1, 15));
            System.out.println(StringUtils.abbreviate(t3_1, 5, 15));
            System.out.println(StringUtils.abbreviate(t3_2, 10));
            // 劈分字符串
            String t4_1 = "A b,c.d|e";
            String t4_2 = "Pharmacy, basketball funky";
            String[] array1 = StringUtils.split(t4_1, " ,.|");
            String[] array2 = StringUtils.split(t4_2, " ,", 2);
            System.out.println(ArrayUtils.toString(array1));
            System.out.println(ArrayUtils.toString(array2));
            // 查找嵌套字符串
            String t5_1 = "ABC1234ABC4567";
            System.out.println(StringUtils.substringBetween(t5_1, "1234", "4567"));
            System.out.println(StringUtils.substringBetween(t5_1, "12345", "4567"));
            // 去除尾部換行符
            String t6_1 = "Hello\n";
            System.out.println(StringUtils.chomp(t6_1));
            String t6_2 = "Another test\r\n";
            System.out.println(StringUtils.chomp(t6_2));
            // 重復字符串
            System.out.println(StringUtils.repeat("*", 10));
            System.out.println(StringUtils.repeat("China ", 5));
            // 顛倒字符串
            System.out.println(StringUtils.reverse("ABCDE"));
            // 判斷字符串內容的類型
            String t7_1 = "Virginia";
            System.out.println("Is state number? " + StringUtils.isNumeric(t7_1));// 全由數(shù)字組成
            System.out.println("Is state alpha? " + StringUtils.isAlpha(t7_1));// 全由字母組成
            System.out.println("Is state alphanumeric? " + StringUtils.isAlphanumeric(t7_1));// 全由數(shù)字或數(shù)字組成
            System.out.println("Is state alphaspace? " + StringUtils.isAlphaSpace(t7_1));// 全由字母或空格組成
            // 取得某字符串在另一字符串中出現(xiàn)的次數(shù)
            System.out.println(StringUtils.countMatches("Chinese People", "e"));
            // 部分截取字符串
            String t8_1 = " 25 * (30,40) [50,60] | 30";
            System.out.print("N0: " + StringUtils.substringBeforeLast(t8_1, "*"));
            System.out.print(", N1: " + StringUtils.substringBetween(t8_1, "(", ","));
            System.out.print(", N2: " + StringUtils.substringBetween(t8_1, ",", ")"));
            System.out.print(", N3: " + StringUtils.substringBetween(t8_1, "[", ","));
            System.out.print(", N4: " + StringUtils.substringBetween(t8_1, ",", "]"));
            System.out.print(", N5: " + StringUtils.substringAfterLast(t8_1, "|"));
           }

          }


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 哈尔滨市| 嘉祥县| 台山市| 同仁县| 荆门市| 鸡泽县| 大冶市| 中方县| 香港| 萨迦县| 济阳县| 乐都县| 永登县| 乌拉特前旗| 台州市| 龙江县| 肥乡县| 青河县| 高雄市| 江川县| 化州市| 南陵县| 叶城县| 柳江县| 阿拉善左旗| 兰州市| 东海县| 泽库县| 永兴县| 鲜城| 贞丰县| 乌兰浩特市| 蓝山县| 盐亭县| 阜平县| 土默特左旗| 宁明县| 桑日县| 西宁市| 鄂伦春自治旗| 堆龙德庆县|