日期:2014-05-17 浏览次数:20785 次
//isEmpty 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 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" System.out.println(StringUtils.strip(" abcyx", "xyz")); // " abc" System.out.println(StringUtils.stripStart("yxabcxyz ", "xyz")); // "abcxyz " System.out.println(StringUtils.stripEnd(" xyzabcyx", "xyz")); // " xyzabc"
//默认半角空格分割 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, ","); Syste