1.commons-lang3 概述
apache提供的眾多commons工具包,號稱Java第二API,而common里面lang3包更是被我們使用得最多的。因此本文主要詳細講解lang3包里面幾乎每個類的使用,希望以后大家使用此工具包,寫出優雅的代碼。
maven依賴:
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.11</version> </dependency>
API參考文檔:https://commons.apache.org/proper/commons-lang/javadocs/api-release/index.html
1.1 commons-lang3和commons-lang的區別
lang3是Apache Commons 團隊發布的工具包,要求jdk版本在1.5以上,相對於lang來說完全支持java5的特性,廢除了一些舊的API。該版本無法兼容舊有版本,於是為了避免沖突改名為lang3
注意:lang包可以說是廢棄了,以后請不要使用。采用lang3直接代替即可
2.StringUtils 工具類
2.1 isEmpty方法與isNotEmpty方法
(1)isEmpty方法
判斷某字符串是否為空,為空的標准是 str==null 或 str.length()==0
方法API:
public static boolean isEmpty(CharSequence cs)
下面是 StringUtils 判斷是否為空的示例:
public class StringUtilsTest { public static void main(String[] args) { System.out.println(StringUtils.isEmpty(null));//true System.out.println(StringUtils.isEmpty(""));//true System.out.println(StringUtils.isEmpty(" ")); //false 注意在 StringUtils 中空格作非空處理 System.out.println(StringUtils.isEmpty(" "));//false System.out.println(StringUtils.isEmpty("bob"));//false System.out.println(StringUtils.isEmpty(" bob "));//false } }
(2) isNotEmpty方法
判斷某字符串是否非空,等於 !isEmpty(String str)
方法API:
public static boolean isNotEmpty(String str)
下面是 StringUtils 判斷是否非空的示例:
import org.apache.commons.lang3.StringUtils; /** * @Author lucky * @Date 2021/12/14 18:48 */ public class StringUtilsTest { public static void main(String[] args) { System.out.println(StringUtils.isNotEmpty(null));//false System.out.println(StringUtils.isNotEmpty(""));//false System.out.println(StringUtils.isNotEmpty(" ")); //true 注意在 StringUtils 中空格作非空處理 System.out.println(StringUtils.isNotEmpty(" "));//true System.out.println(StringUtils.isNotEmpty("bob"));//true System.out.println(StringUtils.isNotEmpty(" bob "));//true } }
2.2 isBlank方法與isNotBlank方法
(1)isBlank方法
判斷某字符串是否為空或長度為0或由空白符(whitespace) 構成
public class StringUtilsTest { public static void main(String[] args) { System.out.println(StringUtils.isBlank(null));//true System.out.println(StringUtils.isBlank(""));//true System.out.println(StringUtils.isBlank(" ")); //true 注意在 StringUtils 中空格作非空處理 System.out.println(StringUtils.isBlank(" "));//true System.out.println(StringUtils.isBlank("bob"));//false System.out.println(StringUtils.isBlank(" bob "));//false } }
(2)isNotBlank方法
判斷某字符串是否不為空且長度不為0且不由空白符(whitespace) 構成,等於 !isBlank(String str)
2.3 trim方法
移除字符串兩端的空字符串,制表符char <= 32如:\n \t
public static String trim(String str)
案例:
/** * @Author lucky * @Date 2021/12/14 18:48 */ public class StringUtilsTest { public static void main(String[] args) { System.out.println(StringUtils.trim(null)); //null System.out.println(StringUtils.trim(""));//"" System.out.println(StringUtils.trim(" abc "));//abc } }
2.4 equals方法與equalsIgnoreCase方法
(1) equals方法
字符串比對方法,是比較實用的方法之一,兩個比較的字符串都能為空,不會報空指針異常。
public static boolean equals(CharSequence cs1,CharSequence cs2)
案例:
public class StringUtilsTest { public static void main(String[] args) { System.out.println(StringUtils.equals(null, null)); //true System.out.println(StringUtils.equals(null, "abc"));//false System.out.println(StringUtils.equals("abc", "abc"));//true System.out.println(StringUtils.equals("abc", "ABC"));//false } }
(2)equalsIgnoreCase方法
變體字符串比較(忽略大小寫),在驗證碼……等字符串比較,真是很實用。
public class StringUtilsTest { public static void main(String[] args) { System.out.println(StringUtils.equalsIgnoreCase("abc", "abc")); //true System.out.println(StringUtils.equalsIgnoreCase("abc", "ABC"));//true } }
2.5 indexOf方法
查找 CharSequence seq中CharSequence searchSeq出現的第一個索引位置,同時處理 null。
public static int indexOf(CharSequence seq,CharSequence searchSeq)
案例:
public class StringUtilsTest { public static void main(String[] args) { System.out.println(StringUtils.indexOf(null, "")); //-1 System.out.println(StringUtils.indexOf(null, null)); //-1 System.out.println(StringUtils.indexOf("aabaabaa", "a"));//0 System.out.println(StringUtils.indexOf("aabaabaa", "b"));//2 System.out.println(StringUtils.indexOf("aabaabaa", "ab"));//1 } }
2.6 contains與containsAny
(1)contains
字符串seq是否包含searchChar
public static boolean contains(CharSequence seq,CharSequence searchSeq)
(2)containsAny
檢查 CharSequence 是否包含給定字符集中的任何字符。
public static boolean containsAny(CharSequence cs,CharSequence searchChars)
public static boolean containsAny(CharSequence cs, char... searchChars)
案例:
public class StringUtilsTest { public static void main(String[] args) { System.out.println(StringUtils.contains("abc", 'z')); //false System.out.println(StringUtils.contains("abc", 'c')); //true char[] cArray={'z','a'}; System.out.println(StringUtils.containsAny("zzabyycdxx", cArray)); //true System.out.println(StringUtils.containsAny("zzabyycdxx", "ac")); //true System.out.println(StringUtils.containsAny("zzabyycdxx", "ef")); //false } }
2.7 substring
字符串截取
public static String substring(String str,int start)
public static String substring(String str,int start,int end)
案例:
public class StringUtilsTest { public static void main(String[] args) { System.out.println(StringUtils.substring("abcdef", 2, 5)); //cde System.out.println(StringUtils.substring("abcdef", 2, 8)); //cdef System.out.println(StringUtils.substring("abcdef", 2)); //cdef } }
2.8 split方法
字符串分割
public static String[] split(String str,char separatorChar)
案例:
System.out.println(Arrays.toString(StringUtils.split("ab:cd:ef", ":"))); //[ab, cd, ef]
2.9 join方法
字符串拼接
public static String join(Object[] array,char separator)
案例:
String[] strArray={"ab","cd","efg"}; System.out.println(StringUtils.join(strArray, ":")); //ab:cd:efg
2.10 deleteWhitespace方法
刪除空格
System.out.println(StringUtils.deleteWhitespace(" ab c ")); //abc
2.11 capitalize方法(首字母大寫)與uncapitalize方法
(1) capitalize方法
首字母小寫轉為大寫
System.out.println(StringUtils.capitalize("cat")); //Cat
(2) uncapitalize方法
首字母大寫轉為小寫
System.out.println(StringUtils.uncapitalize("Cat")); //cat System.out.println(StringUtils.uncapitalize("CCat")); //cCat
2.12 isAlpha方法
判斷字符串是否由字母組成
public static boolean isAlpha(CharSequence cs)
案例:
System.out.println(StringUtils.isAlpha("abc")); //true System.out.println(StringUtils.isAlpha("ab2c")); //false
2.13 reverse方法
字符串翻轉
public static String reverse(String str)
案例:
System.out.println(StringUtils.reverse("bat") ); //tab
2.14 countMatches方法
統計一字符串在另一字符串中出現次數
public static int countMatches(CharSequence str,CharSequence sub)
案例:
System.out.println(StringUtils.countMatches("abba", "a")); //2 System.out.println(StringUtils.countMatches("abba", "ab")); //1
2.15 startsWith與endsWith方法
(1)startsWith
檢查起始字符串是否匹配
public static boolean startsWith(CharSequence str,CharSequence prefix)
(2)endsWith
檢查字符串結尾后綴是否匹配
public static boolean endsWith(CharSequence str,CharSequence suffix)
(3)案例
System.out.println(StringUtils.startsWith("abcdef", "abc")); //true System.out.println(StringUtils.startsWith("abcdef", "abd")); //false System.out.println(StringUtils.endsWith("abcdef", "def")); //true System.out.println(StringUtils.endsWith("abcdef", "ded")); //false
2.16 upperCase與lowerCase方法
大小寫轉換
public static String upperCase(String str) public static String lowerCase(String str)
案例:
System.out.println(StringUtils.upperCase("aBc")); //ABC System.out.println(StringUtils.lowerCase("aBc")); //abc
2.17 repeat方法
重復字符
public static String repeat(String str, int repeat)
案例:
System.out.println(StringUtils.repeat('e', 5)); //eeeee System.out.println(StringUtils.repeat("abc", 3)); //abcabcabc
3.RandomUtils
RandomUtils幫助我們產生隨機數,不止是數字類型,連boolean類型都可以通過RandomUtils產生。
3.1 nextInt方法
指定范圍生成int
類型的隨機數
public static int nextInt()
案例:
System.out.println(RandomUtils.nextInt(0,1000) ); //在0-1000之間產生一位隨機數
3.2 nextBoolean方法
返回一個隨機布爾值
public static boolean nextBoolean()
案例:
System.out.println(RandomUtils.nextBoolean()); //隨機生成一個boolean值
3.3 nextDouble方法
指定范圍生成double
類型的隨機數
public static double nextDouble(double startInclusive,double endExclusive)
案例:
System.out.println(RandomUtils.nextDouble(1, 5)); //4.053482296310822
4.RandomStringUtils
4.1 random方法
(1)創建一個隨機字符串,其長度是指定的字符數,字符將從參數的字母數字字符集中選擇
public static String random(int count,boolean letters,boolean numbers)
案例:
public static void main(String[] args) { /** * 創建一個隨機字符串,其長度是指定的字符數,字符將從參數的字母數字字符集中選擇 * count 生成的字符串的長度 * letters true,生成的字符串可以包括字母字符 * numbers true,生成的字符串可以包含數字字符 */ System.out.println(RandomStringUtils.random(15, true, false));//mLuCZXhpVuskRrw System.out.println(RandomStringUtils.random(15, true, true));//iZyUi3SVoyhwFNJ }
(2)創建一個隨機字符串,其長度是指定的字符數
public static String random(int count, String chars)
案例:
/** * 創建一個隨機字符串,其長度是指定的字符數。 * 字符將從字符串指定的字符集中選擇,不能為空。如果NULL,則使用所有字符集。 */ System.out.println(RandomStringUtils.random(15, "abcdefgABCDEFG123456789"));//E2edaFGF74B24E8
4.2 randomAlphabetic
產生一個長度為指定的隨機字符串的字符數,字符將從拉丁字母(a-z、A-Z)
public static String randomAlphabetic(int count)
案例:
/** * 產生一個長度為指定的隨機字符串的字符數,字符將從拉丁字母(a-z、A-Z的選擇)。 * count:創建隨機字符串的長度 */ System.out.println(RandomStringUtils.randomAlphabetic(15));//EoDyGRexfgbFblf
4.3 randomAlphanumeric
創建一個隨機字符串,其長度是指定的字符數,字符將從拉丁字母(a-z、A-Z)和數字0-9中選擇
public static String randomAlphanumeric(int count)
案例:
/** * 創建一個隨機字符串,其長度是指定的字符數,字符將從拉丁字母(a-z、A-Z)和數字0-9中選擇。 * count :創建的隨機數長度 */ System.out.println(RandomStringUtils.randomAlphanumeric(15));//vu9RBXAd8JWttbw
4.4 randomNumeric
創建一個隨機字符串,其長度是指定的字符數,將從數字字符集中選擇字符
public static String randomNumeric(int count)
案例:
/** * 創建一個隨機字符串,其長度是指定的字符數,將從數字字符集中選擇字符。 * count:生成隨機數的長度 */ System.out.println(RandomStringUtils.randomNumeric(15));//174076343426817
參考文獻:
https://blog.csdn.net/wangmx1993328/article/details/102488632/
https://blog.csdn.net/f641385712/article/details/82468927
https://blog.csdn.net/wuge507639721/article/details/81532438
https://www.jianshu.com/p/1886903ed14c----經典
https://blog.csdn.net/yaomingyang/article/details/79107764----RandomStringUtils經典