The use of various toolboxes in Java

The use of various toolboxes in Java

[1] StrUtil

(1) Commonly used methods

String str = "abCDEfghi";
//是否为空
boolean blank = StrUtil.isBlank(str);//false
//是否不为空
boolean notBlank = StrUtil.isNotBlank(str);//true
//去掉字符串后缀(removeprefix:前缀)
String removeSuffix = StrUtil.removeSuffix("test.txt", ".txt");//test
//忽略大小写去掉前缀(removeSuffixIgnoreCase:去掉后缀)
String removePrefixIgnoreCase = StrUtil.removePrefixIgnoreCase(str, "A");//bCDEfghi
//sub方法
//顺数第2个到第4个,包含尾部包含头
String sub = StrUtil.sub(str, 2, 4);//CD
//-3表示倒数第三个字符
String sub1 = StrUtil.sub(str, 2, -3);//CDEf
//format方法(使用字符串模板代替字符串拼接)
String template = "{}爱{}!{}";
String fin = StrUtil.format(template, "我", "JAVA","哈"); //我爱JAVA!哈

(2) hasBlank, hasEmpty methods

It is to give some strings, if it returns true once it is empty, it is often used to judge whether many fields are empty (such as web form data).

The difference between these two methods is that hasEmpty only judges whether it is null or an empty string (""), while hasBlank also counts invisible characters as empty, and isEmpty and isBlank are the same.

(3) sub method

To avoid the subString method out of bounds problem, the index position also supports negative numbers, -1 means the last character (this idea comes from Python), and if you accidentally reverse the first position and the second position, it will automatically Amendments (for example, if you want to intercept the part between the 4th and 2nd characters, it is also possible

public static String sub(CharSequence str, int fromIndex, int toIndex) 
public static String subPreGbk(CharSequence str, int len, CharSequence suffix) 
public static String maxLength(CharSequence string, int length) 
public static String subPre(CharSequence string, int toIndex) 
public static String subSuf(CharSequence string, int fromIndex) 
public static String subSufByLength(CharSequence string, int length) 
public static String subWithLength(String input, int fromIndex, int length) 
public static String subBefore(CharSequence string, CharSequence separator, boolean isLastSeparator) 
public static String subBefore(CharSequence string, char separator, boolean isLastSeparator) 
public static String subAfter(CharSequence string, CharSequence separator, boolean isLastSeparator) 
public static String subAfter(CharSequence string, char separator, boolean isLastSeparator) 
public static String subBetween(CharSequence str, CharSequence before, CharSequence after) 
public static String subBetween(CharSequence str, CharSequence beforeAndAfter)

the case

String str = “abcdefgh”;
String strSub1 = StrUtil.sub(str, 2, 3); //strSub1 -> c
String strSub2 = StrUtil.sub(str, 2, -3); //strSub2 -> cde
String strSub3 = StrUtil.sub(str, 3, 2); //strSub2 -> c

(4) The method related to empty space and carriage return operation

public static boolean isBlank(CharSequence str)
public static boolean isBlankIfStr(Object obj)
public static boolean isNotBlank(CharSequence str)
public static boolean hasBlank(CharSequence… strs)
public static boolean isAllBlank(CharSequence… strs)
public static boolean isEmpty(CharSequence str)
public static boolean isEmptyIfStr(Object obj)
public static boolean isNotEmpty(CharSequence str)
public static String nullToEmpty(CharSequence str)
public static String nullToDefault(CharSequence str, String defaultStr)
public static String emptyToDefault(CharSequence str, String defaultStr)
public static String blankToDefault(CharSequence str, String defaultStr)
public static String emptyToNull(CharSequence str)
public static boolean hasEmpty(CharSequence… strs)
public static boolean isAllEmpty(CharSequence… strs)
public static boolean isNullOrUndefined(CharSequence str)
public static boolean isEmptyOrUndefined(CharSequence str)
public static boolean isBlankOrUndefined(CharSequence str)
public static String cleanBlank(CharSequence str)

// 去空格

StrUtil.cleanBlank

// 去\n\r

StrUtil.removeAllLineBreaks()

(5) String inclusion relationship

the case

// 字符串中: 同时匹配 “小明” 和 “19岁” 这两个字符才返回true
boolean isContain = StrUtil.containsAll(“我叫小明今年18岁职业java软件工程师”, “小明”,19岁”);

Other methods

public static boolean startWith(CharSequence str, char c) 
public static boolean startWith(CharSequence str, CharSequence prefix, boolean isIgnoreCase) 
public static boolean startWith(CharSequence str, CharSequence prefix) 
public static boolean startWithIgnoreCase(CharSequence str, CharSequence prefix) 
public static boolean startWithAny(CharSequence str, CharSequence... prefixes) 
public static boolean endWith(CharSequence str, char c) 
public static boolean endWith(CharSequence str, CharSequence suffix, boolean isIgnoreCase) 
public static boolean endWith(CharSequence str, CharSequence suffix) 
public static boolean endWithIgnoreCase(CharSequence str, CharSequence suffix) 
public static boolean endWithAny(CharSequence str, CharSequence... suffixes) 
public static boolean contains(CharSequence str, char searchChar) 
public static boolean containsAny(CharSequence str, CharSequence... testStrs) 
public static boolean containsAny(CharSequence str, char... testChars) 
public static boolean containsBlank(CharSequence str) 
public static String getContainsStr(CharSequence str, CharSequence... testStrs) 
public static boolean containsIgnoreCase(CharSequence str, CharSequence testStr) 
public static boolean containsAnyIgnoreCase(CharSequence str, CharSequence... testStrs) 
public static String getContainsStrIgnoreCase(CharSequence str, CharSequence... testStrs)

(6) Some processing of the head and tail

public static String trim(CharSequence str) 
public static void trim(String[] strs) 
public static String trimToEmpty(CharSequence str) 
public static String trimToNull(CharSequence str) 
public static String trimStart(CharSequence str) 
public static String trimEnd(CharSequence str) 
public static String trim(CharSequence str, int mode) 
public static String strip(CharSequence str, CharSequence prefixOrSuffix) 
public static String strip(CharSequence str, CharSequence prefix, CharSequence suffix) 
public static String stripIgnoreCase(CharSequence str, CharSequence prefixOrSuffix) 
public static String stripIgnoreCase(CharSequence str, CharSequence prefix, CharSequence suffix) 
public static String addPrefixIfNot(CharSequence str, CharSequence prefix) 
public static String addSuffixIfNot(CharSequence str, CharSequence suffix) 
public static boolean isSurround(CharSequence str, CharSequence prefix, CharSequence suffix) 
public static boolean isSurround(CharSequence str, char prefix, char suffix) 

(7) Delete character operation

public static String removeAll(CharSequence str, CharSequence strToRemove) 
public static String removeAll(CharSequence str, char... chars) 
public static String removeAllLineBreaks(CharSequence str) 
public static String removePreAndLowerFirst(CharSequence str, int preLength) 
public static String removePreAndLowerFirst(CharSequence str, CharSequence prefix) 
public static String removePrefix(CharSequence str, CharSequence prefix) 
public static String removePrefixIgnoreCase(CharSequence str, CharSequence prefix) 
public static String removeSuffix(CharSequence str, CharSequence suffix) 
public static String removeSufAndLowerFirst(CharSequence str, CharSequence suffix) 
public static String removeSuffixIgnoreCase(CharSequence str, CharSequence suffix) 

(8) Case conversion

public static String upperFirstAndAddPre(CharSequence str, String preString) 
public static String upperFirst(CharSequence str) 
public static String lowerFirst(CharSequence str) 
public static boolean isUpperCase(CharSequence str) 
public static boolean isLowerCase(CharSequence str) 

(9) Split operation

public static String[] splitToArray(CharSequence str, char separator) 
public static long[] splitToLong(CharSequence str, char separator) 
public static long[] splitToLong(CharSequence str, CharSequence separator) 
public static int[] splitToInt(CharSequence str, char separator) 
public static int[] splitToInt(CharSequence str, CharSequence separator) 
public static List<String> split(CharSequence str, char separator) 
public static String[] splitToArray(CharSequence str, char separator, int limit) 
public static List<String> split(CharSequence str, char separator, int limit) 
public static List<String> splitTrim(CharSequence str, char separator) 
public static List<String> splitTrim(CharSequence str, CharSequence separator) 
public static List<String> splitTrim(CharSequence str, char separator, int limit) 
public static List<String> splitTrim(CharSequence str, CharSequence separator, int limit) 
public static List<String> split(CharSequence str, char separator, boolean isTrim, boolean ignoreEmpty) 
public static List<String> split(CharSequence str, char separator, int limit, boolean isTrim, boolean ignoreEmpty) 
public static List<String> split(CharSequence str, CharSequence separator, int limit, boolean isTrim, boolean ignoreEmpty) 
public static String[] split(CharSequence str, CharSequence separator) 
public static String[] split(CharSequence str, int len) 
public static String[] cut(CharSequence str, int partLength) 

(10) Judging equality

public static boolean equals(CharSequence str1, CharSequence str2) 
public static boolean equalsIgnoreCase(CharSequence str1, CharSequence str2) 
public static boolean equals(CharSequence str1, CharSequence str2, boolean ignoreCase) 
public static boolean isSubEquals(CharSequence str1, int start1, CharSequence str2, int start2, int length, boolean ignoreCase) 
public static boolean isAllCharMatch(CharSequence value, Matcher<Character> matcher) 
public static boolean equalsCharAt(CharSequence str, int position, char c) 

(11) Occurrence statistics

public static int count(CharSequence content, CharSequence strForSearch) 
public static int count(CharSequence content, char charForSearch)

[2] StringUtils

The operation object of the StringUtils method is an object of type Java.lang.String, which is a supplement to the operation method of String type provided by JDK, and is null-safe (that is, if the input parameter String is null, NullPointerException will not be thrown, but a Corresponding processing, for example, if the input is null, the return is also null, etc. In addition to the constructor, there are more than 130 methods in StringUtils, and they are all static, so we can call StringUtils.xxx() like this

(1) isEmpty: Determine whether a string is empty

public static boolean isEmpty(String str): Determine whether a string is empty, the standard for being empty is str==null or str.length()==0

StringUtils.isEmpty(null) = true
StringUtils.isEmpty("") = true 
StringUtils.isEmpty(" ") = false //注意在 StringUtils 中空格作非空处理
StringUtils.isEmpty("   ") = false
StringUtils.isEmpty("bob") = false
StringUtils.isEmpty(" bob ") = false

(2) isNotEmpty: Determine whether a string is not empty

Determine whether a string is not empty, equal to !isEmpty(String str): public static boolean isNotEmpty(String str)

StringUtils.isNotEmpty(null) = false
StringUtils.isNotEmpty("") = false
StringUtils.isNotEmpty(" ") = true
StringUtils.isNotEmpty("         ") = true
StringUtils.isNotEmpty("bob") = true
StringUtils.isNotEmpty(" bob ") = true

(3) isBlank(String str): Determine whether a string is empty or has a length of 0 or consists of blank characters (whitespace)

public static boolean isBlank(String str)

StringUtils.isBlank(null) = true
StringUtils.isBlank("") = true
StringUtils.isBlank(" ") = true
StringUtils.isBlank("        ") = true
StringUtils.isBlank("\t \n \f \r") = true   //对于制表符、换行符、换页符和回车符
 
StringUtils.isBlank()   //均识为空白符
StringUtils.isBlank("\b") = false   //"\b"为单词边界符
StringUtils.isBlank("bob") = false
StringUtils.isBlank(" bob ") = false

(4)isNotBlank(String str)

Determine whether a string is not empty and the length is not 0 and does not consist of whitespace, equal to !isBlank(String str)

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

(5)trim(String str)

Remove the control characters (control characters, char <= 32) at both ends of the string, and return null if the input is null

StringUtils.trim(null) = null
StringUtils.trim("") = ""
StringUtils.trim(" ") = ""
StringUtils.trim(" \b \t \n \f \r    ") = ""
StringUtils.trim("     \n\tss   \b") = "ss"
StringUtils.trim(" d   d dd     ") = "d   d dd"
StringUtils.trim("dd     ") = "dd"
StringUtils.trim("     dd       ") = "dd"

(6)trimToNull(String str)

Remove the control characters (control characters, char <= 32) at both ends of the string, if it becomes null or "", return null

StringUtils.trimToNull(null) = null
StringUtils.trimToNull("") = null
StringUtils.trimToNull(" ") = null
StringUtils.trimToNull("     \b \t \n \f \r    ") = null
StringUtils.trimToNull("     \n\tss   \b") = "ss"
StringUtils.trimToNull(" d   d dd     ") = "d   d dd"
StringUtils.trimToNull("dd     ") = "dd"
StringUtils.trimToNull("     dd       ") = "dd"

(7) trimToEmpty(String str)

Remove the control characters (control characters, char <= 32) at both ends of the string, if it becomes null or "", return ""

StringUtils.trimToEmpty(null) = ""
StringUtils.trimToEmpty("") = ""
StringUtils.trimToEmpty(" ") = ""
StringUtils.trimToEmpty("     \b \t \n \f \r    ") = ""
StringUtils.trimToEmpty("     \n\tss   \b") = "ss"
StringUtils.trimToEmpty(" d   d dd     ") = "d   d dd"
StringUtils.trimToEmpty("dd     ") = "dd"
StringUtils.trimToEmpty("     dd       ") = "dd"

(8)strip(String str)

Remove the whitespace at both ends of the string, and return null if the input is null
Note the difference with trim()

StringUtils.strip(null) = null
StringUtils.strip("") = ""
StringUtils.strip(" ") = ""
StringUtils.strip("     \b \t \n \f \r    ") = "\b"
StringUtils.strip("     \n\tss   \b") = "ss   \b"
StringUtils.strip(" d   d dd     ") = "d   d dd"
StringUtils.strip("dd     ") = "dd"
StringUtils.strip("     dd       ") = "dd"

(9)stripToNull(String str)

Remove the whitespace at both ends of the string, if it becomes null or "", return null
(note the difference with trimToNull()

StringUtils.stripToNull(null) = null
StringUtils.stripToNull("") = null
StringUtils.stripToNull(" ") = null
StringUtils.stripToNull("     \b \t \n \f \r    ") = "\b"
StringUtils.stripToNull("     \n\tss   \b") = "ss   \b"
StringUtils.stripToNull(" d   d dd     ") = "d   d dd"
StringUtils.stripToNull("dd     ") = "dd"
StringUtils.stripToNull("     dd       ") = "dd"

(10)stripToEmpty(String str)

Remove the whitespace at both ends of the string, if it becomes null or "", return ""
Note the difference with trimToEmpty()

StringUtils.stripToNull(null) = ""
StringUtils.stripToNull("") = ""
StringUtils.stripToNull(" ") = ""
StringUtils.stripToNull("     \b \t \n \f \r    ") = "\b"
StringUtils.stripToNull("     \n\tss   \b") = "ss   \b"
StringUtils.stripToNull(" d   d dd     ") = "d   d dd"
StringUtils.stripToNull("dd     ") = "dd"
StringUtils.stripToNull("     dd       ") = "dd"

(11) Other methods

(1) public static String strip(String str, String stripChars)
removes the characters in stripChars at both ends of str.
If str is null or equal to "", returns itself;
if stripChars is null or "", returns strip(String str).

(2) public static String stripStart(String str, String stripChars)
removes the characters in stripChars at the front of str.

(3) public static String stripEnd(String str, String stripChars)
is similar to 11, remove the characters in stripChars at the end of str.

(4) public static String[] stripAll(String[] strs)
performs strip(String str) on each string in the string array, and then returns.
If strs is null or strs length is 0, returns strs itself

(5) public static String[] stripAll(String[] strs, String stripChars)
performs strip(String str, String stripChars) on each string in the string array, and then returns.
If strs is null or strs length is 0, returns strs itself

(6) public static boolean equals(String str1, String str2)
compares whether two strings are equal, and if both are empty, they are also considered equal.

(7) public static boolean equalsIgnoreCase(String str1, String str2)
compares whether two strings are equal, regardless of case, if both are empty, they are also considered equal.

(8) public static int indexOf(String str, char searchChar)
returns the first occurrence position of the character searchChar in the string str.
Returns -1 if searchChar does not appear in str,
also returns -1 if str is null or ""

(9) public static int indexOf(String str, char searchChar, int startPos)
returns the first occurrence position of the character searchChar starting from startPos in the string str.
Returns -1 if searchChar does not appear in str since startPos,
also returns -1 if str is null or ""

(10) public static int indexOf(String str, String searchStr)
returns the first occurrence position of the string searchStr in the string str.
Returns -1 if str is null or searchStr is null,
returns 0 if searchStr is "" and str is not null,
returns -1 if searchStr is not in str

(11) public static int ordinalIndexOf(String str, String searchStr, int ordinal)
returns the ordinal position of the string searchStr in the string str.
If str=null or searchStr=null or ordinal<=0, return -1
Example (* represents any string):

StringUtils.ordinalIndexOf(null, *, *) = -1
StringUtils.ordinalIndexOf(*, null, *) = -1
StringUtils.ordinalIndexOf("", "", *) = 0
StringUtils.ordinalIndexOf("aabaabaa", "a", 1) = 0
StringUtils.ordinalIndexOf("aabaabaa", "a", 2) = 1
StringUtils.ordinalIndexOf("aabaabaa", "b", 1) = 2
StringUtils.ordinalIndexOf("aabaabaa", "b", 2) = 5
StringUtils.ordinalIndexOf("aabaabaa", "ab", 1) = 1
StringUtils.ordinalIndexOf("aabaabaa", "ab", 2) = 4
StringUtils.ordinalIndexOf("aabaabaa", "bc", 1) = -1
StringUtils.ordinalIndexOf("aabaabaa", "", 1) = 0
StringUtils.ordinalIndexOf("aabaabaa", "", 2) = 0

(12) public static int indexOf(String str, String searchStr, int startPos)
returns the first occurrence position of the string searchStr starting from startPos in the string str.
Example (* represents any string):

StringUtils.indexOf(null, *, *) = -1
StringUtils.indexOf(*, null, *) = -1
StringUtils.indexOf("", "", 0) = 0
StringUtils.indexOf("aabaabaa", "a", 0) = 0
StringUtils.indexOf("aabaabaa", "b", 0) = 2
StringUtils.indexOf("aabaabaa", "ab", 0) = 1
StringUtils.indexOf("aabaabaa", "b", 3) = 5
StringUtils.indexOf("aabaabaa", "b", 9) = -1
StringUtils.indexOf("aabaabaa", "b", -1) = 2
StringUtils.indexOf("aabaabaa", "", 2) = 2
StringUtils.indexOf("abc", "", 9) = 3

(13) The basic principle of public static int lastIndexOf(String str, char searchChar)
is the same as 18

(14) public static int lastIndexOf(String str, char searchChar, int startPos)
Basic principle same as 19

(15) public static int lastIndexOf(String str, String searchStr)
Basic principle same as 20

(16)public static int lastIndexOf(String str, String searchStr, int startPos)

[3] Collection Utils

(1) Judging whether the set is empty or not

(1) Determine whether the collection is empty

CollectionUtils.isEmpty(null): true
CollectionUtils.isEmpty(new ArrayList()): true  
CollectionUtils.isEmpty({
    
    a,b}): false

(2) Determine whether the collection is not empty

CollectionUtils.isNotEmpty(null): false
CollectionUtils.isNotEmpty(new ArrayList()): false
CollectionUtils.isNotEmpty({
    
    a,b}): true

(2) Set union

@Test
public void testUnion(){
    
    
    String[] arrayA = new String[] {
    
     "A", "B", "C", "D", "E", "F" };  
    String[] arrayB = new String[] {
    
     "B", "D", "F", "G", "H", "K" };
    List<String> listA = Arrays.asList(arrayA);
    List<String> listB = Arrays.asList(arrayB);
    //2个数组取并集 
    System.out.println(ArrayUtils.toString(CollectionUtils.union(listA, listB)));
    //[A, B, C, D, E, F, G, H, K]
}

(3) Set intersection

@Test
public void testIntersection(){
    
    
    String[] arrayA = new String[] {
    
     "A", "B", "C", "D", "E", "F" };  
    String[] arrayB = new String[] {
    
     "B", "D", "F", "G", "H", "K" };
    List<String> listA = Arrays.asList(arrayA);
    List<String> listB = Arrays.asList(arrayB);
    //2个数组取交集 
    System.out.println(ArrayUtils.toString(CollectionUtils.intersection(listA, listB)));
    //[B, D, F]
 
}

(4) Complement of intersection (disjunction)

@Test
public void testDisjunction(){
    
    
    String[] arrayA = new String[] {
    
     "A", "B", "C", "D", "E", "F" };  
    String[] arrayB = new String[] {
    
     "B", "D", "F", "G", "H", "K" };
    List<String> listA = Arrays.asList(arrayA);
    List<String> listB = Arrays.asList(arrayB);
    //2个数组取交集 的补集
    System.out.println(ArrayUtils.toString(CollectionUtils.disjunction(listA, listB)));
    //[A, C, E, G, H, K]
}

(5) Difference (deduction)

@Test
public void testSubtract(){
    
    
    String[] arrayA = new String[] {
    
     "A", "B", "C", "D", "E", "F" };  
    String[] arrayB = new String[] {
    
     "B", "D", "F", "G", "H", "K" };
    List<String> listA = Arrays.asList(arrayA);
    List<String> listB = Arrays.asList(arrayB);
    //arrayA扣除arrayB
    System.out.println(ArrayUtils.toString(CollectionUtils.subtract(listA, listB)));
    //[A, C, E]
 
}

(6) Whether the collection is empty

@Test
public void testIsEmpty(){
    
    
 
    class Person{
    
    }
    class Girl extends Person{
    
    }
 
    List<Integer> first = new ArrayList<>();
    List<Integer> second = null;
    List<Person> boy = new ArrayList<>();
    //每个男孩心里都装着一个女孩
    boy.add(new Girl());
    //判断集合是否为空
    System.out.println(CollectionUtils.isEmpty(first));   //true
    System.out.println(CollectionUtils.isEmpty(second));   //true
    System.out.println(CollectionUtils.isEmpty(boy));   //false
 
    //判断集合是否不为空
    System.out.println(CollectionUtils.isNotEmpty(first));   //false
    System.out.println(CollectionUtils.isNotEmpty(second));   //false
    System.out.println(CollectionUtils.isNotEmpty(boy));   //true
}

(7) Whether the sets are equal

@Test
public void testIsEqual(){
    
    
 
    class Person{
    
    }
    class Girl extends Person{
    
    
    }
 
    List<Integer> first = new ArrayList<>();
    List<Integer> second = new ArrayList<>();
    first.add(1);
    first.add(2);
    second.add(2);
    second.add(1);
    Girl goldGirl = new Girl();
    List<Person> boy1 = new ArrayList<>();
    //每个男孩心里都装着一个女孩
    boy1.add(new Girl());
    List<Person> boy2 = new ArrayList<>();
    //每个男孩心里都装着一个女孩
    boy2.add(new Girl());
    //比较两集合值
    System.out.println(CollectionUtils.isEqualCollection(first,second));   //true
    System.out.println(CollectionUtils.isEqualCollection(first,boy1));   //false
    System.out.println(CollectionUtils.isEqualCollection(boy1,boy2));   //false
 
    List<Person> boy3 = new ArrayList<>();
    //每个男孩心里都装着一个女孩
    boy3.add(goldGirl);
    List<Person> boy4 = new ArrayList<>();
    boy4.add(goldGirl);
    System.out.println(CollectionUtils.isEqualCollection(boy3,boy4));   //true
}

(8) Unmodifiable collections

We operate on c, and s also obtains the same content as c, so that other people can be prevented from modifying the s object. Sometimes it needs to be protected to prevent the returned result from being modified by others.

@Test
public void testUnmodifiableCollection(){
    
    
    Collection<String> c = new ArrayList<>();
    Collection<String> s = CollectionUtils.unmodifiableCollection(c);
    c.add("boy");
    c.add("love");
    c.add("girl");
    //! s.add("have a error");
    System.out.println(s);
}

Collections.unmodifiableCollection can get a mirror image of a collection, and its return result cannot be changed directly, otherwise an error will be prompted

java.lang.UnsupportedOperationException
at org.apache.commons.collections.collection.UnmodifiableCollection.add(UnmodifiableCollection.java:75)

【4】ObjectUtil

【五】ObjectUtils

[6] FileUtil

【Seven】DateUtil

[8] Commonly used case analysis

[1] Method for judging whether a string is empty

(1) 4 states of String

1- Declare and do not reference the value to null
2- Allocated the memory space and assign it to ""
3- Allocated the memory space without assigning a value (default '''') String A=new String();
4- Only declared but not referenced String B;
5-has a value

(2) Two states of String empty string

1-null
2-“”

(3) 4 ways to compare the string is empty

(1) if(s == null || s.isEmpty()): method provided after Java SE 6.0
(2) if(s == null || s.length() <= 0): compare strings Length, highest efficiency
(3) if (s == null || s == ""): relatively intuitive and simple method
(4) if (s == null || "".equals(s)): very efficient Low (== reference data type compares the value equals compares the address but the string class rewrites the equals method to compare the value)

(4) StringUtils comparison

It is recommended to use common.lang3

(1) One is under the org.apache.commons.lang package; it can only compare whether the length of the string is 0

public boolean isEmpty() {
    
    
    return value.length == 0;
}

(2) One is under the org.springframework.util package: the parameter is the Object class, which can compare any type

public static boolean isEmpty(Object str){
    
    
    return (str == null || "".equals(str));
}

So we generally use stringutil under the springframework package more conveniently or directly if(s == null || s.isEmpty()) is more rigorous

(3) Use isBlank or isEmpty? Use isBlank, even if there is a space, it is also empty.
It is recommended to use isBlank() of common.lang3

//推荐使用(如果字符中是空的,也是true)  
System.out.println(StringUtils.isBlank(" "));//true
//字符中包含空格,它认为该字符串不为空
System.out.println(StringUtils.isEmpty(" "));//false

(5) StrUtils comparison

[2] Determine whether the object is empty

[3] Determine whether the List collection and Map collection are empty

Guess you like

Origin blog.csdn.net/weixin_44823875/article/details/130927552