Spring强大的StringUtils工具类你会用吗
[ Spring学习 ] Spring 的 StringUtils 工具类这么强大 你会用那些方法 ?
:point_left: <stron> 🙏</stron>
💋如果对你有帮助,给博主一个免费的点赞💋 👋博客主页🎉 秋日的晚霞 ⭐️更多文章 请关注主页📝 👋一起走上java学习之路! 🎄欢迎各位🔎点赞👍评论收藏⭐️🎄
[TOC]
🍃获取文件扩展名 getFilenameExtension
从给定的 Java 资源路径中提取文件扩展名,例如“mypathmyfile.txt”->“txt”。
//获取后缀文件名 String path = "D:\\API开发文档\\JAVA_API_1.8_CHS - 快捷方式"; String filenameExtension = StringUtils.getFilenameExtension(path); System.out.println("filenameExtension = " + filenameExtension);
输出结果
filenameExtension = 8_CHS - 快捷方式
getFilenameExtension 的 源码
public static String getFilenameExtension(@Nullable String path) { if (path == null) { return null; } //EXTENSION_SEPARATOR = . int extIndex = path.lastIndexOf(EXTENSION_SEPARATOR); if (extIndex == -1) { return null; } //FOLDER_SEPARATOR = / int folderIndex = path.lastIndexOf(FOLDER_SEPARATOR); if (folderIndex > extIndex) { return null; } return path.substring(extIndex + 1); }
🍃获取文件扩展名 getFilenameExtension
从给定的 Java 资源路径中提取文件扩展名,例如“mypathmyfile.txt”->“txt”。
//获取后缀文件名 String path = "D:\\API开发文档\\JAVA_API_1.8_CHS - 快捷方式"; String filenameExtension = StringUtils.getFilenameExtension(path); System.out.println("filenameExtension = " + filenameExtension);
输出结果
filenameExtension = 8_CHS - 快捷方式
getFilenameExtension 的 源码
public static String getFilenameExtension(@Nullable String path) { if (path == null) { return null; } //EXTENSION_SEPARATOR = . int extIndex = path.lastIndexOf(EXTENSION_SEPARATOR); if (extIndex == -1) { return null; } //FOLDER_SEPARATOR = / int folderIndex = path.lastIndexOf(FOLDER_SEPARATOR); if (folderIndex > extIndex) { return null; } return path.substring(extIndex + 1); }
🍃去除文件扩展名 stripFilenameExtension
public static void main(String[] args) { String path = "D:\\API开发文档\\JAVA_API_1.8_CHS - 快捷方式"; String stripFilenameExtension = StringUtils.stripFilenameExtension(path); System.out.println("stripFilenameExtension = " + stripFilenameExtension); }
输出结果
filenameExtension = 8_CHS - 快捷方式
stripFilenameExtension 的 源码
public static String stripFilenameExtension(String path) { int extIndex = path.lastIndexOf(EXTENSION_SEPARATOR); if (extIndex == -1) { return path; }
🍃替换所有的旧指定字符串 replace
用另一个字符串替换字符串中所有出现的子字符串
public static void main(String[] args) { String str = "abcdefgabc"; String replace = StringUtils.replace(str,"abc","ABC"); System.out.println("replace = " + replace); }
输出结果
replace = ABCdefgABC
replace 源码
public static String replace(String inString, String oldPattern, @Nullable String newPattern) { if (!hasLength(inString) || !hasLength(oldPattern) || newPattern == null) { return inString; } 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(); }
🍃删除所有的匹配字符串 delete
public static void main(String[] args) { String str = "abcdefgabc"; String delete = StringUtils.delete(str,"abc"); System.out.println("delete = " + delete); }
输出结果
delete = defg
delete 源码
public static String delete(String inString, String pattern) { return replace(inString, pattern, ""); } public static String replace(String inString, String oldPattern, @Nullable String newPattern) { if (!hasLength(inString) || !hasLength(oldPattern) || newPattern == null) { return inString; } 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(); }
🍃判断对象是否为空 isEmpty
检查给定对象(可能是字符串)是否为空。这实际上是 !hasLength(String) 的快捷方式。
String s = ""; boolean flag = StringUtils.isEmpty(s ); System.out.println("flag = " + flag);
输出结果
flag = true
isEmpty源码
public static boolean isEmpty(@Nullable Object str) { return (str == null || "".equals(str)); }
🍃判断字符串是否有长度 hasLength
检查给定的 String 既不是 null 也不是长度为 0。 注意 hasLength 方法只能传入String类型的对象
String s = ""; boolean flag = StringUtils.hasLength(s); System.out.println("flag = " + flag);
输出结果
flag = false
hasLength源码
public static boolean hasLength(@Nullable String str) { return (str != null && !str.isEmpty()); }
🍃判断字符串是否有内容 hasText
检查给定的字符串是否包含实际文本。更具体地说,如果 String 不为 null,其长度大于 0,并且至少包含一个非空白字符,则此方法返回 true。
public static void main(String[] args) { String s = "adsda qewq"; boolean flag = StringUtils.hasText(s); System.out.println("flag = " + flag); }
输出结果
flag = true
hasText 源码
public static boolean hasText(@Nullable String str) { return (str != null && !str.isEmpty() && containsText(str)); }
🍃检查字符串是否有空白字符 containsWhitespace
检查给定的字符串是否包含任何空白字符。
public static void main(String[] args) { String s = "adsda qewq"; boolean flag = StringUtils.containsWhitespace(s); System.out.println("flag = " + flag); }
输出结果
flag = true
containsWhitespace 源码
public static boolean containsWhitespace(@Nullable String str) { return containsWhitespace((CharSequence) str); } public static boolean containsWhitespace(@Nullable CharSequence str) { if (!hasLength(str)) { return false; } int strLen = str.length(); for (int i = 0; i < strLen; i++) { if (Character.isWhitespace(str.charAt(i))) { return true; } } return false; }
🍃去除字符串前后空白 trimWhitespace
*注意: 只会去除前后 不会去除中间空白 *
public static void main(String[] args) { String s = " abcd efg "; String trimWhitespace = StringUtils.trimWhitespace(s); System.out.println("trimWhitespace = " + trimWhitespace); }
输出结果
trimWhitespace = abcd efg
trimWhitespace 源码
public static String trimWhitespace(String str) { if (!hasLength(str)) { return str; } int beginIndex = 0; int endIndex = str.length() - 1; while (beginIndex <= endIndex && Character.isWhitespace(str.charAt(beginIndex))) { beginIndex++; } while (endIndex > beginIndex && Character.isWhitespace(str.charAt(endIndex))) { endIndex--; } return str.substring(beginIndex, endIndex + 1); }
🍃去除字符串所有空白 trimAllWhitespace
修剪给定字符串中的所有空格:前导、尾随和字符之间。
public static void main(String[] args) { String s = " abcd efg "; String trimAllWhitespace = StringUtils.trimAllWhitespace(s); System.out.println("trimAllWhitespace = " + trimAllWhitespace); }
输出结果
trimAllWhitespace = abcdefg
trimAllWhitespace 源码
public static String trimAllWhitespace(String str) { if (!hasLength(str)) { return str; } int len = str.length(); StringBuilder sb = new StringBuilder(str.length()); for (int i = 0; i < len; i++) { char c = str.charAt(i); if (!Character.isWhitespace(c)) { sb.append(c); } } return sb.toString(); }
🍃去除字符串左边空格 trimLeadingWhitespace
从给定的字符串中修剪前导空格。
public static void main(String[] args) { String s = " abcd efg "; String trimLeadingWhitespace = StringUtils.trimLeadingWhitespace(s); System.out.println("trimLeadingWhitespace = " + trimLeadingWhitespace); }
输出结果
trimLeadingWhitespace = abcd efg //注意空格到此
trimLeadingWhitespace 源码
public static String trimLeadingWhitespace(String str) { if (!hasLength(str)) { return str; } StringBuilder sb = new StringBuilder(str); while (sb.length() > 0 && Character.isWhitespace(sb.charAt(0))) { sb.deleteCharAt(0); } return sb.toString(); }
🍃去除字符串右边空格 trimTrailingWhitespace
检查给定的字符串是否包含实际文本。更具体地说,如果 String 不为 null,其长度大于 0,并且至少包含一个非空白字符,则此方法返回 true。
public static void main(String[] args) { String s = " abcd efg "; String trimTrailingWhitespace = StringUtils.trimTrailingWhitespace(s); System.out.println("trimTrailingWhitespace = " + trimTrailingWhitespace); }
输出结果
trimTrailingWhitespace = abcd efg
trimTrailingWhitespace 源码
public static String trimTrailingWhitespace(String str) { if (!hasLength(str)) { return str; } StringBuilder sb = new StringBuilder(str); while (sb.length() > 0 && Character.isWhitespace(sb.charAt(sb.length() - 1))) { sb.deleteCharAt(sb.length() - 1); } return sb.toString(); }
🍃删除字符串左边第一个给定字符 trimLeadingCharacter
public static void main(String[] args) { String s = "abc abcd efg "; String trimLeadingCharacter = StringUtils.trimLeadingCharacter(s,'a'); System.out.println("trimTrailingWhitespace = " + trimLeadingCharacter); }
输出结果
trimTrailingWhitespace = bc abcd efg
trimLeadingCharacter 源码
public static String trimLeadingCharacter(String str, char leadingCharacter) { if (!hasLength(str)) { return str; } StringBuilder sb = new StringBuilder(str); while (sb.length() > 0 && sb.charAt(0) == leadingCharacter) { sb.deleteCharAt(0); } return sb.toString(); }
🍃删除字符串右边第一个给定字符 trimTrailingCharacter
检查给定的字符串是否包含实际文本。更具体地说,如果 String 不为 null,其长度大于 0,并且至少包含一个非空白字符,则此方法返回 true。
public static void main(String[] args) { String s = "abc abcd efg a"; String trimTrailingCharacter = StringUtils.trimTrailingCharacter(s,'a'); System.out.println("trimTrailingWhitespace = " + trimTrailingCharacter); }
输出结果
trimTrailingWhitespace = abc abcd efg //空格到此
trimTrailingCharacter 源码
public static String trimTrailingCharacter(String str, char trailingCharacter) { if (!hasLength(str)) { return str; } StringBuilder sb = new StringBuilder(str); while (sb.length() > 0 && sb.charAt(sb.length() - 1) == trailingCharacter) { sb.deleteCharAt(sb.length() - 1); } return sb.toString(); }
🍃判断字符串是否以给定字符串开头(忽略大小写) startsWithIgnoreCase
public static void main(String[] args) { String s = "Abc abcd efg aBC"; boolean startsWithIgnoreCase = StringUtils.startsWithIgnoreCase(s, "abc"); System.out.println("startsWithIgnoreCase = " + startsWithIgnoreCase); }
输出结果
startsWithIgnoreCase = true
startsWithIgnoreCase 源码
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())); }
🍃判断字符串是否以给定字符串结尾 (忽略大小写) endsWithIgnoreCase
public static void main(String[] args) { String s = "Abc abcd efg aBC"; boolean endsWithIgnoreCase = StringUtils.endsWithIgnoreCase(s, "abc"); System.out.println("startsWithIgnoreCase = " + endsWithIgnoreCase); }
输出结果
startsWithIgnoreCase = true
startsWithIgnoreCase 源码
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())); }#Java求职##Spring##学习路径#