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

摘要:
isEmpty方法API:publicstaticbooleanisNotEmpty下面是StringUtils判断是否非空的示例:importorg.apache.commons.lang3.StringUtils;/***@Authorlucky*@Date2021/12/1418:48*/publicclassStringUtilsTest{publicstaticvoidmain{System.out.println;//falseSystem.out.println;//falseSystem.out.println;//true注意在StringUtils中空格作非空处理System.out.println;//trueSystem.out.println;//trueSystem.out.println;//true}}2.2isBlank方法与isNotBlank方法isBlank方法判断某字符串是否为空或长度为0或由空白符构成publicclassStringUtilsTest{publicstaticvoidmain{System.out.println;//trueSystem.out.println;//trueSystem.out.println;//true注意在StringUtils中空格作非空处理System.out.println;//trueSystem.out.println;//falseSystem.out.println;//false}}isNotBlank方法publicstaticbooleanisNotBlank判断某字符串是否不为空且长度不为0且不由空白符构成,等于!

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.1isEmpty方法与isNotEmpty方法

(1)isEmpty方法

判断某字符串是否为空,为空的标准是 str==null 或 str.length()==0

方法API:

public static boolean isEmpty(CharSequence cs)

下面是 StringUtils 判断是否为空的示例:

public classStringUtilsTest {
    public static voidmain(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 判断是否非空的示例:

importorg.apache.commons.lang3.StringUtils;

/*** @Author lucky
 * @Date 2021/12/14 18:48
 */
public classStringUtilsTest {
    public static voidmain(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.2isBlank方法与isNotBlank方法

(1)isBlank方法

判断某字符串是否为空或长度为0或由空白符(whitespace) 构成

public classStringUtilsTest {
    public static voidmain(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.3trim方法

移除字符串两端的空字符串,制表符char <= 32如:\n \t

public static String trim(String str)

案例:

/*** @Author lucky
 * @Date 2021/12/14 18:48
 */
public classStringUtilsTest {
    public static voidmain(String[] args) {
        System.out.println(StringUtils.trim(null)); //null
        System.out.println(StringUtils.trim(""));//""
        System.out.println(StringUtils.trim("    abc    "));//abc
}
}

2.4equals方法与equalsIgnoreCase方法

(1)equals方法

字符串比对方法,是比较实用的方法之一,两个比较的字符串都能为空,不会报空指针异常。

public static boolean equals(CharSequence cs1,CharSequence cs2)

案例:

public classStringUtilsTest {
    public static voidmain(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 classStringUtilsTest {
    public static voidmain(String[] args) {
        System.out.println(StringUtils.equalsIgnoreCase("abc", "abc")); //true
        System.out.println(StringUtils.equalsIgnoreCase("abc", "ABC"));//true
}
}

2.5indexOf方法

查找 CharSequence seq中CharSequence searchSeq出现的第一个索引位置,同时处理 null。

public static int indexOf(CharSequence seq,CharSequence searchSeq)

案例:

public classStringUtilsTest {
    public static voidmain(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.6contains与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 staticbooleancontainsAny(CharSequencecs,
                                  char...searchChars)

案例:

public classStringUtilsTest {
    public static voidmain(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.7substring

字符串截取

public static String substring(String str,int start)
public staticStringsubstring(Stringstr,intstart,intend)

案例:

public classStringUtilsTest {
    public static voidmain(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.8split方法

字符串分割

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.10deleteWhitespace方法

删除空格

System.out.println(StringUtils.deleteWhitespace("   ab  c  ")); //abc

2.11capitalize方法(首字母大写)与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.12isAlpha方法

判断字符串是否由字母组成

public static boolean isAlpha(CharSequence cs)

案例:

System.out.println(StringUtils.isAlpha("abc")); //true
System.out.println(StringUtils.isAlpha("ab2c")); //false

2.13reverse方法

字符串翻转

public static String reverse(String str)

案例:

System.out.println(StringUtils.reverse("bat") ); //tab

2.14countMatches方法

统计一字符串在另一字符串中出现次数

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.15startsWith与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.16upperCase与lowerCase方法

大小写转换

public staticString upperCase(String str)
public static String lowerCase(String str)

案例:

System.out.println(StringUtils.upperCase("aBc")); //ABC
System.out.println(StringUtils.lowerCase("aBc")); //abc

2.17repeat方法

重复字符

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.1nextInt方法

指定范围生成int类型的随机数

public static int nextInt()

案例:

System.out.println(RandomUtils.nextInt(0,1000) ); //在0-1000之间产生一位随机数

3.2nextBoolean方法

返回一个随机布尔值

public static boolean nextBoolean()

案例:

System.out.println(RandomUtils.nextBoolean()); //随机生成一个boolean值

3.3nextDouble方法

指定范围生成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 voidmain(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.2randomAlphabetic

产生一个长度为指定的随机字符串的字符数,字符将从拉丁字母(a-z、A-Z)

public static String randomAlphabetic(int count)

案例:

/*** 产生一个长度为指定的随机字符串的字符数,字符将从拉丁字母(a-z、A-Z的选择)。
         * count:创建随机字符串的长度
         */System.out.println(RandomStringUtils.randomAlphabetic(15));//EoDyGRexfgbFblf

4.3randomAlphanumeric

创建一个随机字符串,其长度是指定的字符数,字符将从拉丁字母(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.4randomNumeric

创建一个随机字符串,其长度是指定的字符数,将从数字字符集中选择字符

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经典

免责声明:文章转载自《Java基础知识15commonslang3 第3方开源库的具体使用01(StringUtils、RandomUtils类、RandomStringUtils类)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇使用NODEJS+REDIS开发一个消息队列以及定时任务处理第11章 PADS功能使用技巧(2)-最全面下篇

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

相关文章

在springmvc中配置jedis(转)

主要学习https://github.com/thinkgem/jeesite。一下代码均参考于此并稍作修改。 1.jedis 首先,需要添加jedis: <!--jedis--> <dependency> <groupId>redis.clients</groupId> &l...

MySqlHelper、CacheHelper

MySqlHelper代码: using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Configuration; using System.Data; using M...

Springboot+post请求接口

本文是Springboot+post请求接口的生成,包含了带cookie 和参数发送post请求。新增lombok框架,lombok的使用可以不用在类中添加成员变量的get/set方法。框架自带了相关方法。如不需要再添加以下内容 public String getUsename() { return usename; } p...

.NET Core MD5加密 32位和16位

public class MD5Help {   //此代码示例通过创建哈希字符串适用于任何 MD5 哈希函数 (在任何平台) 上创建 32 个字符的十六进制格式哈希字符串官网案例改编   /// <summary>   ///获取32位md5加密   /// </summary>   /// <param nam...

java-response-乱码解决

(1)响应体设置文本 PrintWriter getWriter() 获得字符流,通过字符流的write(String s)方法可以将字符串设置到response 缓冲区中,随后Tomcat会将response缓冲区中的内容组装成Http响应返回给浏览   器端。 关于设置中文的乱码问题 原因:response缓冲区的默认编码是iso8859-1,此码表中...

Java中判断String不为空的问题性能比较

 function 1: 最多人使用的一个方法, 直观, 方便, 但效率很低. function 2: 比较字符串长度, 效率高, 是我知道的最好一个方法. function 3: Java SE 6.0 才开始提供的方法, 效率和方法二几乎相等, 但出于兼容性考虑, 不推荐使用    以下是三种方法在机器上的运行结果: (机器性能不一, 仅供参考) fu...