Spring Utils工具类常用方法实例

作者:海之浪子 时间:2023-05-01 05:37:37 

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);
}

来源:https://www.cnblogs.com/haizhilangzi/p/12717354.html

标签:Spring,Utils,工具,类
0
投稿

猜你喜欢

  • 在IntelliJ IDEA中为自己设计的类库生成JavaDoc的方法示例

    2023-11-25 09:49:02
  • C#中的Socket编程详解

    2021-10-24 01:25:00
  • JAVA多线程之实现用户任务排队并预估排队时长

    2022-03-26 03:06:20
  • c#爬虫爬取京东的商品信息

    2022-12-03 14:38:11
  • C++智能指针实例详解

    2021-12-18 00:38:59
  • c++中数字与字符串之间的转换方法(推荐)

    2023-02-22 17:58:09
  • Spring自动装配之方法、构造器位置的自动注入操作

    2021-11-30 23:28:40
  • openFeign服务之间调用保持请求头信息处理方式

    2022-11-07 23:45:21
  • Java 方法的定义与调用详解

    2023-11-04 13:52:58
  • C# CultureInfo类案例详解

    2023-04-20 05:00:45
  • SpringBoot利用jackson格式化时间的三种方法

    2022-03-26 22:28:56
  • c# 实现雪花分形的示例

    2023-05-10 02:59:56
  • Android系统添加自定义鼠标样式通过按键切换实例详解

    2022-09-26 21:07:45
  • C#(.net)水印图片的生成完整实例

    2022-05-06 07:05:30
  • Spring框架学习之Cache抽象详解

    2023-07-20 17:37:47
  • Struts2中Action中是否需要实现Execute方法

    2021-10-30 06:57:23
  • Android实现图片添加阴影效果的2种方法

    2022-10-31 06:46:55
  • springboot ErrorPageFilter的实际应用详解

    2023-11-24 01:02:59
  • C#实现Socket通信的解决方法

    2022-06-01 22:06:00
  • Android开发实现跟随手指的小球效果示例

    2022-05-31 08:58:10
  • asp之家 软件编程 m.aspxhome.com