Skip to content

io microsphere util StringUtils

github-actions[bot] edited this page Mar 21, 2026 · 1 revision

StringUtils

Type: Class | Module: microsphere-java-core | Package: io.microsphere.util | Since: 1.0.0

Source: microsphere-java-core/src/main/java/io/microsphere/util/StringUtils.java

Overview

The utilities class for String

Declaration

public abstract class StringUtils implements Utils

Author: Mercy

Version Information

  • Introduced in: 1.0.0
  • Current Project Version: 0.1.10-SNAPSHOT

Version Compatibility

This component is tested and compatible with the following Java versions:

Java Version Status
Java 8 ✅ Compatible
Java 11 ✅ Compatible
Java 17 ✅ Compatible
Java 21 ✅ Compatible
Java 25 ✅ Compatible

Examples

Method Examples

isBlank

StringUtils.isBlank(null)      = true
StringUtils.isBlank("")        = true
StringUtils.isBlank(" ")       = true
StringUtils.isBlank("  a  ")   = false
StringUtils.isBlank("abc")    = false
StringUtils.isBlank("\t\n\f")  = true

isNotBlank

StringUtils.isNotBlank(null)      = false
StringUtils.isNotBlank("")        = false
StringUtils.isNotBlank(" ")       = false
StringUtils.isNotBlank("  a  ")   = true
StringUtils.isNotBlank("abc")     = true
StringUtils.isNotBlank("\t\n\f")  = false

split

StringUtils.split(null, ',')       = []
StringUtils.split("", ';')         = []
StringUtils.split("a,b,c", ',')    = ["a", "b", "c"]
StringUtils.split("a;b;c", ',')    = ["a;b;c"]
StringUtils.split("a,,b,c", ',')   = ["a", "", "b", "c"]

split

StringUtils.split(null, ",")       = []
StringUtils.split("", null)        = []
StringUtils.split("", ";")         = []
StringUtils.split("abc", "")       = ["a", "b", "c"]
StringUtils.split("a,b,c", ",")    = ["a", "b", "c"]
StringUtils.split("a;b;c", ",")    = ["a;b;c"]
StringUtils.split("a,,b,c", ",")   = ["a", "", "b", "c"]

contains

StringUtils.contains(null, null)     = false
StringUtils.contains(null, "abc")    = false
StringUtils.contains("abc", null)    = false
StringUtils.contains("", "")         = true
StringUtils.contains("abc", "")      = true
StringUtils.contains("abc", "a")     = true
StringUtils.contains("abc", "z")     = false
StringUtils.contains("abc", "abc")   = true

startsWith

StringUtils.startsWith(null, null)     = false
StringUtils.startsWith(null, "abc")    = false
StringUtils.startsWith("abc", null)    = false
StringUtils.startsWith("", "")         = true
StringUtils.startsWith("abc", "")      = true
StringUtils.startsWith("abc", "a")     = true
StringUtils.startsWith("abc", "ab")    = true
StringUtils.startsWith("abc", "z")     = false
StringUtils.startsWith("abc", "abcd")  = false

endsWith

StringUtils.endsWith(null, null)     = false
StringUtils.endsWith(null, "abc")    = false
StringUtils.endsWith("abc", null)    = false
StringUtils.endsWith("", "")         = true
StringUtils.endsWith("abc", "")      = true
StringUtils.endsWith("abc", "c")     = true
StringUtils.endsWith("abc", "bc")    = true
StringUtils.endsWith("abc", "abc")   = true
StringUtils.endsWith("abc", "d")     = false
StringUtils.endsWith("abc", "abcd")  = false

replace

StringUtils.replace(null, *, *)        = null
StringUtils.replace("", *, *)          = ""
StringUtils.replace("any", null, *)    = "any"
StringUtils.replace("any", *, null)    = "any"
StringUtils.replace("any", "", *)      = "any"
StringUtils.replace("aba", "a", null)  = "aba"
StringUtils.replace("aba", "a", "")    = "b"
StringUtils.replace("aba", "a", "z")   = "zbz"

replace

StringUtils.replace(null, *, *, *)         = null
StringUtils.replace("", *, *, *)           = ""
StringUtils.replace("any", null, *, *)     = "any"
StringUtils.replace("any", *, null, *)     = "any"
StringUtils.replace("any", "", *, *)       = "any"
StringUtils.replace("any", *, *, 0)        = "any"
StringUtils.replace("abaa", "a", null, -1) = "abaa"
StringUtils.replace("abaa", "a", "", -1)   = "b"
StringUtils.replace("abaa", "a", "z", 0)   = "abaa"
StringUtils.replace("abaa", "a", "z", 1)   = "zbaa"
StringUtils.replace("abaa", "a", "z", 2)   = "zbza"
StringUtils.replace("abaa", "a", "z", -1)  = "zbzz"

substringBetween

StringUtils.substringBetween(null, *)            = null
StringUtils.substringBetween("", "")             = ""
StringUtils.substringBetween("", "tag")          = null
StringUtils.substringBetween("tagabctag", null)  = null
StringUtils.substringBetween("tagabctag", "")    = ""
StringUtils.substringBetween("tagabctag", "tag") = "abc"

substringBetween

StringUtils.substringBetween("wx[b]yz", "[", "]") = "b"
StringUtils.substringBetween(null, *, *)          = null
StringUtils.substringBetween(*, null, *)          = null
StringUtils.substringBetween(*, *, null)          = null
StringUtils.substringBetween("", "", "")          = ""
StringUtils.substringBetween("", "", "]")         = null
StringUtils.substringBetween("", "[", "]")        = null
StringUtils.substringBetween("yabcz", "", "")     = ""
StringUtils.substringBetween("yabcz", "y", "z")   = "abc"
StringUtils.substringBetween("yabczyabcz", "y", "z")   = "abc"

substringBefore

StringUtils.substringBefore(null, *)      = null
StringUtils.substringBefore("", *)        = ""
StringUtils.substringBefore("abc", "a")   = ""
StringUtils.substringBefore("abcba", "b") = "a"
StringUtils.substringBefore("abc", "c")   = "ab"
StringUtils.substringBefore("abc", "d")   = "abc"
StringUtils.substringBefore("abc", "")    = ""
StringUtils.substringBefore("abc", null)  = "abc"

substringAfter

StringUtils.substringAfter(null, *)      = null
StringUtils.substringAfter("", *)        = ""
StringUtils.substringAfter(*, null)      = ""
StringUtils.substringAfter("abc", "a")   = "bc"
StringUtils.substringAfter("abcba", "b") = "cba"
StringUtils.substringAfter("abc", "c")   = ""
StringUtils.substringAfter("abc", "d")   = ""
StringUtils.substringAfter("abc", "")    = "abc"

substringBeforeLast

StringUtils.substringBeforeLast(null, *)      = null
StringUtils.substringBeforeLast("", *)        = ""
StringUtils.substringBeforeLast("abcba", "b") = "abc"
StringUtils.substringBeforeLast("abc", "c")   = "ab"
StringUtils.substringBeforeLast("a", "a")     = ""
StringUtils.substringBeforeLast("a", "z")     = "a"
StringUtils.substringBeforeLast("a", null)    = "a"
StringUtils.substringBeforeLast("a", "")      = "a"

substringAfterLast

StringUtils.substringAfterLast(null, *)      = null
StringUtils.substringAfterLast("", *)        = ""
StringUtils.substringAfterLast(*, "")        = ""
StringUtils.substringAfterLast(*, null)      = ""
StringUtils.substringAfterLast("abc", "a")   = "bc"
StringUtils.substringAfterLast("abcba", "b") = "a"
StringUtils.substringAfterLast("abc", "c")   = ""
StringUtils.substringAfterLast("a", "a")     = ""
StringUtils.substringAfterLast("a", "z")     = ""

isNumeric

StringUtils.isNumeric(null)   = false
StringUtils.isNumeric("")     = true
StringUtils.isNumeric("  ")   = false
StringUtils.isNumeric("123")  = true
StringUtils.isNumeric("12 3") = false
StringUtils.isNumeric("ab2c") = false
StringUtils.isNumeric("12-3") = false
StringUtils.isNumeric("12.3") = false

Usage

Maven Dependency

Add the following dependency to your pom.xml:

<dependency>
    <groupId>io.github.microsphere-projects</groupId>
    <artifactId>microsphere-java-core</artifactId>
    <version>${microsphere-java.version}</version>
</dependency>

Tip: Use the BOM (microsphere-java-dependencies) for consistent version management. See the Getting Started guide.

Import

import io.microsphere.util.StringUtils;

API Reference

Public Methods

Method Description
isBlank Represents an empty string constant : ""
isNotBlank Checks if a String is not blank.
split Splits the provided String into an array of Strings using the specified char delimiter.
split Splits the provided String into an array of Strings using the specified String delimiter.
contains Checks if a CharSequence contains another CharSequence.
startsWith Checks if a String starts with another String.
endsWith Checks if a String ends with another String.
replace Represents a failed index search.
replace Replaces a String with another String inside a larger String,
substringBetween Gets the String that is nested in between two instances of the
substringBetween Gets the String that is nested in between two Strings.
substringBefore Gets the substring before the first occurrence of a separator.
substringAfter Gets the substring after the first occurrence of a separator.
substringBeforeLast Gets the substring before the last occurrence of a separator.
substringAfterLast Gets the substring after the last occurrence of a separator.
isNumeric Checks if the String contains only unicode digits.
containsWhitespace Checks whether the given String contains any whitespace characters.
trimWhitespace Trims leading and trailing whitespace from the given String.
trimLeadingWhitespace Trims leading whitespace from the given String.
trimTrailingWhitespace Trims trailing whitespace from the given String.

Method Details

isBlank

public static boolean isBlank(String value)

Represents an empty string constant : "" / public final static String EMPTY = "";

/** Represents an empty string constant: "" / public final static String EMPTY_STRING = EMPTY;

/** An empty array of String. /

isNotBlank

public static boolean isNotBlank(String value)

Checks if a String is not blank.

A string is considered not blank if it contains at least one non-whitespace character. This method is the inverse of #isBlank(String).

Example Usage

`StringUtils.isNotBlank(null)      = false
StringUtils.isNotBlank("")        = false
StringUtils.isNotBlank(" ")       = false
StringUtils.isNotBlank("  a  ")   = true
StringUtils.isNotBlank("abc")     = true
StringUtils.isNotBlank("\t\n\f")  = false
`

split

public static String[] split(@Nullable String value, char delimiter)

Splits the provided String into an array of Strings using the specified char delimiter.

A null or empty input String returns an empty array. If the delimiter is not found, an array containing just the input String is returned.

Example Usage

`StringUtils.split(null, ',')       = []
StringUtils.split("", ';')         = []
StringUtils.split("a,b,c", ',')    = ["a", "b", "c"]
StringUtils.split("a;b;c", ',')    = ["a;b;c"]
StringUtils.split("a,,b,c", ',')   = ["a", "", "b", "c"]
`

split

public static String[] split(@Nullable String value, @Nullable String delimiter)

Splits the provided String into an array of Strings using the specified String delimiter.

A null or empty input String returns an empty array. If the delimiter is not found, an array containing just the input String is returned.

Example Usage

`StringUtils.split(null, ",")       = []
StringUtils.split("", null)        = []
StringUtils.split("", ";")         = []
StringUtils.split("abc", "")       = ["a", "b", "c"]
StringUtils.split("a,b,c", ",")    = ["a", "b", "c"]
StringUtils.split("a;b;c", ",")    = ["a;b;c"]
StringUtils.split("a,,b,c", ",")   = ["a", "", "b", "c"]
`

contains

public static boolean contains(String value, CharSequence part)

Checks if a CharSequence contains another CharSequence.

This method is case-sensitive and uses the String#contains(CharSequence) method. A null CharSequence returns false.

Example Usage

`StringUtils.contains(null, null)     = false
StringUtils.contains(null, "abc")    = false
StringUtils.contains("abc", null)    = false
StringUtils.contains("", "")         = true
StringUtils.contains("abc", "")      = true
StringUtils.contains("abc", "a")     = true
StringUtils.contains("abc", "z")     = false
StringUtils.contains("abc", "abc")   = true
`

startsWith

public static boolean startsWith(String value, String part)

Checks if a String starts with another String.

This method is case-sensitive and uses the String#startsWith(String) method. A null reference for either parameter returns false.

Example Usage

`StringUtils.startsWith(null, null)     = false
StringUtils.startsWith(null, "abc")    = false
StringUtils.startsWith("abc", null)    = false
StringUtils.startsWith("", "")         = true
StringUtils.startsWith("abc", "")      = true
StringUtils.startsWith("abc", "a")     = true
StringUtils.startsWith("abc", "ab")    = true
StringUtils.startsWith("abc", "z")     = false
StringUtils.startsWith("abc", "abcd")  = false
`

endsWith

public static boolean endsWith(String value, String part)

Checks if a String ends with another String.

This method is case-sensitive and uses the String#endsWith(String) method. A null reference for either parameter returns false.

Example Usage

`StringUtils.endsWith(null, null)     = false
StringUtils.endsWith(null, "abc")    = false
StringUtils.endsWith("abc", null)    = false
StringUtils.endsWith("", "")         = true
StringUtils.endsWith("abc", "")      = true
StringUtils.endsWith("abc", "c")     = true
StringUtils.endsWith("abc", "bc")    = true
StringUtils.endsWith("abc", "abc")   = true
StringUtils.endsWith("abc", "d")     = false
StringUtils.endsWith("abc", "abcd")  = false
`

replace

public static String replace(String text, String searchString, String replacement)

Represents a failed index search. / public static final int INDEX_NOT_FOUND = -1;

/**

Replaces all occurrences of a String within another String.

A null reference passed to this method is a no-op.

Example Usage

`StringUtils.replace(null, *, *)        = null
StringUtils.replace("", *, *)          = ""
StringUtils.replace("any", null, *)    = "any"
StringUtils.replace("any", *, null)    = "any"
StringUtils.replace("any", "", *)      = "any"
StringUtils.replace("aba", "a", null)  = "aba"
StringUtils.replace("aba", "a", "")    = "b"
StringUtils.replace("aba", "a", "z")   = "zbz"
`

replace

public static String replace(String text, String searchString, String replacement, int max)

Replaces a String with another String inside a larger String, for the first max values of the search String.

A null reference passed to this method is a no-op.

Example Usage

`StringUtils.replace(null, *, *, *)         = null
StringUtils.replace("", *, *, *)           = ""
StringUtils.replace("any", null, *, *)     = "any"
StringUtils.replace("any", *, null, *)     = "any"
StringUtils.replace("any", "", *, *)       = "any"
StringUtils.replace("any", *, *, 0)        = "any"
StringUtils.replace("abaa", "a", null, -1) = "abaa"
StringUtils.replace("abaa", "a", "", -1)   = "b"
StringUtils.replace("abaa", "a", "z", 0)   = "abaa"
StringUtils.replace("abaa", "a", "z", 1)   = "zbaa"
StringUtils.replace("abaa", "a", "z", 2)   = "zbza"
StringUtils.replace("abaa", "a", "z", -1)  = "zbzz"
`

substringBetween

public static String substringBetween(String str, String tag)

Gets the String that is nested in between two instances of the same String.

A null input String returns null. A null tag returns null.

Example Usage

`StringUtils.substringBetween(null, *)            = null
StringUtils.substringBetween("", "")             = ""
StringUtils.substringBetween("", "tag")          = null
StringUtils.substringBetween("tagabctag", null)  = null
StringUtils.substringBetween("tagabctag", "")    = ""
StringUtils.substringBetween("tagabctag", "tag") = "abc"
`

substringBetween

public static String substringBetween(String str, String open, String close)

Gets the String that is nested in between two Strings. Only the first match is returned.

A null input String returns null. A null open/close returns null (no match). An empty ("") open and close returns an empty string.

Example Usage

`StringUtils.substringBetween("wx[b]yz", "[", "]") = "b"
StringUtils.substringBetween(null, *, *)          = null
StringUtils.substringBetween(*, null, *)          = null
StringUtils.substringBetween(*, *, null)          = null
StringUtils.substringBetween("", "", "")          = ""
StringUtils.substringBetween("", "", "]")         = null
StringUtils.substringBetween("", "[", "]")        = null
StringUtils.substringBetween("yabcz", "", "")     = ""
StringUtils.substringBetween("yabcz", "y", "z")   = "abc"
StringUtils.substringBetween("yabczyabcz", "y", "z")   = "abc"
`

substringBefore

public static String substringBefore(String str, String separator)

Gets the substring before the first occurrence of a separator. The separator is not returned.

A null string input will return null. An empty ("") string input will return the empty string. A null separator will return the input string.

If nothing is found, the string input is returned.

Example Usage

`StringUtils.substringBefore(null, *)      = null
StringUtils.substringBefore("", *)        = ""
StringUtils.substringBefore("abc", "a")   = ""
StringUtils.substringBefore("abcba", "b") = "a"
StringUtils.substringBefore("abc", "c")   = "ab"
StringUtils.substringBefore("abc", "d")   = "abc"
StringUtils.substringBefore("abc", "")    = ""
StringUtils.substringBefore("abc", null)  = "abc"
`

substringAfter

public static String substringAfter(String str, String separator)

Gets the substring after the first occurrence of a separator. The separator is not returned.

A null string input will return null. An empty ("") string input will return the empty string. A null separator will return the empty string if the input string is not null.

If nothing is found, the empty string is returned.

Example Usage

`StringUtils.substringAfter(null, *)      = null
StringUtils.substringAfter("", *)        = ""
StringUtils.substringAfter(*, null)      = ""
StringUtils.substringAfter("abc", "a")   = "bc"
StringUtils.substringAfter("abcba", "b") = "cba"
StringUtils.substringAfter("abc", "c")   = ""
StringUtils.substringAfter("abc", "d")   = ""
StringUtils.substringAfter("abc", "")    = "abc"
`

substringBeforeLast

public static String substringBeforeLast(String str, String separator)

Gets the substring before the last occurrence of a separator. The separator is not returned.

A null string input will return null. An empty ("") string input will return the empty string. An empty or null separator will return the input string.

If nothing is found, the string input is returned.

Example Usage

`StringUtils.substringBeforeLast(null, *)      = null
StringUtils.substringBeforeLast("", *)        = ""
StringUtils.substringBeforeLast("abcba", "b") = "abc"
StringUtils.substringBeforeLast("abc", "c")   = "ab"
StringUtils.substringBeforeLast("a", "a")     = ""
StringUtils.substringBeforeLast("a", "z")     = "a"
StringUtils.substringBeforeLast("a", null)    = "a"
StringUtils.substringBeforeLast("a", "")      = "a"
`

substringAfterLast

public static String substringAfterLast(String str, String separator)

Gets the substring after the last occurrence of a separator. The separator is not returned.

A null string input will return null. An empty ("") string input will return the empty string. An empty or null separator will return the empty string if the input string is not null.

If nothing is found, the empty string is returned.

Example Usage

`StringUtils.substringAfterLast(null, *)      = null
StringUtils.substringAfterLast("", *)        = ""
StringUtils.substringAfterLast(*, "")        = ""
StringUtils.substringAfterLast(*, null)      = ""
StringUtils.substringAfterLast("abc", "a")   = "bc"
StringUtils.substringAfterLast("abcba", "b") = "a"
StringUtils.substringAfterLast("abc", "c")   = ""
StringUtils.substringAfterLast("a", "a")     = ""
StringUtils.substringAfterLast("a", "z")     = ""
`

isNumeric

public static boolean isNumeric(String str)

Checks if the String contains only unicode digits. A decimal point is not a unicode digit and returns false.

null will return false. An empty String (length()=0) will return true.

Example Usage

`StringUtils.isNumeric(null)   = false
StringUtils.isNumeric("")     = true
StringUtils.isNumeric("  ")   = false
StringUtils.isNumeric("123")  = true
StringUtils.isNumeric("12 3") = false
StringUtils.isNumeric("ab2c") = false
StringUtils.isNumeric("12-3") = false
StringUtils.isNumeric("12.3") = false
`

containsWhitespace

public static boolean containsWhitespace(@Nullable String str)

Checks whether the given String contains any whitespace characters.

A whitespace character is defined as any character that returns true when passed to Character#isWhitespace(char).

Example Usage

- `containsWhitespace(null)` returns `false`
- `containsWhitespace("")` returns `false`
- `containsWhitespace("hello world")` returns `true`
- `containsWhitespace("hello\tworld")` returns `true`
- `containsWhitespace("helloworld")` returns `false`

trimWhitespace

public static String trimWhitespace(String str)

Trims leading and trailing whitespace from the given String.

This method removes whitespace characters (as defined by Character#isWhitespace(char)) from the beginning and end of the input string. If the input is null or empty, it will be returned as-is.

Example Usage

- `trimWhitespace(null)` returns `null`
- `trimWhitespace("")` returns `""`
- `trimWhitespace("  abc  ")` returns `"abc"`
- `trimWhitespace("abc")` returns `"abc"`
- `trimWhitespace("   abc def   ")` returns `"abc def"`

trimLeadingWhitespace

public static String trimLeadingWhitespace(String str)

Trims leading whitespace from the given String.

This method removes whitespace characters (as defined by Character#isWhitespace(char)) from the beginning of the input string. If the input is null or empty, it will be returned as-is.

Example Usage

- `trimLeadingWhitespace(null)` returns `null`
- `trimLeadingWhitespace("")` returns `""`
- `trimLeadingWhitespace("  abc  ")` returns `"abc  "`
- `trimLeadingWhitespace("abc")` returns `"abc"`
- `trimLeadingWhitespace("   abc def   ")` returns `"abc def   "`

trimTrailingWhitespace

public static String trimTrailingWhitespace(String str)

Trims trailing whitespace from the given String.

This method removes whitespace characters (as defined by Character#isWhitespace(char)) from the end of the input string. If the input is null or empty, it will be returned as-is.

Example Usage

- `trimTrailingWhitespace(null)` returns `null`
- `trimTrailingWhitespace("")` returns `""`
- `trimTrailingWhitespace("  abc  ")` returns `"  abc"`
- `trimTrailingWhitespace("abc")` returns `"abc"`
- `trimTrailingWhitespace("abc def   ")` returns `"abc def"`

This documentation was auto-generated from the source code of microsphere-java.

Home

java-annotations

java-core

jdk-tools

lang-model

annotation-processor

java-test

Clone this wiki locally