版本:commons-lang3-3.1.jar html
org.apache.commons.lang中的StringUtils類操做對象是java.lang.String類型的對象,是JDK提供的String類型操做方法的補充,而且是null安全的(即若是輸入參數String爲null則不會拋出NullPointerException,而是作了相應處理,例如,若是輸入爲null則返回也是null等.java
這個工具包能夠當作是對java.lang的擴展。提供了諸如StringUtils, StringEscapeUtils, RandomStringUtils, Tokenizer, WordUtils等工具類。apache
Class | Description |
---|---|
ArrayUtils | Operations on arrays, primitive arrays (like |
CharEncoding | Character encoding names required of every implementation of the Java platform.api |
ClassPathUtils | Operations regarding the classpath.數組 |
ClassUtils | Operates on classes without using reflection.安全 |
RandomStringUtils | Operations for random |
RandomUtils | Utility library that supplements the standard |
StringUtils | Operations on |
SystemUtils | Helpers for |
ThreadUtils | Helpers for |
Validate | This class assists in validating arguments. |
其中對幾個現階段用的比較多的包中類的經常使用方法作介紹,不按期更新
//isEmpty //Checks if a CharSequence is empty ("") or null. System.out.println(StringUtils.isEmpty(null)); // true System.out.println(StringUtils.isEmpty("")); // true System.out.println(StringUtils.isEmpty(" ")); // false System.out.println(StringUtils.isEmpty("bob")); // false System.out.println(StringUtils.isEmpty(" bob ")); // false //isBlank //Checks if a CharSequence is whitespace, empty ("") or null. System.out.println(StringUtils.isBlank(null)); // true System.out.println(StringUtils.isBlank("")); // true System.out.println(StringUtils.isBlank(" ")); // true System.out.println(StringUtils.isBlank("bob")); // false System.out.println(StringUtils.isBlank(" bob ")); // false
//trim 去掉首尾的空格 System.out.println(StringUtils.trim(null)); // null System.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" //strip System.out.println(StringUtils.strip(null)); // null System.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"
//默認半角空格分割 String str1 = "aaa bbb ccc"; String[] dim1 = StringUtils.split(str1); // => ["aaa", "bbb", "ccc"] System.out.println(dim1.length);//3 System.out.println(dim1[0]);//"aaa" System.out.println(dim1[1]);//"bbb" System.out.println(dim1[2]);//"ccc" //指定分隔符 String str2 = "aaa,bbb,ccc"; String[] dim2 = StringUtils.split(str2, ","); // => ["aaa", "bbb", "ccc"] System.out.println(dim2.length);//3 System.out.println(dim2[0]);//"aaa" System.out.println(dim2[1]);//"bbb" System.out.println(dim2[2]);//"ccc" //去除空字符串 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" //包含空字符串 String str4 = "aaa,,bbb"; String[] dim4 = StringUtils.splitPreserveAllTokens(str4, ","); // => ["aaa", "", "bbb"] System.out.println(dim4.length);//3 System.out.println(dim4[0]);//"aaa" System.out.println(dim4[1]);//"" System.out.println(dim4[2]);//"bbb" //指定分割的最大次數(超事後不分割) String str5 = "aaa,bbb,ccc"; String[] dim5 = StringUtils.split(str5, ",", 2); // => ["aaa", "bbb,ccc"] System.out.println(dim5.length);//2 System.out.println(dim5[0]);//"aaa" System.out.println(dim5[1]);//"bbb,ccc"
//數組元素拼接 String[] array = {"aaa", "bbb", "ccc"}; String result1 = StringUtils.join(array, ","); System.out.println(result1);//"aaa,bbb,ccc" //集合元素拼接 List<String> list = new ArrayList<String>(); list.add("aaa"); list.add("bbb"); list.add("ccc"); String result2 = StringUtils.join(list, ","); System.out.println(result2);//"aaa,bbb,ccc"
對於StingUtils的字符串處理類經常使用方法就這些,還有些方法根據具體代碼需求查閱Api文檔
對於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 // 截取部分元素 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 // 判斷是否包含某元素 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 System.out.println(ArrayUtils.isEmpty(array62));//true System.out.println(ArrayUtils.isNotEmpty(array63));//true // 判斷數組長度是否相等 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
經常使用屬性整理以下:
Methods
Modifier and Type | Method and Description |
---|---|
static Object | cloneBean(Object bean) Clone a bean based on the available property getters and setters, even if the bean class itself does not implement Cloneable. |
static void | copyProperties(Object dest, Object orig) Copy property values from the origin bean to the destination bean for all cases where the property names are the same. |
static void | copyProperty(Object bean, String name, Object value) Copy the specified property value to the specified destination bean, performing any type conversion that is required. 將指定的屬性值複製到指定的目標bean,執行所需的任何類型轉換。 |
static Map<String,String> | describe(Object bean) Return the entire set of properties for which the specified bean provides a read method. |
static String[] | getArrayProperty(Object bean, String name) Return the value of the specified array property of the specified bean, as a String array. 將指定的bean的指定數組屬性的值做爲String數組返回。 |
static String | getIndexedProperty(Object bean, String name) Return the value of the specified indexed property of the specified bean, as a String. |
static String | getIndexedProperty(Object bean, String name, int index) Return the value of the specified indexed property of the specified bean, as a String. 將指定的bean的指定索引屬性的值做爲String返回。 |
static String | getMappedProperty(Object bean, String name) Return the value of the specified indexed property of the specified bean, as a String. |
static String | getMappedProperty(Object bean, String name, String key) Return the value of the specified mapped property of the specified bean, as a String. |
static String | getNestedProperty(Object bean, String name) Return the value of the (possibly nested) property of the specified name, for the specified bean, as a String. |
static String | getProperty(Object bean, String name) Return the value of the specified property of the specified bean, no matter which property reference format is used, as a String. |
static String | getSimpleProperty(Object bean, String name) Return the value of the specified simple property of the specified bean, converted to a String. |
static void | populate(Object bean, Map<String,? extends Object> properties) Populate the JavaBeans properties of the specified bean, based on the specified name/value pairs. |
static void | setProperty(Object bean, String name, Object value) Set the specified property value, performing type conversions as required to conform to the type of the destination propert 設置指定的屬性值,根據須要執行類型轉換以符合目標屬性的類型 |
複製Bean //public static Object cloneBean(Object bean)
Book bk=new Book(); bk.setTitle("java編程"); bk.setContent("java編程語言"); //public static Object cloneBean(Object bean) Book copyBean=(Book)BeanUtils.cloneBean(bk); System.out.println(copyBean.getTitle()); System.out.println(copyBean.getContent());
賦值Bean //public static void copyProperties(Object dest,Object orig)
JavaBook javabook=new JavaBook(); javabook.setTitle("JAVA類書籍"); javabook.setContent("java併發編程"); javabook.setZhuozhe("不祥"); Book bk=new Book(); //copyProperties(Object dest, Object orig) BeanUtils.copyProperties(bk, javabook); System.out.println(bk.getTitle()); System.out.println(bk.getContent()); // copyProperty(Object bean, String name, Object value) BeanUtils.copyProperty(javabook, "title", "測試書籍"); //public static String getProperty(Object bean, String name) System.out.println(BeanUtils.getProperty(bk, "title"));
Bean的populate // populate(Object bean, Map<String,? extends Object> properties)
Book bk=new Book(); Map<String, String> map5 = new HashMap<String, String>(); map5.put("title", "rensanning"); map5.put("content", "31"); // public static void populate(Object bean, Map<String,? extends Object> properties) BeanUtils.populate(bk, map5); System.out.println(BeanUtils.getProperty(bk, "title")); System.out.println(BeanUtils.getProperty(bk, "content"));
其餘方法根據實際應用參照API
-------------------------------分割線---------------------
2017年4月7日18:23:15