Organize methods that can be used with StringUtils

StringUtils had a lot of methods that I could use, so I've organized the ones that I found useful.

What is StringUtils?

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 and isNotEmpty

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

isBlank and isNotBlank

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 and isAlphaSpace

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 and isNumericSpace

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 and isAlphanumericSpace

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

contains and contains None

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

Organize methods that can be used with StringUtils
[Ruby] Methods that can be used with strings
Ruby array methods that can be used with Rails (other than each)
Summary of css selectors that can be used with Nookogiri
Create a page control that can be used with RecyclerView
Firebase-Realtime Database on Android that can be used with copy
SwiftUI View that can be used in combination with other frameworks
Learning Ruby with AtCoder Beginners Selection [Some Sums] Increase the methods that can be used
Simple slot machine implementation that can be used with copy and paste
Performance analysis and failure diagnostic tools that can be used with OpenJDK
Range where variables can be used with ruby [Scope]
About the matter that hidden_field can be used insanely
Convenient shortcut keys that can be used in Eclipse
Four-in-a-row with gravity that can be played on the console
Syntax and exception occurrence conditions that can be used when comparing with null in Java
Static analysis tool that can be used on GitHub [Java version]
Build an environment where pip3 can be used with CentOS7 + Python3
Summary of ORM "uroboroSQL" that can be used in enterprise Java
File form status check sheet that can be deleted with thumbnails
I made a question that can be used for a technical interview
Power skills that can be used quickly at any time --Reflection
Summary of JDK that can be installed with Homebrew (as of November 2019)
Introduction to Java that can be understood even with Krillin (Part 1)
Until ruby can be used on windows ...
Set the access load that can be changed graphically with JMeter (Part 2)
Set the access load that can be changed graphically with JMeter (Part 1)
Initial settings until S2Dao can be used
Object-oriented that can be understood by fairies
[ERROR message display] A simplified version that can be used at any time with the rails partial template.
[Swift] Color Picker that can be used with copy and paste (palette that allows you to freely select colors)
[Android] I want to create a ViewPager that can be used for tutorials
Technology excerpt that can be used for creating EC sites in Java training
I made a THETA API client that can be used for plug-in development
[Rails 6] method :: delete cannot be used with link_to
A ruby ​​script that creates an rsa private key that can be used with OpenSSL from any two prime numbers
[Book Review] Unit testing of programming sites that can be done with zero experience
The story that the port can no longer be used in the Spring boot sample program
Compiled kotlin with cli with docker and created an environment that can be executed with java
Please note that Spring Boot + Tomcat 8.5.8 cannot be used!
Basic functional interface that can be understood in 3 minutes
Write a class that can be ordered in Java
Play with Java function nodes that can use Java with Node-RED
Scala String can be used other than java.lang.String method
Private methods can or cannot be overridden by public methods
Ruby on Rails 5 quick learning practice guide that can be used in the field Summary
[Java 8] Sorting method in alphabetical order and string length order that can be used in coding tests
The world of Azure IoT that can be played on the DE10-Nano board: Ajuchika with FPGA !!?