Spring中的SpringUtils工具类

摘要:
Spring中SpringUtilsSpring提供的工具类主要在框架中使用。此类提供了一些简单的方法,并提供了易于使用的方法来拆分字符串,例如CSV字符串、集合和数组=null&&str.length()˃=prefix.length)&&str.regionMatches);}判断字符串是否以某个字符串publicstaticoolean endsWithIgnoreCase{return(str!=null&&str.length()˃=suffix.length)&&str.segmentMatches);}替换字符串中的所有子字符串,替换为另一个字符串publicstaticStringreplace{if(!HasLength | | newPattern==null){returninString;}//oldPattern字符串int index=inString的第一次出现。索引;如果{//nooccurrence-˃canreturninputas isreturninString;}//字符串长度intcapacity=inString。长度();如果{capacity+=16;}StringBuildersb=newStringBuilder;intpos=0;//我们在eoldstringintpatLen=oldPattern.length()中的位置;而{sb.append;sb.apped;pos=index+patLen;index=inString.indexOf;}//将任何字符附加到匹配项的右侧;returnsb.toString();}规范化路径publicstaticStringcleanPath{if(!

Spring中的SpringUtils

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

免责声明:文章转载自《Spring中的SpringUtils工具类》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇kafka服务器搭建与测试QQ加群组件-iPhone、Android、网页上加入QQ群下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

Jboss

同时,为了扩大JBoss的企业市场,JBoss已经签署了许多渠道合作伙伴。2004年6月,JBoss宣布JBoss应用服务器已通过Sun公司的J2EE认证。这是JBoss应用服务器历史上最重要的里程碑。JBossAOP 1.0于2004年10月发布。这也证实了JBoss是一家创新型公司。JBoss应用服务器5.0于2008年12月6日正式发布。新版本的应用服...

转:(WIN)S04-CH01 PCIE XDMA开发环境搭建以及环路测试

XDMAIP使用部分教程分LINUX篇和WINDOWS篇两个部分。通过实战,面向应用,提供给大家XILINXFPGAPCIE应用解决方案。本教程以MK7160FA作为样机测试。这是一款高性价比的FPGA开发板。而M_AXI_LITE挂载的BRAM是需要进行BAR空间操作,所以地址设置为0x80000000...

C# 没落了吗?

首先,这个数字--------------------------------------------C#是否正在衰落与微软的整个平台密切相关。近年来,使用C#的人越来越少,这也是因为越来越少的人专门为Microsoft平台开发产品。现在是移动时代,微软基本上错过了互联网和移动互联网这两波浪潮。现在生活不容易。在软件工程中,人们常说“唯一不变的就是改变本身”...

HTML中canvas的大小调整

今天用到canvas元素,发现它的大小不是像普通dom元素一样,直接设置css样式可以改变的,它会由自己原本的大小伸缩。例如,1.如果直接在canvas元素中添加style='300px;height:300px',这会导致伸缩,如图2.换种方式在css样式表中加入样式设置宽高产生的效果也是一样的。...

dBFs和dBm

dBFs和dBmdBFs是用来表征数字域功率值的大小,一般情况下我们定义0dBFs为数字域满刻度功率值,即数字域中功率的最大值;因此看到的dBFs的值都是负的。...

FTPClient

(iReplyCode==331||iReplyCode==230)){CloseSocketConnect();thrownewIOException(strReply.Substring(4));}if(iReplyCode!=230){SendCommand("PASS"+strRemotePass);if(!=null){SendCommand("Q...