Hello everyone, today I will share about the tip of checking String null or full space conveniently
I will use the library StringUtils class to download the jar file to import into the library at ( link )
If you use maven project, it’s easier, just import in pom.xml file
1 2 3 4 5 6 7 | <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.7</version> </dependency> |
If you want to check if the String is null / full space / empty, then manually check will be like this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | package test; import org.apache.commons.lang3.StringUtils; public class Main { public static void main(String[] args) { String a = null; String b = ""; String c = " "; System.out.println("manual result: " + "a: " + validateStringManual(a) + " b: " + validateStringManual(b) + " c: " + validateStringManual(c)); } private static Boolean validateStringManual(String str) { if (length(str) == 0 || isFullspace(str)) { return true; } return false; } public static int length(CharSequence cs) { return cs == null ? 0 : cs.length(); } public static Boolean isFullspace(CharSequence cs) { int strLen = length(cs); for (int i = 0; i < strLen; ++i) { if (!Character.isWhitespace(cs.charAt(i))) { return false; } } return true; } } |
Output
1 2 | manual result: a: true b: true c: true |
Very long and messy, right?
Now if we use the library, we just need to do this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | package test; import org.apache.commons.lang3.StringUtils; public class Main { public static void main(String[] args) { String a = null; String b = ""; String c = " "; System.out.println("StringUtils result: " + "a: " + StringUtils.isBlank(a) + " b: " + StringUtils.isBlank(a) + " c: " + StringUtils.isBlank(c)); } } |
Output
1 2 | StringUtils result: a: true b: true c: true |
Still the same, very convenient, right?
If you do not want to check the fullspace, use the following method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | package test; import org.apache.commons.lang3.StringUtils; public class Main { public static void main(String[] args) { String a = null; String b = ""; String c = " "; System.out.println("StringUtils result: " + "a: " + StringUtils.isEmpty(a) + " b: " + StringUtils.isEmpty(a) + " c: " + StringUtils.isEmpty(c)); } } |
Output
1 2 | StringUtils result: a: true b: true c: false |
StringUtils has many other libraries that you can check and use for your purposes. For example, isNotBlank (), isNotEmpty () …
Now let’s move on to validate Collections in java
Maven project, then add this paragraph to pom.xml file:
1 2 3 4 5 6 7 8 | <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-collections4 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-collections4</artifactId> <version>4.1</version> </dependency> |
And java projects usually import jar file to download at the link
An example of checking whether that Collection is empty or null
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | package test; import org.apache.commons.collections4.CollectionUtils; import java.util.ArrayList; import java.util.List; public class Main { public static void main(String[] args) { List<String> list1 = null; List<String> list2 = new ArrayList<>(); List<String> list3 = new ArrayList<>(); list3.add("xxx"); System.out.println("result: " + "list1: " + CollectionUtils.isEmpty(list1)); System.out.println("result: " + "list2: " + CollectionUtils.isEmpty(list2)); System.out.println("result: " + "list3: " + CollectionUtils.isEmpty(list3)); } } |
Output
1 2 3 4 | result: list1: true result: list2: true result: list3: false |
This is just one of the functions of the CollectionUtils class, depending on the purpose of the use you get to use.
Thank you for reading all of your post, if something went wrong, please comment for me to correct it
My Fanpage: https://www.facebook.com/morethanacoder
His utube : thanglaptrinh