📝个人主页🌹:个人主页
⏩收录专栏⏪:日常经验
🌹🌹期待您的关注 🌹🌹,让我们共同进步!
总是区分不清楚这几个的差别:我们来直接验证一下:
package com.atguigu.signcenter.controller;import com.atguigu.signcenter.annotation.ExistsApiToken;
import com.atguigu.signcenter.annotation.VerifySign;
import com.atguigu.signcenter.constant.ReqParameterConstant;
import com.atguigu.signcenter.entity.User1;
import com.atguigu.signcenter.service.SecurityUtilTestService;
import com.atguigu.signcenter.util.testUtil;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;import java.io.UnsupportedEncodingException;
import java.util.*;/*** 原文链接:https://blog.csdn.net/weixin_47560078/article/details/118222785* @author: jd* @create: 2024-07-30*/@Slf4j
@Controller
public class TestController {public static void main(String[] args) {//判断某字符串是否为空或长度为0或由空白符(whitespace)构成System.out.println("=========isBlank=========");System.out.println("StringUtils.isBlank(null) = " + StringUtils.isBlank(null)); //trueSystem.out.println("StringUtils.isBlank(\"\") = " + StringUtils.isBlank("")); //trueSystem.out.println("StringUtils.isBlank(\" \") = " + StringUtils.isBlank(" ")); //trueSystem.out.println("StringUtils.isBlank(\"\\t \\n \\f \\r\") = " + StringUtils.isBlank("\t \n \f \r")); // trueSystem.out.println("StringUtils.isBlank(\"\\b\") = " + StringUtils.isBlank("\b")); //falseSystem.out.println("StringUtils.isBlank(\"bob\") = " + StringUtils.isBlank("bob")); //falseSystem.out.println("StringUtils.isBlank(\" bob \") = " + StringUtils.isBlank(" bob ")); //falseSystem.out.println("=========isNotBlank=========");//判断某字符串是否不为空且长度不为0且不由空白符(whitespace)构成,等于!isBlank(String str)System.out.println("StringUtils.isNotBlank(null) = " + StringUtils.isNotBlank(null));System.out.println("StringUtils.isNotBlank(\"\") = " + StringUtils.isNotBlank(""));System.out.println("StringUtils.isNotBlank(\" \") = " + StringUtils.isNotBlank(" "));System.out.println("StringUtils.isNotBlank(\" \") = " + StringUtils.isNotBlank(" "));System.out.println("StringUtils.isNotBlank(\"\\t \\n \\f \\r\") = " + StringUtils.isNotBlank("\t \n \f \r"));System.out.println("StringUtils.isNotBlank(\"\\b\") = " + StringUtils.isNotBlank("\b"));System.out.println("StringUtils.isNotBlank(\"bob\") = " + StringUtils.isNotBlank("bob"));System.out.println("StringUtils.isNotBlank(\" bob \") = " + StringUtils.isNotBlank(" bob "));System.out.println("=========isEmpty=========");//判断某字符串是否为空,为空的标准是str==null或str.length()==0System.out.println("StringUtils.isEmpty(null) = " + StringUtils.isEmpty(null)); //trueSystem.out.println("StringUtils.isEmpty(\"\") = " + StringUtils.isEmpty("")); //trueSystem.out.println("StringUtils.isEmpty(\" \") = " + StringUtils.isEmpty(" ")); //falseSystem.out.println("StringUtils.isEmpty(\"bob\") = " + StringUtils.isEmpty("bob")); //falseSystem.out.println("StringUtils.isEmpty(\" bob \") = " + StringUtils.isEmpty(" bob ")); //false//只会检查传入的对象是否为null。如果对象为null,则返回true,否则返回falseSystem.out.println("Objects.isNull(null) = " + Objects.isNull(null)); //trueSystem.out.println("Objects.isNull(\"\") = " + Objects.isNull("")); //falseSystem.out.println("Objects.isNull(\"aaa\") = " + Objects.isNull("aaa")); //false}}
结果
=========isBlank=========
StringUtils.isBlank(null) = true
StringUtils.isBlank("") = true
StringUtils.isBlank(" ") = true
StringUtils.isBlank("\t \n \f \r") = true
StringUtils.isBlank("\b") = false
StringUtils.isBlank("bob") = false
StringUtils.isBlank(" bob ") = false
=========isNotBlank=========
StringUtils.isNotBlank(null) = false
StringUtils.isNotBlank("") = false
StringUtils.isNotBlank(" ") = false
StringUtils.isNotBlank(" ") = false
StringUtils.isNotBlank("\t \n \f \r") = false
StringUtils.isNotBlank("\b") = true
StringUtils.isNotBlank("bob") = true
StringUtils.isNotBlank(" bob ") = true
=========isEmpty=========
StringUtils.isEmpty(null) = true
StringUtils.isEmpty("") = true
StringUtils.isEmpty(" ") = false
StringUtils.isEmpty("bob") = false
StringUtils.isEmpty(" bob ") = false
=========Objects.isNull=========
Objects.isNull(null) = true
Objects.isNull("") = false
Objects.isNull("aaa") = false
总结:
我这里对于判空和判断非空都验证了,其实只是记住一方面即可。
判断是null的范围,从粗到细 isBlank < isEmpty < isNUll
//所以总结一下,isBlank的筛选是最粗的,对于null “” “ ” 都是空的,这三个区分不出来,都是true
// isEmpty 则能把null 和“” 这两个区分不出来,都是 true
//isNull 则是最细的 只有真正的null是true,否则是false