SoFunction
Updated on 2025-04-11

Use of Java utility library commons-lang3

Java utility library commons-lang3

Apache Commons Lang is a popular library of Java utility tools, in whichcommons-lang3Its latest mainstream version is used to enhance Java core functionality, especially forPackage extension.

The following iscommons-lang3Overview of features and how to use some of these tools (e.g.RegExUtils)。

Maven dependencies

In usecommons-lang3Before, you need to add Maven dependencies to your project:

<dependency>
    <groupId></groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.12.0</version> <!-- Or the latest version -->
</dependency>

Main tools and functions

1. StringUtils

  • Provides various practical methods for string manipulation.
import .;

public class StringUtilsExample {
    public static void main(String[] args) {
        String str = "  Hello World  ";

        // Determine whether the string is empty or blank        ((str)); // false

        // Delete spaces at both ends        ((str)); // "Hello World"

        // Invert the string        ((str)); // "  dlroW olleH  "
    }
}

2. RegExUtils

  • Advanced tools for handling regular expressions.
import .;

public class RegExUtilsExample {
    public static void main(String[] args) {
        String text = "key1:value1, key2:value2, key3:value3";

        // Replace colon with underscore        String result = (text, ":", "_");
        (result); // "key1_value1, key2_value2, key3_value3"
    }
}

3. NumberUtils

  • Provides tools and methods for digital operations.
import .;

public class NumberUtilsExample {
    public static void main(String[] args) {
        // Determine whether it is a number        (("123")); // true
        (("12.3")); // true
        (("abc")); // false

        // Find the maximum value in the array        int[] numbers = {1, 2, 3, 4, 5};
        ((numbers)); // 5
    }
}

4. DateUtils

  • Provides tools for date and time operation.
import .;

import ;
import ;

public class DateUtilsExample {
    public static void main(String[] args) throws ParseException {
        String dateStr = "2025-01-01";

        //Date of parsing        Date date = (dateStr, "yyyy-MM-dd");
        (date);

        // Add days        Date newDate = (date, 5);
        (newDate);
    }
}

5. RandomStringUtils

  • A tool for generating random strings.
import .;

public class RandomStringUtilsExample {
    public static void main(String[] args) {
        // Generate random alphabetical strings        String randomAlphabetic = (10);
        (randomAlphabetic);

        // Generate random numeric strings        String randomNumeric = (10);
        (randomNumeric);
    }
}

6. ObjectUtils

  • Provides tools for manipulating objects.
import .;

public class ObjectUtilsExample {
    public static void main(String[] args) {
        String str = null;

        // If the object is null, provide the default value        ((str, "Default Value")); // "Default Value"

        // Determine whether multiple objects are non-empty        (("A", "B", "C")); // true
        (("A", null, "C")); // false
    }
}

Summarize

commons-lang3Provides a series of tool classes that enhance the core Java functions, commonly used functions include:

  • String processingStringUtilsRegExUtils
  • Digital operationNumberUtils
  • Date operationDateUtils
  • Random string generationRandomStringUtils
  • Object ToolsObjectUtils

The above is personal experience. I hope you can give you a reference and I hope you can support me more.