目录
1.isEmpty/isNotEmpty:判断字符串是否为 null 或长度是否为 0;/是否不为 null 且长度不为 0;
2.isBlank:判断字符串是否为 null、空字符串或全为空格字符;
3.1.判断字符串是否不为 null、不为空字符串且不全为空格字符。
4.isAnyEmpty判断多个字符序列是否都为空,如果有一个为空则返回 true。
5.isNoneEmpty() 与 isAnyEmpty 方法功能相反,当所有字符序列都不为空时返回 true。
6.判断字符串是否相等StringUtils.equals和String.equals
StringUtils工具类主要用于操作java.lang.Sting对象。lang3工具包提供。
这个工具是在这个依赖里面的
依赖
<!-- lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${lang3.version}</version>
</dependency>
<lang3.version>3.8.1</lang3.version>
1.isEmpty/isNotEmpty:判断字符串是否为 null 或长度是否为 0;/是否不为 null 且长度不为 0;
package com.lpc.lang3Test;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
/**
* @Author lpc
* @Date 2024 01 08 14 03
**/
public class Lang3 {
@Test
public void test(){
/**
* StringUtils.isEmpty 判断是空
* StringUtils.isNotEmpty 判断不是空
*/
String username="123456";
System.out.println(StringUtils.isEmpty(username)); //false
System.out.println(StringUtils.isNotEmpty(username));//true
//username2为空,那么它是怎么判断的?
String username2="";
System.out.println(StringUtils.isEmpty(username2)); //true
System.out.println(StringUtils.isNotEmpty(username2));//false
//username3为空格,那么它是怎么判断的?认为空格也是字符串
String username3=" ";
System.out.println(StringUtils.isEmpty(username3)); //false
System.out.println(StringUtils.isNotEmpty(username3));//true
String username4="路飞";
System.out.println(StringUtils.isEmpty(username4)); //false
System.out.println(StringUtils.isNotEmpty(username4));//true
String username5="....";
System.out.println(StringUtils.isEmpty(username5)); //false
System.out.println(StringUtils.isNotEmpty(username5));//true
}
}
2.isBlank:判断字符串是否为 null、空字符串或全为空格字符;
@Test
public void test2(){
/**
* 判断字符串是否为 null、空字符串或全为空格字符;
*/
String username="123456";
System.out.println(StringUtils.isBlank(username));//判断username是否为空,false
//username2为空,那么它是怎么判断的?
String username2="";
System.out.println(StringUtils.isBlank(username2));//判断username2是否为空,true
//username3为空格,那么它是怎么判断的?认为空格为空
String username3=" ";
System.out.println(StringUtils.isBlank(username3)); //判断username3是否为空,true
//username4中有空格,那么它是怎么判断的?
String username4="123 456";
System.out.println(StringUtils.isBlank(username4)); //判断username4是否为空,false
}
3.isNotBlank
3.1.判断字符串是否不为 null、不为空字符串且不全为空格字符。
@Test
public void test22(){
/**
* isNotBlank:判断字符串是否不为 null、不为空字符串且不全为空格字符。
*/
String username="123456";
System.out.println(StringUtils.isNotBlank(username));//判断username是否不为空,true
//username2为空,那么它是怎么判断的?
String username2="";
System.out.println(StringUtils.isNotBlank(username2));//判断username2是否不为空,false
//username3全为空格,那么它是怎么判断的?认为空格为空
//username3全为空格,认为它是为空的
String username3=" ";
System.out.println(StringUtils.isNotBlank(username3)); //判断username3是否不为空,false
//username4中有空格,那么它是怎么判断的?
//username4中有空格,判断它是不为空的
String username4="123 456";
System.out.println(StringUtils.isNotBlank(username4)); //判断username4是否不为空,true
//前面是空格,后面是字符串
//认为它是不为空的
String username5=" 123456";
System.out.println(StringUtils.isNotBlank(username5)); //判断username5是否不为空,true
}
3.2.判断传过来的字符串不为空,就能进行模糊匹配
示例:这个代码是不全的 看like就行
public Page<E> getList(String startTime, String endTime, Integer current, Integer size,String name)
Page<E> result = this.page(page, Wrappers.lambdaQuery(E.class)
.like(StringUtils.isNotBlank(name), E::getName, name)
4.isAnyEmpty判断多个字符序列是否都为空,如果有一个为空则返回 true。
@Test
public void test3(){
/**
* isAnyEmpty判断多个字符序列是否都为空,如果有一个为空则返回 true。
*/
System.out.println(StringUtils.isAnyEmpty("","六花","勇太")); //这里面有一个为空,那么返回true
System.out.println(StringUtils.isAnyEmpty("六花","520","勇太"));//这里面都不为空,那么返回false
}
5.isNoneEmpty() 与 isAnyEmpty 方法功能相反,当所有字符序列都不为空时返回 true。
@Test
public void test4(){
/**
* isNoneEmpty() ,当所有字符序列都不为空时返回 true。
*/
System.out.println(StringUtils.isNoneEmpty("","六花","勇太")); //这里面有一个为空,那么返回false
System.out.println(StringUtils.isNoneEmpty("六花","520","勇太"));//这里面都不为空,那么返回true
}
6.判断字符串是否相等StringUtils.equals和String.equals
@Test
public void Users13() {
// 这种情况下二者是一样的,
String a = "";
String b = "";
System.out.println(StringUtils.equals(a,b));//true
System.out.println(a.equals(b));//true
}
@Test
public void Users14() {
// 这种情况下是会出问题的,
String a = null;
String b = null;
System.out.println(StringUtils.equals(a,b));//true
System.out.println(a.equals(b));//报空指针,因为a为null
}
-
实际工作中推荐使用StringUtils.equals(),不管2个那个为null,都可以防止空指针,如果要用a.equals(b)这个,那前提是a不能为null,b可以为null,这样也可以防止空指针,工作中可能你不知道那个会为null,所以还是用StringUtils.equals()这个比较好.
7.StringUtils.isNoneBlank(参数)
如果字符串是空、只有空格组成、或者字符串变量指向null,那么返回true,这个方法很适合填写表单的时候用来检查是不是用户有表单没有填写,或者只输入了空格。怎么使用给一个案例
@Test
public void Users11() {
String name = ""; // 空字符串
String address = " "; // 只包含空格
String phoneNumber = null; // 字符串变量为 null
String city = "New York"; // 有效的字符串
// 使用 StringUtils.isNoneBlank 判断字符串是否为空
if (StringUtils.isNoneBlank(name)) {
System.out.println("姓名已填写");
} else {
System.out.println("姓名未填写");
}
if (StringUtils.isNoneBlank(address)) {
System.out.println("地址已填写");
} else {
System.out.println("地址未填写");
}
if (StringUtils.isNoneBlank(phoneNumber)) {
System.out.println("电话号码已填写");
} else {
System.out.println("电话号码未填写");
}
if (StringUtils.isNoneBlank(city)) {
System.out.println("城市已填写");
} else {
System.out.println("城市未填写");
}
}