org.apache.commons.lang3 使用案例

package com.simple.test; html

 

import java.util.Date; java

import java.util.Iterator; apache

import java.util.Map; api

 

import org.apache.commons.lang3.ArrayUtils; 數組

import org.apache.commons.lang3.ClassUtils; dom

import org.apache.commons.lang3.RandomStringUtils; spa

import org.apache.commons.lang3.StringEscapeUtils; orm

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

import org.apache.commons.lang3.SystemUtils; 對象

import org.apache.commons.lang3.math.NumberUtils; 

import org.apache.commons.lang3.time.DateFormatUtils; 

import org.apache.commons.lang3.time.DateUtils; 

import org.junit.Test; 

 

public class TestString { 

 

    public static void main(String[] args) { 

        String[] test = { "33", "ddffd" }; 

        String[] test2 = { "33", "ddffd" }; 

        String[] test1 = { "ddffd", "33" }; 

 

        // 1.判斷兩個數據是否相等, 若是內容相同, 順序相同 則返回 真 

        System.out.println("判斷兩個數組是否相同: " + ArrayUtils.isEquals(test, test2)); 

        System.out.println("判斷數組中是否包含一個對象: " + ArrayUtils.contains(test, "33")); 

         

        // 2.{33,ddffd} 將數組內容以{,}形式輸出. 

        System.out.println("輸出數組中的數據: "+ArrayUtils.toString(test)); 

         

        System.out.println("講一個二維數組轉換成MAP...."); 

        Map map = ArrayUtils.toMap(new String[][] { { "RED", "#FF0000" }, { "GREEN", "#00FF00" }, { "BLUE", "#0000FF" } }); 

        // 3.toMap 一個數組,但每一個元素 Each element of the array 

        // must be either a {@link java.util.Map.Entry} or an Array, 

        // 方式一 下面是遍歷map的方式,取得其keySet.iterator(); 

        Iterator it = map.keySet().iterator(); 

        while (it.hasNext()) { 

            String key = (String) it.next(); 

            // it.next()只包含key 

            System.out.println("key:" + key + "value:" + map.get(key)); 

        } 

        System.out.println("講一個二維數組轉換成MAP 打印結束...."); 

        // 方式二,取得其entrySet()集合, 

        Iterator it1 = map.entrySet().iterator(); 

        while (it.hasNext()) { 

            Map.Entry entry = (Map.Entry) it1.next(); 

            // it1.next()中包含key和value 

            System.out.println("key :" + entry.getKey() + "value :" + entry.getValue()); 

        } 

 

        // 4.取得類名 

        System.out.println("取得一個類的名稱: "+ ClassUtils.getShortClassName(Test.class)); 

        // 取得其包名 

        System.out.println("取得一個類的包名: "+ ClassUtils.getPackageName(Test.class)); 

        // 5.NumberUtils 

        System.out.println("將一個字符串轉換成數字: "+ NumberUtils.toInt("6")); 

        System.out.println("將一個字符串轉換成數字, 輸入一個默認參數: "+ NumberUtils.toInt("7", 10));// 返回7 若是第一個參數爲 null 返回10  

         

        // 6.五位的隨機字母和數字 

        System.out.println("取得隨機字母和數字: "+RandomStringUtils.randomAlphanumeric(15)); 

        // 7.StringEscapeUtils 

        System.out.println(StringEscapeUtils.unescapeHtml3("</html>")); 

        // 輸出結果爲&lt;html&gt; 

        System.out.println(StringEscapeUtils.escapeJava("String")); 

        // 8.StringUtils,判斷是不是空格字符 

        System.out.println("判斷一個字符串是不是 空格: "+StringUtils.isBlank("   ")); 

        // 將數組中的內容以,分隔 

        System.out.println("將數組中的內容以,分隔: "+ StringUtils.join(test, ",")); 

        // 在右邊加下字符,使之總長度爲6 

        System.out.println("在右邊加下字符,使之總長度爲6: "+ StringUtils.rightPad("abc", 6, 'T')); 

        // 首字母大寫 

        System.out.println("首字母大寫: "+ StringUtils.capitalize("abc")); 

        // Deletes all whitespaces from a String 刪除全部空格 

        System.out.println("刪除全部空格 : "+ StringUtils.deleteWhitespace("   ab  c  ")); 

        // 判斷是否包含這個字符 

        System.out.println("判斷是否包含這個字符 : "+ StringUtils.contains("abc", "ab")); 

        // 表示左邊兩個字符 

        System.out.println("取得一個字符串左邊的兩個字符: "+ StringUtils.left("abc", 2)); 

        System.out.println("取得一個字符串右邊的三個字符 : "+ StringUtils.right("abcd", 3)); 

         

         

        System.out.println("把一個字符串轉換爲BigDecimal對象: " + NumberUtils.createBigDecimal("0.25")); 

        System.out.println("找出最大值: " + NumberUtils.max(new int[]{1,2,3})); 

        System.out.println("JavaHome: " + SystemUtils.getJavaHome()); 

        System.out.println("臨時目錄位置: " + SystemUtils.getJavaIoTmpDir()); 

         

         

        System.out.println("日期格式處理: " + DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss")); 

        System.out.println("日期加 7天: " + DateFormatUtils.format(DateUtils.addDays(new Date(), 7), "yyyy-MM-dd HH:mm:ss")); 

         

    } 

相關文章
相關標籤/搜索