Common notes

Introducing Commons lang3 dependency

Official website The dependency import mode of maven is given

<dependency>
  <groupId>org.apache.commons</groupId>
  <artifactId>commons-lang3</artifactId>
  <version>3.8.1</version>
</dependency>
Correspondingly, if you use gradle to manage projects, you can add:
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.8.1'

 

lang3 library RandomUtils random number utility class

RandomUtils class full path:

org.apache.commons.lang3.RandomUtils

Generate random boolean value

println( RandomUtils.nextBoolean() );

Generate random int number

The range of random number is [0, integer.max_value].

println( RandomUtils.nextInt() );

Generate random int numbers in the specified range

For example, generate a random int number in the range of [1, 100]:

println( RandomUtils.nextInt(1, 100) );

Generate random long number

The range of random number is [0, long.max value].

println( RandomUtils.nextLong() );

Generate random long numbers in the specified range

For example, generate a random long number in the range of [1, 100]:

println( RandomUtils.nextLong(1, 100) );

Generate random float number

The range of random number is [0, Float.MAX_VALUE].

println( RandomUtils.nextFloat() );

Generate random float numbers for the specified range

For example, generate the number of random float s in the range of [1, 100]:

println( RandomUtils.nextFloat(1, 100) );

Generate random double number

The range of random number is [0, Double.MAX_VALUE].

println( RandomUtils.nextDouble() );

Generate a random number of double s in the specified range

For example, generate the number of random double in the range of [1, 100]:

println( RandomUtils.nextDouble(1, 100) );

 

lang3 library RandomStringUtils random string utility class

RandomStringUtils class full path:

org.apache.commons.lang3.RandomStringUtils

Generate random strings with all supported characters

For example, generate a random string of length 10:

println( RandomStringUtils.random(10) );
Operation result:
Lock and knead

Generating random strings with English uppercase and lowercase letters

For example, generate a random string of length 10:

println( RandomStringUtils.randomAlphabetic(10) );

Operation result:

PHPAWLxpZD

Generate random string with English case letters + numbers

For example, generate a random string of length 20:

println( RandomStringUtils.randomAlphanumeric(20) );

Operation result:

mvmcgMmMd3T43rfyJyur

Generating random strings with numbers

For example, generate a random string of length 20:

println( RandomStringUtils.randomNumeric(20) );

Operation result:

5275393600

 

lang3 library SerializationUtils serialization tool class

SerializationUtils class full path:

org.apache.commons.lang3.SerializationUtils

clone method

This method is used to deeply clone a serializable object.

Let's define a Person class:

class Person implements Serializable {
    public String name;
    public int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
}

Use example:

Person p1 = new Person("letiantian", 18);
Person p2 = SerializationUtils.clone(p1);

println(p1 == p2);              // false
println(p1.name == p2.name);    // false
println(p2.name);               // "letiantian"

 

 

lang3 library StringUtils string utility class

Full path of StringUtils class:

import org.apache.commons.lang3.StringUtils;
There are many practical static methods for handling strings under the StringUtils class.

 

Method name describe
contains Determine if another string is included in the string
startsWith Determine whether one string begins with another
endsWith Determine whether one string ends with another
isBlank Determine whether the string is composed of blank characters
isEmpty Judge whether the string is empty
isNumeric Determine whether a string is made up of numbers only
isAlpha Determine whether a string is composed of only letters
join Connection string
leftPad Fill characters to the left of the string
rightPad Fill characters to the right of a string
center Fill in the content on both sides of the specified string, and put the specified string in the middle
repeat Repeat concatenation string
reverse Flip string
split** Split string
trim Remove whitespace on both sides of the string
strip Remove whitespace on both sides of the string
truncate Truncation string

lang3 library StringUtils class isEmpty method

Full path of StringUtils class:

import org.apache.commons.lang3.StringUtils;

isEmpty null

For isEmpty, returns true if the string is null or ''. Otherwise, false is returned.

println( StringUtils.isEmpty(null) );   // true
println( StringUtils.isEmpty("") );     // true
println( StringUtils.isEmpty(" ") );    // false
println( StringUtils.isEmpty(" \n") );  // false
println( StringUtils.isEmpty(" a ") );  // false

isNotEmpty non empty

In contrast to isEmpty, isNotEmpty is used to determine whether the string is not empty. null and 0-length strings are empty. Strings with blank characters are not empty.

println( StringUtils.isNotEmpty(null) );   // false
println( StringUtils.isNotEmpty("") );     // false
println( StringUtils.isNotEmpty(" ") );    // true
println( StringUtils.isNotEmpty(" \n") );  // true
println( StringUtils.isNotEmpty(" a ") );  // true

isAllEmpty determines whether multiple strings are empty

println( StringUtils.isAllEmpty("", null) );                        // true
println( StringUtils.isAllEmpty( new String[]{"", null}) );         // true
println( StringUtils.isAllEmpty( "abc", null) );                    // false
println( StringUtils.isAllEmpty( new String[]{"abc", null}) );      // false
println( StringUtils.isAllEmpty( "abc", "xyz") );                   // false
println( StringUtils.isAllEmpty( new String[]{"abc", "xyz"}) );     // false

isAnyEmpty determines whether at least one of the strings is empty

println( StringUtils.isAnyEmpty("", null) );                    // true
println( StringUtils.isAnyEmpty( new String[]{"", null}) );     // true
println( StringUtils.isAnyEmpty( "abc", null) );                // true
println( StringUtils.isAnyEmpty( new String[]{"abc", null}) );  // true
println( StringUtils.isAnyEmpty( "abc", "xyz") );               // false
println( StringUtils.isAnyEmpty( new String[]{"abc", "xyz"}) ); // false

isNoneEmpty determines whether multiple strings are not empty

Opposite to isAnyEmpty.

println( StringUtils.isNoneEmpty("", null) );                       // false
println( StringUtils.isNoneEmpty( new String[]{"", null}) );        // false
println( StringUtils.isNoneEmpty( "abc", null) );                   // false
println( StringUtils.isNoneEmpty( new String[]{"abc", null}) );     // false
println( StringUtils.isNoneEmpty( "abc", "xyz") );                  // true
println( StringUtils.isNoneEmpty( new String[]{"abc", "xyz"}) );    // true

 

lang3 library StringUtils class isBlank method

Full path of StringUtils class:

import org.apache.commons.lang3.StringUtils;

isBlank

isBlank is used to determine whether the string is blank. Returns true if the string is null, an empty string (""), or is made up of blank characters, otherwise false. What are white space characters? java.lang.Character class isWhitespace static method determines that the character that is true is a blank character.

println( StringUtils.isBlank(null) );       // true
println( StringUtils.isBlank("") );         // true
println( StringUtils.isBlank(" ") );        // true
println( StringUtils.isBlank(" \n") );      // true
println( StringUtils.isBlank(" \n\t\r") );  // true
println( StringUtils.isBlank(" a ") );      // false

isNotBlank

The opposite of isBlank.

println( StringUtils.isNotBlank(null) );   // false
println( StringUtils.isNotBlank("") );     // false
println( StringUtils.isNotBlank(" ") );    // false
println( StringUtils.isNotBlank(" \n") );  // false
println( StringUtils.isNotBlank(" a ") );  // true

isAllBlank determines whether multiple strings are blank

println( StringUtils.isAllBlank("", null) );                // true
println( StringUtils.isAllBlank("", "\n") );                // true
println( StringUtils.isAllBlank(new String[] {"", "\n"}) );   // true
println( StringUtils.isAllBlank("abc") );                   // false
println( StringUtils.isAllBlank("abc", "") );               // false

isAnyBlank determines whether at least one string is blank

println( StringUtils.isAnyBlank(null) );                // Special attention shall be paid to false, which is not in line with the expectation
println( StringUtils.isAnyBlank(null, null) );          // true
println( StringUtils.isAnyBlank("", null) );            // true
println( StringUtils.isAnyBlank("", "\n") );            // true
println( StringUtils.isAnyBlank(new String[] {"", "\n"}) );   // true
println( StringUtils.isAnyBlank("abc") );               // false
println( StringUtils.isAnyBlank("abc", "") );           // true
println( StringUtils.isAnyBlank("abc", "", "xy") );     // true

isNoneBlank determines whether multiple strings are blank

println( StringUtils.isNoneBlank(null) );                // Pay special attention to "true -" this does not meet the expectation
println( StringUtils.isNoneBlank(null, null) );          // false
println( StringUtils.isNoneBlank("", null) );            // false
println( StringUtils.isNoneBlank("", "\n") );            // false
println( StringUtils.isNoneBlank(new String[] {"", "\n"}) );   // false
println( StringUtils.isNoneBlank("abc") );               // true
println( StringUtils.isNoneBlank("abc", "") );           // false
println( StringUtils.isNoneBlank("abc", "xy") );         // true

lang3 library StringUtils class isNumeric, isAlpha

Full path of StringUtils class:

import org.apache.commons.lang3.StringUtils;

isNumeric determines whether a string is composed of only numbers

println( StringUtils.isNumeric("123") );    // true
println( StringUtils.isNumeric("123.") );   // false
println( StringUtils.isNumeric(" 123.") );  // false
println( StringUtils.isNumeric(null) );     // false

isNumericSpace

Determines whether a string consists only of numbers or white space characters.

isAlpha

Determines whether a string consists of only letters.

isAlphaSpace

Determines whether a string consists only of numbers or white space characters.

isAlphanumeric

Determines whether a string consists of only letters or numbers.

isAlphanumericSpace

Determine whether the string is composed of letters, numbers and blank characters.

isWhitespace

Determines whether a string consists only of white space characters.

isAllUpperCase

Determines whether a string consists of only uppercase letters.

isAllLowerCase

Determines whether a string consists of only lowercase letters.

 

lang3 library StringUtils class contains method

Full path of StringUtils class:

import org.apache.commons.lang3.StringUtils;
The contains method supports two strings as parameters to determine whether there is the content of the second string in the first string.
println( StringUtils.contains(null, null) );    // false
println( StringUtils.contains(null, "") );      // false
println( StringUtils.contains("abc", null) );   // false
println( StringUtils.contains("abc", "d") );    // false

println( StringUtils.contains("", "") );        // true
println( StringUtils.contains("abc", "") );     // true
println( StringUtils.contains("abc", "ab") );   // true

lang3 library StringUtils class startsWith method

 

Full path of StringUtils class:

import org.apache.commons.lang3.StringUtils;
The startsWith method determines whether a string begins with another string.
println( StringUtils.startsWith(null, null) );      // true
println( StringUtils.startsWith("abc", "a") );      // true
println( StringUtils.startsWith("abc", "abc") );    // true
println( StringUtils.startsWith("abc", "abcd") );   // false
println( StringUtils.startsWith("abc", "bc") );     // false
println( StringUtils.startsWith("abc", null) );     // false
println( StringUtils.startsWith("abc", "A") );      // false

Another way to ignore case is to start swith ignorecase.

lang3 library StringUtils class endsWith method

Full path of StringUtils class:

import org.apache.commons.lang3.StringUtils;
The endsWith method is used to determine whether a string ends in another string.
println( StringUtils.endsWith(null, null) );      // true
println( StringUtils.endsWith("abc", "c") );      // true
println( StringUtils.endsWith("abc", "abc") );    // true
println( StringUtils.endsWith("abc", "abcd") );   // false
println( StringUtils.endsWith("abc", "ab") );     // false
println( StringUtils.endsWith("abc", null) );     // false
println( StringUtils.endsWith("abc", "C") );      // false

Another way to ignore case: endswith ignorecase.

lang3 library StringUtils class join method

 

Full path of StringUtils class:

import org.apache.commons.lang3.StringUtils;
The join method is used to connect strings.
  • Parameter 1: array or List, multiple strings to be connected together (or can be converted into strings)
  • Parameter 2: what string is used to connect multiple strings in parameter 1.
  • Return type: String
println( StringUtils.join(new String[]{}, "---") );                 // ""
println( StringUtils.join(new String[]{"foo"}, "---") );            // "foo"
println( StringUtils.join(new String[]{"foo", "bar"}, "---") );     // "foo---bar"
println( StringUtils.join(new Object[]{"foo", "bar", 2}, ",") );    // "foo,bar,2"
println( StringUtils.join(new Integer[]{1, 2, 3}, ",") );           // "1,2,3"
println( StringUtils.join(Arrays.asList(1, 2, 3, 4), ",") );        // "1,2,3,4"
// The following array of primitive types is not supported
println( StringUtils.join(new int[]{1, 2, 3}, ","));                // [I@1b9e1916/

lang3 library StringUtils class leftPad, rightPad, center method

Full path of StringUtils class:

import org.apache.commons.lang3.StringUtils;

leftPad left fill

Fills the left side of the string with characters.

  • Parameter 1: the filled string
  • Parameter 2: the length of the filled string. If it is less than the length of parameter 1 string, the original length of parameter 1 will be maintained.
  • Parameter 3 [optional]: what is used to fill in? Blank is used by default
println( StringUtils.leftPad("abc", 1) );   // "abc"
println( StringUtils.leftPad("abc", 3) );   // "abc"
println( StringUtils.leftPad("abc", 6) );   // "   abc"
println( StringUtils.leftPad("abc", 6, "-") );  // "---abc"
println( StringUtils.leftPad(null, 6, "-") );  // Null, not "null", but "null" in Java
println( StringUtils.leftPad("abc", 6, "opqrst") );  // "opqabc"

rightPad right fill

Similar to leftPad, but filled on the right.

println( StringUtils.rightPad("abc", 1) );   // "abc"
println( StringUtils.rightPad("abc", 3) );   // "abc"
println( StringUtils.rightPad("abc", 6) );   // "abc   "
println( StringUtils.rightPad("abc", 6, "-") );  // "abc---"
println( StringUtils.rightPad("abc", 6, "opqrst") );  // "abcopq"

center fill on both sides

The center method places the string in the middle by filling both sides of the string with content.

println( StringUtils.center("abc", 1) );   // "abc"
println( StringUtils.center("abc", 3) );   // "abc"
println( StringUtils.center("abc", 6) );   // " abc  "
println( StringUtils.center("abc", 6, "-") );  // "-abc--"
println( StringUtils.center("abc", 7, "-") );  // "--abc--"
println( StringUtils.center("abc", 6, "xyz") );  // "xabcxy"
println( StringUtils.center("abc", 7, "xyz") );  // "xyabcxy"

lang3 library StringUtils class repeat method

Full path of StringUtils class:

import org.apache.commons.lang3.StringUtils;
The repeat method splices the same string together a specified number of times.
println( StringUtils.repeat("abc", -1) );  // ""
println( StringUtils.repeat("abc", 0) );   // ""
println( StringUtils.repeat("abc", 1) );   // "abc"
println( StringUtils.repeat("abc", 2) );   // "abcabc"
println( StringUtils.repeat(null, 2) );   // Null, not "null", but "null" in Java

 

lang3 library StringUtils class reverse method

Full path of StringUtils class:

import org.apache.commons.lang3.StringUtils;
The reverse method is used to flip a string.
println( StringUtils.reverse(null) );   // null
println( StringUtils.reverse("") );     // ""
println( StringUtils.reverse("abc") );  // "cba"

lang3 library StringUtils class split method

 

Full path of StringUtils class:

import org.apache.commons.lang3.StringUtils;
There are many split methods in StringUtils class, which are introduced here.

splitByWholeSeparatorPreserveAllTokens split string

splitByWholeSeparatorPreserveAllTokens splits the string according to the specified string.

String[] result;
result = StringUtils.splitByWholeSeparatorPreserveAllTokens("ab:cd:ef", ":");
println( Arrays.toString(result) ); // ["ab", "cd", "ef"]

result = StringUtils.splitByWholeSeparatorPreserveAllTokens("ab::cd:ef", ":");
println( Arrays.toString(result) ); // ["ab", "", "cd", "ef"]

result = StringUtils.splitByWholeSeparatorPreserveAllTokens(":ab::cd:ef", ":");
println( Arrays.toString(result) ); // ["", "ab", "", "cd", "ef"]

result = StringUtils.splitByWholeSeparatorPreserveAllTokens(":ab::cd:ef", "::");
println( Arrays.toString(result) ); // [":ab", "cd:ef"]

splitByWholeSeparator split string

splitByWholeSeparator splits the string according to the specified string, and removes the empty string.

String[] result;
result = StringUtils.splitByWholeSeparator("ab:cd:ef", ":");
println( Arrays.toString(result) ); // ["ab", "cd", "ef"]

result = StringUtils.splitByWholeSeparator("ab::cd:ef", ":");
println( Arrays.toString(result) ); // ["ab", "cd", "ef"]

result = StringUtils.splitByWholeSeparator(":ab::cd:ef", ":");
println( Arrays.toString(result) ); // ["ab", "cd", "ef"]

result = StringUtils.splitByWholeSeparator(":ab::cd:ef", "::");
println( Arrays.toString(result) ); // [":ab", "cd:ef"]

lang3 library StringUtils class trim, strip method

Full path of StringUtils class:

import org.apache.commons.lang3.StringUtils;

The trim method is similar to the strip method in that it removes white space from both sides of the string.

The definition of empty white character in strip method is: the character judged as true by isWhitespace static method of java.lang.Character class is blank character. The definition of empty white character in trim method is: control character whose ascii code is less than or equal to 32.

trim method

The trim method essentially calls the String class's trim method directly, but it will handle null additionally.

println( StringUtils.trim(null) );          // null
println( StringUtils.trim("") );            // ""
println( StringUtils.trim(" \tabc ") );     // "abc"

trimToNull

It is equivalent to doing trim first. If the result is' ', null will be returned. Others remain unchanged.

println( StringUtils.trimToNull(null) );  // null
println( StringUtils.trimToNull("") );    // null
println( StringUtils.trimToNull(" \tabc ") );    // "abc"

trimToEmpty

It is equivalent to doing trim first. If the result is null, the empty string '' will be returned. Others remain unchanged.

println( StringUtils.trimToEmpty(null) );  // ""
println( StringUtils.trimToEmpty("") );    // ""
println( StringUtils.trimToEmpty(" \tabc ") );    // "abc"

strip method

println( StringUtils.strip(null) );         // null
println( StringUtils.strip("") );           // ""
println( StringUtils.strip(" \tabc ") );    // "abc"

stripToNull

Similar to trimToNull.

stripToEmpty

Similar to trimToEmpty.

 

lang3 library StringUtils class truncate method

Full path of StringUtils class:

import org.apache.commons.lang3.StringUtils;
The truncate method is used to truncate a string.
  • Parameter 1: string to process
  • Parameter 2: length after truncation. If the length of parameter 1 itself is less than parameter 2,
println( StringUtils.truncate(null, 0) );    // null
println( StringUtils.truncate(null, 2) );    // null
println( StringUtils.truncate("", 0) );      // ""
println( StringUtils.truncate("", 2) );      // ""
println( StringUtils.truncate("abc", 0) );   // ""
println( StringUtils.truncate("abc", 2) );   // "ab"
println( StringUtils.truncate("abc", 4) );   // "abc"

Tags: Programming Apache Java less Maven

Posted on Tue, 05 May 2020 02:40:32 -0700 by cjbeck71081