赞
踩
import org.springframework.util.StringUtils;
StringUtils.hasLength(null); // false
StringUtils.hasLength(""); // false
StringUtils.hasLength(" "); // false
StringUtils.hasLength("Hello"); // true
StringUtils.hasText(null); // false
StringUtils.hasText(""); // false
StringUtils.hasText(" "); // false
StringUtils.hasText("12345"); // true
StringUtils.hasText(" 12345 "); // true
StringUtils.containsWhitespace(null); //false
StringUtils.containsWhitespace(""); // false
StringUtils.containsWhitespace("a"); // false
StringUtils.containsWhitespace("abc"); // false
StringUtils.containsWhitespace("abc"); // false
StringUtils.containsWhitespace(" "); // true
StringUtils.containsWhitespace(" a"); // true
StringUtils.containsWhitespace("abc "); // true
StringUtils.containsWhitespace("a b"); // true
StringUtils.containsWhitespace("a b"); // true
char char3 = '/';
StringUtils.matchesCharacter("/", char3); // true
StringUtils.matchesCharacter("hello", char3); // false
StringUtils.startsWithIgnoreCase("Aabz", "a"); // true
StringUtils.startsWithIgnoreCase("babz", "ba"); // true
StringUtils.startsWithIgnoreCase("babz", "a"); // false
StringUtils.endsWithIgnoreCase("AaBz", "bz"); // true
StringUtils.substringMatch("aabbccdd", 1, "abb"); // true
StringUtils.substringMatch("aabbccdd", 0, "aabb"); // true
StringUtils.trimWhitespace(null); // null
StringUtils.trimWhitespace(""); // ""
StringUtils.trimWhitespace(" "); // ""
StringUtils.trimWhitespace("\t"); // ""
StringUtils.trimWhitespace(" a"); // "a"
StringUtils.trimWhitespace("a "); // "a"
StringUtils.trimWhitespace(" a "); // "a"
StringUtils.trimWhitespace(" a b "); // "a b";
StringUtils.trimAllWhitespace(""); // ""
StringUtils.trimAllWhitespace(" "); // ""
StringUtils.trimAllWhitespace("\t"); // ""
StringUtils.trimAllWhitespace(" a"); // "a"
StringUtils.trimAllWhitespace("a "); // "a"
StringUtils.trimAllWhitespace(" a "); // "a"
StringUtils.trimAllWhitespace(" a b "); // "ab"
StringUtils.trimAllWhitespace(" a b c "); // "abc"
StringUtils.trimLeadingWhitespace(null); // null
StringUtils.trimLeadingWhitespace(""); // ""
StringUtils.trimLeadingWhitespace(" "); // ""
StringUtils.trimLeadingWhitespace("\t"); // ""
StringUtils.trimLeadingWhitespace(" a"); // "a"
StringUtils.trimLeadingWhitespace("a "); // "a "
StringUtils.trimLeadingWhitespace(" a "); // "a "
StringUtils.trimLeadingWhitespace(" a b "); // "a b "
StringUtils.trimLeadingWhitespace(" a b c "); // "a b c "
StringUtils.trimTrailingWhitespace(null); // null
StringUtils.trimTrailingWhitespace(" "); // ""
StringUtils.trimTrailingWhitespace("\t"); // ""
StringUtils.trimTrailingWhitespace("a "); // "a"
StringUtils.trimTrailingWhitespace(" a"); // " a"
StringUtils.trimTrailingWhitespace(" a "); // " a"
StringUtils.trimTrailingWhitespace(" a b "); // " a b"
StringUtils.trimTrailingWhitespace(" a b c "); // " a b c"
String testStr = "/1/h/e/l/lo";
StringUtils.trimLeadingCharacter(testStr, '/'); // "1/h/e/l/lo"
String testStr = "//h/e/l/lo";
StringUtils.trimLeadingCharacter(testStr, '/'); // "h/e/l/lo"
String testStr = "/1/h/e/l/lo";
StringUtils.trimTrailingCharacter(testStr, 'o'); // "/1/h/e/l/l"
String testStr = "/1/h/e/l/looo";
StringUtils.trimTrailingCharacter(testStr, 'o'); // "/1/h/e/l/l"
StringUtils.delete("ababaabab", "ab"); // "a"
// 原字符串中包含了子字符串中的a和b,因此删除之后只会剩下z
StringUtils.deleteAny("ababaababz", "bar"); // "z"
StringUtils.countOccurrencesOf("ababaabab", "ab"); // 4
// 参数1:原字符串 参数2:待替换的字符串 参数3:要替换成的字符串
StringUtils.replace("ababaabab", "ab", "cd"); // "cdcdacdcd"
StringUtils.quote("hello"); // "'hello'"
StringUtils.quoteIfString("hello"); // "'hello'"
StringUtils.capitalize("wolfcode"); // "Wolfcode"
StringUtils.uncapitalize("Java"); // "java"
String[] arrs=new String[]{"aa","bb","cc","dd"};
StringUtils.collectionToDelimitedString(Arrays.asList(arrs), ",", "{", "}");
// {aa},{bb},{cc},{dd}
String[] arrs=new String[]{"aa","bb","cc","dd"};
StringUtils.collectionToDelimitedString(Arrays.asList(arrs), ",");
// "aa,bb,cc,dd"
collectionToDelimitedString(coll, ",");
的简写String[] arrs=new String[]{"aa","bb","cc","dd"};
StringUtils.collectionToDelimitedString(Arrays.asList(arrs));
// "aa,bb,cc,dd"
.
分割的最后一个值,常用来获取类名或者文件后缀StringUtils.unqualify("cn.wolfcode.java"); // "java"
StringUtils.unqualify("cn/wolfcode/Hello.java"); // "java"
StringUtils.unqualify("cn/wolfcode/Hello.java", File.separatorChar); // "Hello.java"
// 如果原字符串中没有.的话,就返回原数据
StringUtils.unqualify("Hello", File.separatorChar); // "Hello"
StringUtils.getFilename("mypath/myfile.txt"); // "myfile.txt"
StringUtils.getFilenameExtension("mypath/myfile.txt"); // "txt"
StringUtils.stripFilenameExtension("mypath/myfile.txt"); // "mypath/myfile"
StringUtils.applyRelativePath("d:/java/wolfcode/Test.java", "other/Some.java");
// d:/java/wolfcode/other/Some.java
StringUtils.applyRelativePath("d:/java/wolfcode/Test.java", "Some.java");
// d:/java/wolfcode/Some.java
// 不支持重新定位绝对路径和上级目录等复杂一些的相对路径写法
StringUtils.applyRelativePath("d:/java/wolfcode/Test.java", "../other/Some.java");
// d:/java/wolfcode/../other/Some.java
StringUtils.pathEquals("d:\\java\\other\\Some.java", "d:/java/other/Some.java"); // true
List<String> strings = Arrays.asList("1", "2");
String[] result_toStringArray = StringUtils.toStringArray(strings);
// {"1", "2"}
StringUtils.addStringToArray(new String[] { "a", "b", "c" }, "d");
// { "a", "b", "c", "d"}
String[] arry1 = { "a", "b", "c" };
String[] arry2 ={ "a", "b", "c", "d" };
// 数组不能直接打印,需要通过Arrays.toString()进行转换
System.out.println(Arrays.toString(result_concatenateStringArrays));
// ["a", "b", "c", "a", "b", "c", "d"]
StringUtils.sortStringArray(new String[]{"d","c","b","a"}); // ["a", "b", "c", "d"]
StringUtils.trimArrayElements(new String[]{"d "," c"}); // ["d", "c"]
StringUtils.removeDuplicateStrings(new String[]{"a", "a", "b" ,"a", "c"});
// ["a", "b", "c"]
StringUtils.split("www.wolfcode.cn", ".");
// ["www", "wolfcode.cn"]
StringUtils.delimitedListToStringArray("aa,ba,ca,da", "a,");
// ["a", "b", "c", "da"]
StringUtil.delimitedListToStringArray("h,e,l,l,o", ",");
// ["h", "e", "l", "l", "o"]
,
分隔字符串,是StringUtil.delimitedListToStringArray(XXX, ",");
的简单写法StringUtils.commaDelimitedListToStringArray("h,e,l,l,o"); // ["h", "e", "l", "l", "o"]
import org.springframework.util.CollectionUtils;
CollectionUtils.isEmpty(new HashMap<>()); // true
CollectionUtils.isEmpty(new ArrayList<>()); // true
将数组转换为List
==注意:==该方法内部用Arrays.asList(ObjectUtils.toObjectArray(source));
进行的转换,
如果向转换之后的List进行元素的增删操作,会报错
String[] arr = {"1", "2"};
List objects = CollectionUtils.arrayToList(arr); // ["1", "2"]
String[] arr = {"1", "2"};
List<String> list = new ArrayList<String>() {{
add("A");
add("B");
add("C");
}};
CollectionUtils.mergeArrayIntoCollection(arr, list); // ["A", "B", "C", "1", "2"]
Student student = new Student();
ArrayList<Object> list = new ArrayList<Object>() {{
add(new Student());
add(new Student());
add(student);
}};
// 第二个参数student和第一行被new出来的是一个对象,因此是包含的结果
boolean result = CollectionUtils.containsInstance(list, student);
System.out.println(result); // true
ArrayList<Object> list1 = new ArrayList<Object>() {{
add("1");
add("2");
add("3");
}};
ArrayList<Object> list2 = new ArrayList<Object>() {{
add("3");
add("4");
}};
boolean result = CollectionUtils.containsAny(list1, list2);
System.out.println(result); // true
ArrayList<Object> list1 = new ArrayList<Object>() {{ add("1"); add("2"); add("3"); }}; ArrayList<Object> list2 = new ArrayList<Object>() {{ add("1"); add("2"); add("2"); add("2"); add("3"); }}; Object firstMatch = CollectionUtils.findFirstMatch(list1, list2); System.out.println(firstMatch); // "1"
ArrayList<Object> list = new ArrayList<Object>() {{
add("1");
// 如果把下面的注释解开,结果就变为了false
// add("2");
}};
boolean result = CollectionUtils.hasUniqueObject(list2);
System.out.println(result); // true
ArrayList<Object> list2 = new ArrayList<Object>() {{
// 如果把下面的注释解开,集合中就会有多种类型的元素,就会返回null
// add("1");
// add("2");
add(123);
add(22);
add(234);
}};
Class<?> commonElementType = CollectionUtils.findCommonElementType(list2);
System.out.println(commonElementType); // class java.lang.Integer
ArrayList<Object> list2 = new ArrayList<Object>() {{
add(123);
add(22);
add(234);
}};
Object o = CollectionUtils.firstElement(list2);
System.out.println(o); // 123
import org.springframework.util.NumberUtils;
Integer integer = NumberUtils.convertNumberToTargetClass(1.256, Integer.class);
System.out.println(integer); // 1
Integer integer = NumberUtils.parseNumber("1", Integer.class);
System.out.println(integer); // 1
BigDecimal bigDecimal = NumberUtils.parseNumber("958412.2415", BigDecimal.class);
System.out.println(bigDecimal); // 958412.2415
import org.springframework.util.ObjectUtils;
Student student = null;
Map map = null;
System.out.println(ObjectUtils.isEmpty(student)); // true
System.out.println(ObjectUtils.isEmpty(map)); // true
Map map = null;
System.out.println(ObjectUtils.isArray(map)); // false
String[] arr = {"1", "2"};
System.out.println(ObjectUtils.isArray(arr)); // true
String[] arr = {"1", "2"};
System.out.println(ObjectUtils.containsElement(arr, "1")); // true
System.out.println(ObjectUtils.containsElement(arr, "3")); // false
String[] arr = {"1", "2"};
// 返回一个新数组
String[] strings = ObjectUtils.addObjectToArray(arr, "111");
System.out.println(Arrays.toString(strings)); // [1, 2, 111]
import org.springframework.util.FileCopyUtils;
FileCopyUtils.copy(
new File("E:\\copyTest\\from\\a.txt"),
new File("E:\\copyTest\\to\\b.txt")
);
File in = new File("E:\\copyTest\\from\\a.txt")
InputStream fileInputStream = new FileInputStream(in);
File out = new File("E:\\copyTest\\to\\b.txt")
OutputStream fileOutputStream = new FileOutputStream(out);
FileCopyUtils.copy(fileInputStream, fileOutputStream);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。