StringUtils had a lot of methods that I could use, so I've organized the ones that I found useful.
https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html
It is a method that summarizes various convenient operations for string type string editing. Unlike the String method, it takes care not to cause nullpo. No need to check for null. Note that it is similar to StringUtil
isEmpty is also a String, but it allows nulls and returns false. isNotEmpty is the opposite of isEmpty.
isEmpty.isNotEmpty
String a = "1234";
String b = "";
String c = null;
try {
System.out.println(StringUtils.isEmpty(a));
System.out.println(StringUtils.isNotEmpty(a));
System.out.println(a.isEmpty());
System.out.println(StringUtils.isEmpty(b));
System.out.println(StringUtils.isNotEmpty(b));
System.out.println(b.isEmpty());
System.out.println(StringUtils.isEmpty(c));
System.out.println(StringUtils.isNotEmpty(c));
System.out.println(c.isEmpty());
}catch (Exception e) {
System.out.println("Nullpo");
}
result.
false
true
false
true
false
true
true
false
Nullpo
A method that checks for whitespace and null that String does not have. If all the variables are not blank, the blank judgment is not made.
isBlank.isNotBlank
String a = "1234";
String b = "12 4";
String c = " ";
String d = " ";
String e = "";
String f = null;
System.out.println(StringUtils.isBlank(a));
System.out.println(StringUtils.isNotBlank(a));
System.out.println(StringUtils.isBlank(b));
System.out.println(StringUtils.isNotBlank(b));
System.out.println(StringUtils.isBlank(c));
System.out.println(StringUtils.isNotBlank(c));
System.out.println(StringUtils.isBlank(d));
System.out.println(StringUtils.isNotBlank(d));
System.out.println(StringUtils.isBlank(e));
System.out.println(StringUtils.isNotBlank(e));
System.out.println(StringUtils.isBlank(f));
System.out.println(StringUtils.isNotBlank(f));
result.
false
true
false
true
true
false
true
false
true
false
true
false
isAllLowerCase Make sure it is in lowercase only. Whitespace, empty string, null is false
isAllLowerCase.
String a = "a";
String b = "A";
String c = "Aa";
String d = "Ah";
String e = "1";
String f = " ";
String h = null;
System.out.println(StringUtils.isAllLowerCase(a));
System.out.println(StringUtils.isAllLowerCase(b));
System.out.println(StringUtils.isAllLowerCase(c));
System.out.println(StringUtils.isAllLowerCase(d));
System.out.println(StringUtils.isAllLowerCase(e));
System.out.println(StringUtils.isAllLowerCase(f));
System.out.println(StringUtils.isAllLowerCase(h));
true
false
false
false
false
false
false
isAllUpperCase Make sure it is in uppercase only. Whitespace, empty string, null is false
isAllUpperCase.
String a = "a";
String b = "A";
String c = "Aa";
String d = "Ah";
String e = "1";
String f = " ";
String h = null;
System.out.println(StringUtils.isAllUpperCase(a));
System.out.println(StringUtils.isAllUpperCase(b));
System.out.println(StringUtils.isAllUpperCase(c));
System.out.println(StringUtils.isAllUpperCase(d));
System.out.println(StringUtils.isAllUpperCase(e));
System.out.println(StringUtils.isAllUpperCase(f));
System.out.println(StringUtils.isAllUpperCase(h));
result.
false
true
false
false
false
false
false
isAlpha checks if it is a character. Numbers, spaces, empty strings, null are false isAlphaSpace is a blank-tolerant version of isAlpha
isAlpha.isAlphaSpace
String a = "a";
String b = "A";
String c = "1";
String d = "1aB";
String e = "Ah";
String f = " ";
String h = null;
System.out.println(StringUtils.isAlpha(a));
System.out.println(StringUtils.isAlpha(b));
System.out.println(StringUtils.isAlpha(c));
System.out.println(StringUtils.isAlpha(d));
System.out.println(StringUtils.isAlpha(e));
System.out.println(StringUtils.isAlpha(f));
System.out.println(StringUtils.isAlpha(h));
result.
true
true
false
false
true
false
false
isNumeric checks if it is a number. Numbers, spaces, empty strings, null are false isNumericSpace is a blank-tolerant version of isNumeric
isNumeric.isNumericSpace
String a = "a";
String b = "A";
String c = "1";
String d = "1aB";
String e = "Ah";
String f = " ";
String h = null;
System.out.println(StringUtils.isNumeric(a));
System.out.println(StringUtils.isNumeric(b));
System.out.println(StringUtils.isNumeric(c));
System.out.println(StringUtils.isNumeric(d));
System.out.println(StringUtils.isNumeric(e));
System.out.println(StringUtils.isNumeric(f));
System.out.println(StringUtils.isNumeric(h));
result.
false
false
true
false
false
false
false
isAlphanumeric = isAlpha + isNumeric isAlphanumericSpace = isAlphaSpace + isNumericSpace
isAlphanumeric.isAlphanumericSpace
String a = "a";
String b = "A";
String c = "1";
String d = "1aB";
String e = "Ah";
String f = " ";
String h = null;
System.out.println(StringUtils.isAlphanumeric(a));
System.out.println(StringUtils.isAlphanumericSpace(a));
System.out.println(StringUtils.isAlphanumeric(b));
System.out.println(StringUtils.isAlphanumericSpace(b));
System.out.println(StringUtils.isAlphanumeric(c));
System.out.println(StringUtils.isAlphanumericSpace(c));
System.out.println(StringUtils.isAlphanumeric(d));
System.out.println(StringUtils.isAlphanumericSpace(d));
System.out.println(StringUtils.isAlphanumeric(e));
System.out.println(StringUtils.isAlphanumericSpace(e));
System.out.println(StringUtils.isAlphanumeric(f));
System.out.println(StringUtils.isAlphanumericSpace(f));
System.out.println(StringUtils.isAlphanumeric(h));
System.out.println(StringUtils.isAlphanumericSpace(h));
result.
true
true
true
true
true
true
true
true
true
true
false
true
false
false
Check if the second argument is included in the first argument. Null allowed. containsNone is the opposite of contains
contains.containsNone
String tar = "abcdef";
String a = "a";
String b = "B";
String c = "abc";
String d = "abC";
String e = "";
String f = null;
try {
System.out.println(StringUtils.contains(tar, a));
System.out.println(StringUtils.containsNone(tar, a));
System.out.println(tar.contains(a));
System.out.println(StringUtils.contains(tar, b));
System.out.println(StringUtils.containsNone(tar, b));
System.out.println(tar.contains(b));
System.out.println(StringUtils.contains(tar, c));
System.out.println(StringUtils.containsNone(tar, c));
System.out.println(tar.contains(c));
System.out.println(StringUtils.contains(tar, d));
System.out.println(StringUtils.containsNone(tar, d));
System.out.println(tar.contains(d));
System.out.println(StringUtils.contains(tar, e));
System.out.println(StringUtils.containsNone(tar, e));
System.out.println(tar.contains(e));
System.out.println(StringUtils.contains(tar, f));
System.out.println(StringUtils.containsNone(tar, f));
System.out.println(tar.contains(f));
} catch (Exception ex) {
System.out.println("Nullpo");
}
true
false
true
false
true
false
true
false
true
false
false
false
true
true
true
false
true
Nullpo
equals A null-checked version of the String equals method. Either can be used as null. However, even if both are null, it will be false.
equals.
String a = "a";
String b = "abc";
String c = " ";
String d = "";
String e = null;
try {
System.out.println(StringUtils.equals(a, a));
System.out.println(a.equals(a));
System.out.println(StringUtils.equals(a, b));
System.out.println(a.equals(b));
System.out.println(StringUtils.equals(a, c));
System.out.println(a.equals(c));
System.out.println(StringUtils.equals(a, d));
System.out.println(a.equals(d));
System.out.println(StringUtils.equals(a, e));
System.out.println(a.equals(e));
System.out.println(StringUtils.equals(c, c));
System.out.println(c.equals(c));
System.out.println(StringUtils.equals(c, d));
System.out.println(c.equals(d));
System.out.println(StringUtils.equals(d, d));
System.out.println(d.equals(d));
System.out.println(StringUtils.equals(d, e));
System.out.println(e.equals(e));
System.out.println(StringUtils.equals(e, e));
System.out.println(StringUtils.equals(e, a));
System.out.println(e.equals(e));
} catch (Exception ex) {
System.out.println("Nullpo");
}
result.
true
true
false
false
false
false
false
false
false
false
true
true
false
false
true
true
false
Nullpo
join It arranges a list of character strings separated by (comma) to make one character string. The join of the String object will be dropped by nullpo if the list object is null.
join.
List<String> strList = new ArrayList<>();
List<String> nullList = null;
try {
for (int i = 0; i < 10; i++) {
strList.add(Integer.toString(i));
}
strList.add(" ");
strList.add("");
strList.add(null);
System.out.println(StringUtils.join(strList));
System.out.println(StringUtils.join(nullList));
System.out.println(String.join(",", nullList));
} catch (Exception e) {
System.out.println("Nullpo");
}
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, , , null]
Nullpo
leftPad、rightPad It fills the left and right sides of the character string with spaces so that the specified number of characters is reached. You can also specify the characters to fill in instead of the blank plum.
leftPad.rightPad
String a = "a";
System.out.println(StringUtils.leftPad(a, 5));
System.out.println(StringUtils.leftPad(a, 5, "0"));
System.out.println(StringUtils.rightPad(a, 5));
System.out.println(StringUtils.rightPad(a, 5, "0"));
result.
a
0000a
a
a0000
repeat It creates a character string that repeats the specified character string a specified number of times. If it is null, it will not be repeated.
repeat.
String a = "abc";
String b = " ";
String c = "";
String d = null;
System.out.println(StringUtils.repeat(a, 5));
System.out.println(StringUtils.repeat(b, 5));
System.out.println(StringUtils.repeat(c, 5));
System.out.println(StringUtils.repeat(d, 5));
result.
abcabcabcabcabc
null
swapCase Automatically convert uppercase letters to lowercase letters and lowercase letters to uppercase letters!
swapCase.
String a = "abcdefgh";
String b = "ABCDEFGH";
String c = "aBcDeFgH";
System.out.println(StringUtils.swapCase(a));
System.out.println(StringUtils.swapCase(b));
System.out.println(StringUtils.swapCase(c));
result.
ABCDEFGH
abcdefgh
AbCdEfGh
defaultString If the target character string is null, it will be changed to an empty string. Useful when you don't want to display null.
defaultString.
String a = "abc";
String b = " ";
String c = "";
String d = null;
System.out.println(StringUtils.defaultString(a));
System.out.println(StringUtils.defaultString(b));
System.out.println(StringUtils.defaultString(c));
System.out.println(StringUtils.defaultString(d));
result.
abc
Recommended Posts