Spring Utils工具类常用方法实例
程序员文章站
2022-04-05 23:02:29
spring提供的工具类,主要用于框架内部使用,这个类提供了一些简单的方法,并且提供了易于使用的方法在分割字符串,如csv字符串,以及集合和数组。stringutils提供常用的方法如下:判断对象对象...
spring提供的工具类,主要用于框架内部使用,这个类提供了一些简单的方法,并且提供了易于使用的方法在分割字符串,如csv字符串,以及集合和数组。
stringutils提供常用的方法如下:
判断对象对象是否为null或者空字符串
public static boolean isempty(@nullable object str) { return (str == null || "".equals(str)); }
判断给的序列是否为空或者length为0
public static boolean haslength(@nullable charsequence str) { return (str != null && str.length() > 0); } public static boolean haslength(@nullable string str) { return (str != null && !str.isempty()); }
判断字符串是否以某个字符串开头
public static boolean startswithignorecase(@nullable string str, @nullable string prefix) { return (str != null && prefix != null && str.length() >= prefix.length() && str.regionmatches(true, 0, prefix, 0, prefix.length())); }
判断字符串是否以某个字符串结尾
public static boolean endswithignorecase(@nullable string str, @nullable string suffix) { return (str != null && suffix != null && str.length() >= suffix.length() && str.regionmatches(true, str.length() - suffix.length(), suffix, 0, suffix.length())); }
用另一个字符串替换字符串中出现的所有子字符串
public static string replace(string instring, string oldpattern, @nullable string newpattern) { if (!haslength(instring) || !haslength(oldpattern) || newpattern == null) { return instring; } //oldpattern字符串第一次出现的位置 int index = instring.indexof(oldpattern); if (index == -1) { // no occurrence -> can return input as-is return instring; } //字符串长度 int capacity = instring.length(); if (newpattern.length() > oldpattern.length()) { capacity += 16; } stringbuilder sb = new stringbuilder(capacity); int pos = 0; // our position in the old string int patlen = oldpattern.length(); while (index >= 0) { sb.append(instring, pos, index); sb.append(newpattern); pos = index + patlen; index = instring.indexof(oldpattern, pos); } // append any characters to the right of a match sb.append(instring, pos, instring.length()); return sb.tostring(); }
根据给定的路径规范化路径
public static string cleanpath(string path) { if (!haslength(path)) { return path; } //用新字符串替换旧字符串 string pathtouse = replace(path, windows_folder_separator, folder_separator); // shortcut if there is no work to do if (pathtouse.indexof('.') == -1) { return pathtouse; } // strip prefix from path to analyze, to not treat it as part of the // first path element. this is necessary to correctly parse paths like // "file:core/../core/io/resource.class", where the ".." should just // strip the first "core" directory while keeping the "file:" prefix. int prefixindex = pathtouse.indexof(':'); string prefix = ""; if (prefixindex != -1) { prefix = pathtouse.substring(0, prefixindex + 1); if (prefix.contains(folder_separator)) { prefix = ""; } else { pathtouse = pathtouse.substring(prefixindex + 1); } } if (pathtouse.startswith(folder_separator)) { prefix = prefix + folder_separator; pathtouse = pathtouse.substring(1); } string[] patharray = delimitedlisttostringarray(pathtouse, folder_separator); linkedlist<string> pathelements = new linkedlist<>(); int tops = 0; for (int i = patharray.length - 1; i >= 0; i--) { string element = patharray[i]; if (current_path.equals(element)) { // points to current directory - drop it. } else if (top_path.equals(element)) { // registering top path found. tops++; } else { if (tops > 0) { // merging path element with element corresponding to top path. tops--; } else { // normal path element found. pathelements.add(0, element); } } } // remaining top paths need to be retained. for (int i = 0; i < tops; i++) { pathelements.add(0, top_path); } // if nothing else left, at least explicitly point to current path. if (pathelements.size() == 1 && "".equals(pathelements.getlast()) && !prefix.endswith(folder_separator)) { pathelements.add(0, current_path); } return prefix + collectiontodelimitedstring(pathelements, folder_separator); }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。