簡介
在我們的代碼中經(jīng)常需要對(duì)字符串判空,截取字符串、轉(zhuǎn)換大小寫、分隔字符串、比較字符串、去掉多余空格、拼接字符串、使用正則表達(dá)式等等。如果只用 String 類提供的那些方法,我們需要手寫大量的額外代碼,不然容易出現(xiàn)各種異?!,F(xiàn)在有個(gè)好消息是:org.apache.commons.lang3包下的StringUtils工具類,給我們提供了非常豐富的選擇。
Maven依賴:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
方法列表和描述
- IsEmpty/IsBlank - 檢查字符串是否包含文本
- Trim/Strip - 移除字符串的前導(dǎo)和尾隨空白
- Equals/Compare - 以空安全的方式比較兩個(gè)字符串
- startsWith - 以空安全的方式檢查字符串是否以指定前綴開頭
- endsWith - 以空安全的方式檢查字符串是否以指定后綴結(jié)尾
- IndexOf/LastIndexOf/Contains - 空安全的索引檢查
- IndexOfAny/LastIndexOfAny/IndexOfAnyBut/LastIndexOfAnyBut - 在一組字符串中查找任意字符串的索引
- ContainsOnly/ContainsNone/ContainsAny - 檢查字符串是否只包含/不包含/包含任意一組字符
- Substring/Left/Right/Mid - 空安全的子字符串提取
- SubstringBefore/SubstringAfter/SubstringBetween - 相對(duì)于其他字符串的子字符串提取
- Split/Join - 將字符串拆分為子字符串?dāng)?shù)組,反之亦然
- Remove/Delete - 移除字符串的部分內(nèi)容
- Replace/Overlay - 在字符串中搜索并用另一個(gè)字符串替換
- Chomp/Chop - 移除字符串的最后一部分
- AppendIfMissing - 如果不存在,將后綴追加到字符串的末尾
- PrependIfMissing - 如果不存在,將前綴添加到字符串的開頭
- LeftPad/RightPad/Center/Repeat - 填充字符串
- UpperCase/LowerCase/SwapCase/Capitalize/Uncapitalize - 更改字符串的大小寫
- CountMatches - 計(jì)算一個(gè)字符串在另一個(gè)字符串中出現(xiàn)的次數(shù)
- IsAlpha/IsNumeric/IsWhitespace/IsAsciiPrintable - 檢查字符串中的字符
- DefaultString - 防止空輸入字符串
- Rotate - 旋轉(zhuǎn)(循環(huán)移位)字符串
- Reverse/ReverseDelimited - 反轉(zhuǎn)字符串
- Abbreviate - 使用省略號(hào)或另一個(gè)給定的字符串縮寫字符串
- Difference - 比較字符串并報(bào)告它們之間的差異
- LevenshteinDistance - 將一個(gè)字符串更改為另一個(gè)所需的更改次數(shù)
empyt和blank都是判空有什么區(qū)別:
" " isEmpty 返回false;isBlank返回true文章來源:http://www.zghlxwxcb.cn/news/detail-794610.html
一些常用的字符串常量:文章來源地址http://www.zghlxwxcb.cn/news/detail-794610.html
使用DEMO
1 判斷字符串是否為空或者空白:
import org.apache.commons.lang3.StringUtils;
public class StringUtilsDemo {
public static void main(String[] args) {
String str1 = "Hello, World!";
String str2 = "";
// 判斷字符串是否為空或者空白
System.out.println("Is str1 empty or blank? " + StringUtils.isBlank(str1));
System.out.println("Is str2 empty or blank? " + StringUtils.isBlank(str2));
}
}
2 連接多個(gè)字符串:
import org.apache.commons.lang3.StringUtils;
public class StringUtilsDemo {
public static void main(String[] args) {
String[] words = {"Hello", "World", "Java"};
// 連接多個(gè)字符串
String result = StringUtils.join(words, " ");
System.out.println("Result: " + result);
}
}
截取字符串的前幾個(gè)字符:
import org.apache.commons.lang3.StringUtils;
public class StringUtilsDemo {
public static void main(String[] args) {
String original = "Apache StringUtils Demo";
// 截取字符串的前幾個(gè)字符
String substring = StringUtils.left(original, 10);
System.out.println("Substring: " + substring);
}
}
4 移除字符串中的空格:
import org.apache.commons.lang3.StringUtils;
public class StringUtilsDemo {
public static void main(String[] args) {
String stringWithSpaces = " Remove Spaces ";
// 移除字符串中的空格
String result = StringUtils.deleteWhitespace(stringWithSpaces);
System.out.println("Result: " + result);
}
}
到了這里,關(guān)于Apache StringUtils:Java字符串處理工具類的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!