Java基础知识15commonslang3 第3方开源库的具体使用01(StringUtils、RandomUtils类、RandomStringUtils类)

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方法

public static boolean isNotBlank(String str)
判断某字符串是否不为空且长度不为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经典

原文地址:https://www.cnblogs.com/luckyplj/p/15688466.html