StringUtils常用方法
点击关注,与你共同成长!
StringUtils常用方法
需导入包:
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
常用方法:
1.isEmpty(String str)
判断传入的字符串是否为空,为空true,不为空false;为空标准:str == null 或 str.length() ==0
System.out.println("isEmptyTest:"+StringUtils.isEmpty(null));isNotEmptyTest:true
System.out.println("isEmptyTest:"+StringUtils.isEmpty(""));isNotEmptyTest:true
System.out.println("isEmptyTest:"+StringUtils.isEmpty(" "));isNotEmptyTest:false
System.out.println("isEmptyTest:"+StringUtils.isEmpty("ss"));isNotEmptyTest:false
2.isNotEmpty(String str)
判断传入的字符串是否不为空,等于 !isEmpty(String str)
System.out.println("isNotEmptyTest:"+ StringUtils.isNotEmpty(null));isNotEmptyTest:false
System.out.println("isNotEmptyTest:"+ StringUtils.isNotEmpty(""));isNotEmptyTest:false
System.out.println("isNotEmptyTest:"+ StringUtils.isNotEmpty(" "));isNotEmptyTest:true
System.out.println("isNotEmptyTest:"+ StringUtils.isNotEmpty("aa"));isNotEmptyTest:true
3.isBlank(String str)
判断传入字符串是否为空或长度为0或由空白字符构成,对于制表符、换行符、换页符和回车符StringUtils.isBlank()均识为空白符,"\b"为单词边界符不算空白字符。
System.out.println("isBlankTest:"+StringUtils.isBlank(null));isBlankTest:true
System.out.println("isBlankTest:"+StringUtils.isBlank(""));isBlankTest:true
System.out.println("isBlankTest:"+StringUtils.isBlank(" "));isBlankTest:true
System.out.println("isBlankTest:"+StringUtils.isBlank("aa"));isBlankTest:false
System.out.println("isBlankTest:"+StringUtils.isBlank("\t \n \f \r"));isBlankTest:true
System.out.println("isBlankTest:"+StringUtils.isBlank("\b"));isBlankTest:false
4.isNotBlank(String str)
等于!isBlank(String str)
System.out.println("isNotBlankTest:"+StringUtils.isNotBlank(null));isNotBlankTest:false
System.out.println("isNotBlankTest:"+StringUtils.isNotBlank(""));isNotBlankTest:false
System.out.println("isNotBlankTest:"+StringUtils.isNotBlank(" "));isNotBlankTest:false
System.out.println("isNotBlankTest:"+StringUtils.isNotBlank("aa"));isNotBlankTest:true
System.out.println("isNotBlankTest:"+StringUtils.isNotBlank("\t \n \f \r"));isNotBlankTest:false
System.out.println("isNotBlankTest:"+StringUtils.isNotBlank("\b"));isNotBlankTest:true
5.trim(String str)
去掉字符串俩端的控制符,如果输入为null返回null
System.out.println("trimTest:"+StringUtils.trim(null));trimTest:null
System.out.println("trimTest:"+StringUtils.trim(""));trimTest:
System.out.println("trimTest:"+StringUtils.trim(" "));trimTest:
System.out.println("trimTest:"+StringUtils.trim(" aaa "));trimTest:aaa
6.trimToNull(String str)
去掉字符串俩端的控制符,如果输入为null或"“或” ",返回null
System.out.println("trimToNullTest:"+StringUtils.trimToNull(null));trimToNullTest:null
System.out.println("trimToNullTest:"+StringUtils.trimToNull(""));trimToNullTest:null
System.out.println("trimToNullTest:"+StringUtils.trimToNull(" "));trimToNullTest:null
System.out.println("trimToNullTest:"+StringUtils.trimToNull(" aaa "));trimToNullTest:aaa
7.trimToEmpty(String str)
去掉字符串俩端的控制符,如果输入为null或"“或” “,返回”"
System.out.println("trimToEmptyTest:"+StringUtils.trimToEmpty(null));trimToEmptyTest:
System.out.println("trimToEmptyTest:"+StringUtils.trimToEmpty(""));trimToEmptyTest:
System.out.println("trimToEmptyTest:"+StringUtils.trimToEmpty(" "));trimToEmptyTest:
System.out.println("trimToEmptyTest:"+StringUtils.trimToEmpty(" aaa "));trimToEmptyTest:aaa
8.strip(String str)
去掉字符串两端的空白符,如果输入为null则返回null
System.out.println("stripTest:"+StringUtils.strip(null));stripTest:null
System.out.println("stripTest:"+StringUtils.strip(""));stripTest:
System.out.println("stripTest:"+StringUtils.strip(" "));stripTest:
System.out.println("stripTest:"+StringUtils.strip(" aaa "));stripTest:aaa
9.strip(String str, String stripChars)
去掉str两端的在stripChars中的字符。如果str为null或等于"",则返回它本身;如果stripChars为null或"",则返回strip(String str)。
System.out.println("stripTest:"+StringUtils.strip(" aaa ","a"));stripTest: aaa //没效果
10.stripStart(String str, String stripChars)
去掉str前端的在stripChars中的字符。
System.out.println("stripStartTest:"+StringUtils.stripStart("avvva","a"));stripStartTest:vvva
11.stripEnd(String str, String stripChars)
去掉str后端的在stripChars中的字符。
System.out.println("stripEndTest:"+StringUtils.stripEnd("avvva","a"));stripEndTest:avvv
12.stripAll(String[] strs)
对字符串数组中的每个字符串进行strip(String str),然后返回。如果strs为null或strs长度为0,则返回strs本身
String[] strings = {" aaa ","bb "," cc"};
for (String s : StringUtils.stripAll(strings)) {
System.out.println("stripAllTest:"+s);
}
stripAllTest:aaa
stripAllTest:bb
stripAllTest:cc
13.stripAll(String[] strs, String stripChars)
对字符串数组中的每个字符串进行strip(String str, String stripChars),然后返回。如果strs为null或strs长度为0,则返回strs本身
for (String s : StringUtils.stripAll(strings,"b")) {
System.out.println("stripAllTest:"+s);
}
stripAllTest: aaa
stripAllTest:
stripAllTest: cc
14.equals(String str1, String str2)
比较两个字符串是否相等,如果两个均为空则也认为相等。
System.out.println("equalsTest:"+StringUtils.equals("a","a"));equalsTest:true
System.out.println("equalsTest:"+StringUtils.equals("a","A"));equalsTest:false
System.out.println("equalsTest:"+StringUtils.equals("",""));equalsTest:true
System.out.println("equalsTest:"+StringUtils.equals(null,null));equalsTest:true
System.out.println("equalsTest:"+StringUtils.equals("",null));equalsTest:false
15.equalsIgnoreCase(String str1, String str2)
比较两个字符串是否相等,不区分大小写,如果两个均为空则也认为相等。
System.out.println("equalsIgnoreCaseTest:"+StringUtils.equalsIgnoreCase("a","a"));equalsIgnoreCaseTest:true
System.out.println("equalsIgnoreCaseTest:"+StringUtils.equalsIgnoreCase("a","A"));equalsIgnoreCaseTest:true
16.indexOf(String str, char searchChar)
返回字符searchChar在字符串str中第一次出现的位置。如果searchChar没有在str中出现则返回-1,如果str为null或"",则也返回-1
System.out.println("indexOfTest:"+StringUtils.indexOf("abcd","b"));indexOfTest:1
System.out.println("indexOfTest:"+StringUtils.indexOf("","b"));indexOfTest:-1
System.out.println("indexOfTest:"+StringUtils.indexOf(null,"b"));indexOfTest:-1
System.out.println("indexOfTest:"+StringUtils.indexOf(" ","b"));indexOfTest:-1
17.indexOf(String str, char searchChar, int startPos)
返回字符searchChar从startPos开始在字符串str中第一次出现的位置。
System.out.println("indexOfTest:"+StringUtils.indexOf("abcad","a",1));indexOfTest:3
以上,便是今天的分享,希望大家喜欢,觉得内容不错的,欢迎「分享」「赞」或者点击「在看」支持,谢谢各位。