Java常用工具類(二)org.apache.commons.lang__Java

來源:互聯網
上載者:User
/**
* 1、字串的空判斷  org.aphche.commons.lang.StringUtils

*/

                // Checks if a String is empty ("") or null.System.out.println(StringUtils.isEmpty(null)); // trueSystem.out.println(StringUtils.isEmpty("")); // trueSystem.out.println(StringUtils.isEmpty(" ")); // falseSystem.out.println(StringUtils.isEmpty("bob")); // falseSystem.out.println(StringUtils.isEmpty("  bob  ")); // false// Checks if a String is whitespace, empty ("") or null.System.out.println(StringUtils.isBlank(null)); // trueSystem.out.println(StringUtils.isBlank("")); // trueSystem.out.println(StringUtils.isBlank(" ")); // trueSystem.out.println(StringUtils.isBlank("bob")); // falseSystem.out.println(StringUtils.isBlank("  bob  ")); // false

/**
* 2、字串的Trim
*/

// The String is trimmed using String.trim(). Trim removes start and end characters <= 32. To strip whitespace use strip(String).System.out.println(StringUtils.trim(null)); // nullSystem.out.println(StringUtils.trim("")); // ""System.out.println(StringUtils.trim("     ")); // ""System.out.println(StringUtils.trim("abc")); // "abc"System.out.println(StringUtils.trim("    abc")); // "abc"System.out.println(StringUtils.trim("    abc  ")); // "abc"System.out.println(StringUtils.trim("    ab c  ")); // "ab c"// Strips whitespace from the start and end of a String.System.out.println(StringUtils.strip(null)); // nullSystem.out.println(StringUtils.strip("")); // ""System.out.println(StringUtils.strip("   ")); // ""System.out.println(StringUtils.strip("abc")); // "abc"System.out.println(StringUtils.strip("  abc")); // "abc"System.out.println(StringUtils.strip("abc  ")); // "abc"System.out.println(StringUtils.strip(" abc ")); // "abc"System.out.println(StringUtils.strip(" ab c ")); // "ab c"// strip(String,String);System.out.println(StringUtils.strip("  abcyx", "xyz")); // "  abc"System.out.println(StringUtils.strip(null, "*")); // nullSystem.out.println(StringUtils.strip("", "*"));// ""System.out.println(StringUtils.strip("abc", null)); // "abc"System.out.println(StringUtils.strip("  abc", null)); // "abc"System.out.println(StringUtils.strip("abc  ", null)); // "abc"System.out.println(StringUtils.strip(" abc ", null)); // "abc"System.out.println(StringUtils.strip("  abcyx", "xyz")); // "  abc"// Strips any of a set of characters from the start of a String.System.out.println(StringUtils.stripStart("yxabcxyz  ", "xyz")); // "abcxyz  "// Strips any of a set of characters from the end of a String.System.out.println(StringUtils.stripEnd("  xyzabcyx", "xyz")); // "  xyzabc"


/**
* 3、字串的分割 
*/

//預設半形空格分割  String str1 = "aaa bbb ccc";String[] dim1 = StringUtils.split(str1); // => ["aaa", "bbb", "ccc"]System.out.println(StringUtils.split("abc def")); // ["abc", "def"]System.out.println(StringUtils.split("abc  def")); // ["abc", "def"]System.out.println(StringUtils.split(" abc ")); // ["abc"]  //指定分隔字元  String str2 = "aaa,bbb,ccc";  String[] dim2 = StringUtils.split(str2, ","); // => ["aaa", "bbb", "ccc"]    //去除Null 字元串  String str3 = "aaa,,bbb";  String[] dim3 = StringUtils.split(str3, ","); // => ["aaa", "bbb"]    System.out.println(dim3.length);//2  System.out.println(dim3[0]);//"aaa"  System.out.println(dim3[1]);//"bbb"    //包含Null 字元串  String str4 = "aaa,,bbb";  String[] dim4 = StringUtils.splitPreserveAllTokens(str4, ","); // => ["aaa", "", "bbb"]  System.out.println(StringUtils.splitPreserveAllTokens("::cd:ef", ":"));// ["", "", cd", "ef"]System.out.println(StringUtils.splitPreserveAllTokens(":cd:ef:", ":"));// ["", cd", "ef", ""]  //Splits the provided text into an array with a maximum length.//A zero or negative value implies no limitString str5 = "aaa,bbb,ccc";  String[] dim5 = StringUtils.split(str5, ",", 2); // => ["aaa", "bbb,ccc"]  System.out.println(StringUtils.split("ab:cd:ef", ":", 0));//["ab", "cd", "ef"]System.out.println(StringUtils.split("ab:cd:ef", ":", 2));//["ab", "cd:ef"]


/**
* 4、字串的串連 
*/

//數組元素拼接  String[] array = {"aaa", "bbb", "ccc"};  StringUtils.join(array, ",");   //"aaa,bbb,ccc"  StringUtils.join(array, "--");// "aaa--bbb--ccc"  StringUtils.join(array, "");//"aaabbbccc"//集合元素拼接,the separator character to use, null treated as "".  List<String> list = new ArrayList<String>();  list.add("aaa");  list.add("bbb");  list.add("ccc");  StringUtils.join(list, ","); //"aaa,bbb,ccc" 

/**
* 5、字串的Escape  org.apache.commons.lang.StringEscapeUtils
*/

System.out.println(StringEscapeUtils.escapeCsv("測試測試哦"));//"測試測試哦"  System.out.println(StringEscapeUtils.escapeCsv("測試,測試哦"));//"\"測試,測試哦\""  System.out.println(StringEscapeUtils.escapeCsv("測試\n測試哦"));//"\"測試\n測試哦\""    //System.out.println(StringEscapeUtils.escapeHtml4("測試測試哦"));//"<p>測試測試哦</p>"  System.out.println(StringEscapeUtils.escapeJava("\"rensaninng\",歡迎您。"));//"\"rensaninng\"\uFF0C\u6B22\u8FCE\u60A8\uFF01"    //System.out.println(StringEscapeUtils.escapeEcmaScript("測試'測試哦"));//"\u6D4B\u8BD5\'\u6D4B\u8BD5\u54E6"  System.out.println(StringEscapeUtils.escapeXml("<tt>\"bread\" & \"butter\"</tt>"));//"<tt>"bread" & "butter"</tt>"  

/**
* 6、隨機數   org.apache.commons.lang.RandomStringUtils
*/
// 10位英字  System.out.println(RandomStringUtils.randomAlphabetic(10));    // 10位英數  System.out.println(RandomStringUtils.randomAlphanumeric(10));    // 10位ASCII碼  System.out.println(RandomStringUtils.randomAscii(10));    // 指定文字10位  System.out.println(RandomStringUtils.random(10, "abcde")); 

/**
* 7、數組   org.apache.commons.lang.ArrayUtils
*/
// 追加元素到數組尾部  int[] array1 = {1, 2};  array1 = ArrayUtils.add(array1, 3); // => [1, 2, 3]    System.out.println(array1.length);//3  System.out.println(array1[2]);//3    // 刪除指定位置的元素  int[] array2 = {1, 2, 3};  array2 = ArrayUtils.remove(array2, 2); // => [1, 2]    System.out.println(array2.length);//2    // 截取部分元素   The start index is inclusive, the end index exclusive.int[] array3 = {1, 2, 3, 4};  array3 = ArrayUtils.subarray(array3, 1, 3); // => [2, 3]    System.out.println(array3.length);//2    // 數組拷貝  String[] array4 = {"aaa", "bbb", "ccc"};  String[] copied = (String[]) ArrayUtils.clone(array4); // => {"aaa", "bbb", "ccc"}            System.out.println(copied.length);//3           // 判斷是否包含某元素  Checks if the object is in the given array.String[] array5 = {"aaa", "bbb", "ccc", "bbb"};  boolean result1 = ArrayUtils.contains(array5, "bbb"); // => true       System.out.println(result1);//true    // 判斷某元素在數組中出現的位置(從前往後,沒有返回-1)  int result2 = ArrayUtils.indexOf(array5, "bbb"); // => 1       System.out.println(result2);//1    // 判斷某元素在數組中出現的位置(從後往前,沒有返回-1)  int result3 = ArrayUtils.lastIndexOf(array5, "bbb"); // => 3  System.out.println(result3);//3    // 數組轉Map  Map<Object, Object> map = ArrayUtils.toMap(new String[][]{      {"key1", "value1"},      {"key2", "value2"}  });  System.out.println(map.get("key1"));//"value1"  System.out.println(map.get("key2"));//"value2"    // 判斷數組是否為空白  Object[] array61 = new Object[0];  Object[] array62 = null;  Object[] array63 = new Object[]{"aaa"};    System.out.println(ArrayUtils.isEmpty(array61));//true   Checks if an array of Objects is empty or null.System.out.println(ArrayUtils.isEmpty(array62));//true  System.out.println(ArrayUtils.isNotEmpty(array63));//true  true if the array is not empty or not null  // 判斷數組長度是否相等  Object[] array71 = new Object[]{"aa", "bb", "cc"};  Object[] array72 = new Object[]{"dd", "ee", "ff"};    System.out.println(ArrayUtils.isSameLength(array71, array72));//true    // 判斷數組元素內容是否相等  Object[] array81 = new Object[]{"aa", "bb", "cc"};  Object[] array82 = new Object[]{"aa", "bb", "cc"};    System.out.println(ArrayUtils.isEquals(array81, array82));    // Integer[] 轉化為 int[]  Integer[] array9 = new Integer[]{1, 2};  int[] result = ArrayUtils.toPrimitive(array9);    System.out.println(result.length);//2  System.out.println(result[0]);//1    // int[] 轉化為 Integer[]   int[] array10 = new int[]{1, 2};  Integer[] result10 = ArrayUtils.toObject(array10);    System.out.println(result.length);//2  System.out.println(result10[0].intValue());//1

/**
* 8、日期    org.apache.commons.lang.time.DateUtils
*/

// 產生Date對象  Date date = DateUtils.parseDate("2010/01/01 11:22:33", new String[]{"yyyy/MM/dd HH:mm:ss"});    // 10天后  Date tenDaysAfter = DateUtils.addDays(date, 10); // => 2010/01/11 11:22:33  System.out.println(DateFormatUtils.format(tenDaysAfter, "yyyy/MM/dd HH:mm:ss"));    // 前一個月  Date prevMonth = DateUtils.addMonths(date, -1); // => 2009/12/01 11:22:33  System.out.println(DateFormatUtils.format(prevMonth, "yyyy/MM/dd HH:mm:ss"));    // 判斷是否是同一天  Date date1 = DateUtils.parseDate("2010/01/01 11:22:33", new String[]{"yyyy/MM/dd HH:mm:ss"});  Date date2 = DateUtils.parseDate("2010/01/01 22:33:44", new String[]{"yyyy/MM/dd HH:mm:ss"});  System.out.println(DateUtils.isSameDay(date1, date2));// true    // 日期格式化  System.out.println(DateFormatUtils.format(new Date(), "yyyy/MM/dd HH:mm:ss"));



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.