Spring Utils工具类常用方法实例
作者:海之浪子 发布时间:2023-05-01 05:37:37
标签:Spring,Utils,工具,类
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


猜你喜欢
- JAVA用于开发图形界面应用的 SWING 组件包功能强大,使用方便。接下来我们就使用其写一个简单的图形界面小程序:加法计算器。第一步:首先
- mysql有个字段是bit,只存储1和0,是二进制存储,那么在java的dao层如何映射成boolean呢@Column(name=&quo
- 就是每隔一定的时间显示一张图片,全部图片文件位于:“工作空间\项目名称\bin\动态图\花好月圆\”文件夹下。文件名类似:1001.jpg,
- 前言目前主流的锁有两种,一种是synchronized,另一种就是ReentrantLock,JDK优化到现在目前为止synchronize
- Android 6.0版本对于程序员兄弟来说最不友好的就是权限的问题,动态权限的设置曾经让我很苦恼,目前大部分关于6.0权限设置的框架基本都
- Java生成4位、6位随机数短信验证码生成短信验证码问题,本质是产生某个范围内随机数的问题。比如,要生成6位短信验证码xxxxxx:6位数最
- 背景在使用Spring Boot Mvc的项目中,使用Long类型作为id的类型,但是当前端使用Number类型接收Long类型数据时,由于
- 本文实例讲述了C#使用WebClient登录网站并抓取登录后的网页信息实现方法。分享给大家供大家参考,具体如下:C#登录网站实际上就是模拟浏
- 使用datatables自带后台查询 前台代码:<!DOCTYPE html><html><head>&
- 本文实例讲述了Android实现学生管理系统,分享给大家供大家参考。具体如下:(1)管理系统实现的功能主要是:学生、教师的注册登录,和选课,
- Android应用经常会和服务器端交互,这就需要手机客户端发送网络请求,下面介绍四种常用网络请求方式,我这边是通过Android单元测试来完
- 本文实例为大家分享了java实现酒店管理系统的具体代码,供大家参考,具体内容如下编写环境:MyEclipse2014+sql server2
- 一,准备沙箱环境1,登录支付宝,进入 应用列表界面 https://openhome.alipay.com/dev/workspace2,如
- 有个小伙伴遇到了这样一个问题,就是AutoCompleteTextView实现自动填充的功能。同时要具备手机格式化的功能。下拉列表最后一行是
- //首先导入命名空间 using System.Runtime.InteropServices; /// <summary> /
- 错误内容:com.alibaba.druid.pool.GetConnectionTimeoutException: wait millis
- 这篇做了一个简单的时间轴控件。右侧的数据就是一个简单的字符串。问题还是有的,当右侧的文字长度不一样的时候就会有问题了。现在可以修改一下适配右
- 本文实例讲述了C#实现身份证号码验证的方法。分享给大家供大家参考。具体实现方法如下:随着现在互联网的发展,越来越多的注册用户的地方都用到了身
- Spring Boot如何实现分布式系统中的服务发现和注册?随着互联网的快速发展,越来越多的企业开始将自己的业务迁移到分布式系统中。在这种情
- 使用fileupload组件的原因: Request对象提供了一个getInputStream()方法,通过这个方法可以读取到客户端提交过来