Spring Utils工具類常用方法實(shí)例
Spring提供的工具類,主要用于框架內(nèi)部使用,這個(gè)類提供了一些簡(jiǎn)單的方法,并且提供了易于使用的方法在分割字符串,如CSV字符串,以及集合和數(shù)組。
StringUtils提供常用的方法如下:
判斷對(duì)象對(duì)象是否為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());
}
判斷字符串是否以某個(gè)字符串開(kāi)頭
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()));
}
判斷字符串是否以某個(gè)字符串結(jié)尾
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()));
}
用另一個(gè)字符串替換字符串中出現(xiàn)的所有子字符串
public static String replace(String inString, String oldPattern, @Nullable String newPattern) {
if (!hasLength(inString) || !hasLength(oldPattern) || newPattern == null) {
return inString;
}
//oldPattern字符串第一次出現(xiàn)的位置
int index = inString.indexOf(oldPattern);
if (index == -1) {
// no occurrence -> can return input as-is
return inString;
}
//字符串長(zhǎng)度
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();
}
根據(jù)給定的路徑規(guī)范化路徑
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);
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 詳解Spring的StringUtils踩坑記錄
- JSP 開(kāi)發(fā)之Spring BeanUtils組件使用
- Spring框架JavaMailSender發(fā)送郵件工具類詳解
- 兼容Spring Boot 1.x和2.x配置類參數(shù)綁定的工具類SpringBootBindUtil
- Spring Boot中優(yōu)雅的獲取yml文件工具類
- spring boot結(jié)合Redis實(shí)現(xiàn)工具類的方法示例
- Spring boot工具類靜態(tài)屬性注入及多環(huán)境配置詳解
- Spring獲取ApplicationContext對(duì)象工具類的實(shí)現(xiàn)方法
相關(guān)文章
java Split 實(shí)現(xiàn)去除一個(gè)空格和多個(gè)空格
這篇文章主要介紹了java Split 實(shí)現(xiàn)去除一個(gè)空格和多個(gè)空格,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10
關(guān)于springcloud報(bào)錯(cuò)報(bào)UnsatisfiedDependencyException的問(wèn)題
這篇文章主要介紹了關(guān)于springcloud報(bào)錯(cuò)報(bào)UnsatisfiedDependencyException的問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
Spring MVC使用jstl 標(biāo)簽c:forEach 遍歷輸出雙層嵌套List的數(shù)據(jù)方式
這篇文章主要介紹了Spring MVC使用jstl 標(biāo)簽c:forEach 遍歷輸出雙層嵌套List的數(shù)據(jù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
JavaWeb中HttpSession中表單的重復(fù)提交示例
這篇文章主要介紹了JavaWeb中HttpSession中表單的重復(fù)提交,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03
JAVA實(shí)現(xiàn)基于Tcp協(xié)議的簡(jiǎn)單Socket通信實(shí)例
本篇文章主要介紹了JAVA實(shí)現(xiàn)基于Tcp協(xié)議的簡(jiǎn)單Socket通信實(shí)例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-01-01
解決MyEclipse6.5無(wú)法啟動(dòng),一直停留剛開(kāi)始啟動(dòng)界面的詳解
本篇文章是對(duì)解決MyEclipse6.5無(wú)法啟動(dòng),一直停留剛開(kāi)始啟動(dòng)界面的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05

