public final class StringUtils extends Object
Operations on String that are
null safe.
The StringUtils class defines certain words related to
String handling.
null"")' ', char 32)Character.isWhitespace(char)String.trim()StringUtils handles null input Strings quietly.
That is to say that a null input will return null.
Where a boolean or int is being returned
details vary by method.
A side effect of the null handling is that a
NullPointerException should be considered a bug in
StringUtils.
This class's source was modified from the Apache commons-lang library: https://github.com/apache/commons-lang/
#ThreadSafe#
String| Modifier and Type | Method and Description |
|---|---|
static String |
capitalize(String str)
Capitalizes a String changing the first character to title case as
per
Character.toTitleCase(int). |
static boolean |
equals(String cs1,
String cs2)
Compares two Strings, returning
true if they represent
equal sequences of characters. |
static String |
fromBytes(byte[] bytes,
Charset charset)
Encode the given bytes as a string using the given charset
|
static boolean |
isBlank(CharSequence cs)
Checks if a CharSequence is empty (""), null or whitespace only.
|
static boolean |
isEmpty(CharSequence cs)
Checks if a CharSequence is empty ("") or null.
|
static boolean |
isNotBlank(CharSequence cs)
Checks if a CharSequence is not empty (""), not null and not whitespace only.
|
static String |
lowerCase(String str)
Converts a String to lower case as per
String.toLowerCase(). |
static String |
replacePrefixIgnoreCase(String str,
String prefix,
String replacement)
Replace the prefix of the string provided ignoring case considerations.
|
static boolean |
startsWithIgnoreCase(String str,
String prefix)
Tests if this string starts with the specified prefix ignoring case considerations.
|
static String |
substring(String str,
int start)
Gets a substring from the specified String avoiding exceptions.
|
static String |
substring(String str,
int start,
int end)
Gets a substring from the specified String avoiding exceptions.
|
static String |
trim(String str)
Removes control characters (char <= 32) from both
ends of this String, handling
null by returning
null. |
static String |
trimToEmpty(String str)
Removes control characters (char <= 32) from both
ends of this String returning an empty String ("") if the String
is empty ("") after the trim or if it is
null. |
static String |
trimToNull(String str)
Removes control characters (char <= 32) from both
ends of this String returning
null if the String is
empty ("") after the trim or if it is null. |
static String |
uncapitalize(String str)
Uncapitalizes a String, changing the first character to lower case as
per
Character.toLowerCase(int). |
static String |
upperCase(String str)
Converts a String to upper case as per
String.toUpperCase(). |
public static boolean isEmpty(CharSequence cs)
Checks if a CharSequence is empty ("") or null.
StringUtils.isEmpty(null) = true
StringUtils.isEmpty("") = true
StringUtils.isEmpty(" ") = false
StringUtils.isEmpty("bob") = false
StringUtils.isEmpty(" bob ") = false
NOTE: This method changed in Lang version 2.0. It no longer trims the CharSequence. That functionality is available in isBlank().
cs - the CharSequence to check, may be nulltrue if the CharSequence is empty or nullpublic static boolean isBlank(CharSequence cs)
Checks if a CharSequence is empty (""), null or whitespace only.
Whitespace is defined by Character.isWhitespace(char).
StringUtils.isBlank(null) = true
StringUtils.isBlank("") = true
StringUtils.isBlank(" ") = true
StringUtils.isBlank("bob") = false
StringUtils.isBlank(" bob ") = false
cs - the CharSequence to check, may be nulltrue if the CharSequence is null, empty or whitespace onlypublic static boolean isNotBlank(CharSequence cs)
Checks if a CharSequence is not empty (""), not null and not whitespace only.
Whitespace is defined by Character.isWhitespace(char).
StringUtils.isNotBlank(null) = false
StringUtils.isNotBlank("") = false
StringUtils.isNotBlank(" ") = false
StringUtils.isNotBlank("bob") = true
StringUtils.isNotBlank(" bob ") = true
cs - the CharSequence to check, may be nulltrue if the CharSequence is not empty and not null and not whitespace onlypublic static String trim(String str)
Removes control characters (char <= 32) from both
ends of this String, handling null by returning
null.
The String is trimmed using String.trim().
Trim removes start and end characters <= 32.
StringUtils.trim(null) = null
StringUtils.trim("") = ""
StringUtils.trim(" ") = ""
StringUtils.trim("abc") = "abc"
StringUtils.trim(" abc ") = "abc"
str - the String to be trimmed, may be nullnull if null String inputpublic static String trimToNull(String str)
Removes control characters (char <= 32) from both
ends of this String returning null if the String is
empty ("") after the trim or if it is null.
The String is trimmed using String.trim().
Trim removes start and end characters <= 32.
StringUtils.trimToNull(null) = null
StringUtils.trimToNull("") = null
StringUtils.trimToNull(" ") = null
StringUtils.trimToNull("abc") = "abc"
StringUtils.trimToNull(" abc ") = "abc"
str - the String to be trimmed, may be nullnull if only chars <= 32, empty or null String inputpublic static String trimToEmpty(String str)
Removes control characters (char <= 32) from both
ends of this String returning an empty String ("") if the String
is empty ("") after the trim or if it is null.
The String is trimmed using String.trim().
Trim removes start and end characters <= 32.
StringUtils.trimToEmpty(null) = ""
StringUtils.trimToEmpty("") = ""
StringUtils.trimToEmpty(" ") = ""
StringUtils.trimToEmpty("abc") = "abc"
StringUtils.trimToEmpty(" abc ") = "abc"
str - the String to be trimmed, may be nullnull inputpublic static boolean equals(String cs1, String cs2)
Compares two Strings, returning true if they represent
equal sequences of characters.
nulls are handled without exceptions. Two null
references are considered to be equal. The comparison is case sensitive.
StringUtils.equals(null, null) = true
StringUtils.equals(null, "abc") = false
StringUtils.equals("abc", null) = false
StringUtils.equals("abc", "abc") = true
StringUtils.equals("abc", "ABC") = false
cs1 - the first String, may be nullcs2 - the second String, may be nulltrue if the Strings are equal (case-sensitive), or both nullObject.equals(Object)public static String substring(String str, int start)
Gets a substring from the specified String avoiding exceptions.
A negative start position can be used to start n
characters from the end of the String.
A null String will return null.
An empty ("") String will return "".
StringUtils.substring(null, *) = null
StringUtils.substring("", *) = ""
StringUtils.substring("abc", 0) = "abc"
StringUtils.substring("abc", 2) = "c"
StringUtils.substring("abc", 4) = ""
StringUtils.substring("abc", -2) = "bc"
StringUtils.substring("abc", -4) = "abc"
str - the String to get the substring from, may be nullstart - the position to start from, negative means count back from the end of the String by this many charactersnull if null String inputpublic static String substring(String str, int start, int end)
Gets a substring from the specified String avoiding exceptions.
A negative start position can be used to start/end n
characters from the end of the String.
The returned substring starts with the character in the start
position and ends before the end position. All position counting is
zero-based -- i.e., to start at the beginning of the string use
start = 0. Negative start and end positions can be used to
specify offsets relative to the end of the String.
If start is not strictly to the left of end, ""
is returned.
StringUtils.substring(null, *, *) = null
StringUtils.substring("", * , *) = "";
StringUtils.substring("abc", 0, 2) = "ab"
StringUtils.substring("abc", 2, 0) = ""
StringUtils.substring("abc", 2, 4) = "c"
StringUtils.substring("abc", 4, 6) = ""
StringUtils.substring("abc", 2, 2) = ""
StringUtils.substring("abc", -2, -1) = "b"
StringUtils.substring("abc", -4, 2) = "ab"
str - the String to get the substring from, may be nullstart - the position to start from, negative means count back from the end of the String by this many charactersend - the position to end at (exclusive), negative means count back from the end of the String by this many
charactersnull if null String inputpublic static String upperCase(String str)
Converts a String to upper case as per String.toUpperCase().
A null input String returns null.
StringUtils.upperCase(null) = null
StringUtils.upperCase("") = ""
StringUtils.upperCase("aBc") = "ABC"
This uses "ENGLISH" as the locale.
str - the String to upper case, may be nullnull if null String inputpublic static String lowerCase(String str)
Converts a String to lower case as per String.toLowerCase().
A null input String returns null.
StringUtils.lowerCase(null) = null
StringUtils.lowerCase("") = ""
StringUtils.lowerCase("aBc") = "abc"
This uses "ENGLISH" as the locale.
str - the String to lower case, may be nullnull if null String inputpublic static String capitalize(String str)
Capitalizes a String changing the first character to title case as
per Character.toTitleCase(int). No other characters are changed.
StringUtils.capitalize(null) = null
StringUtils.capitalize("") = ""
StringUtils.capitalize("cat") = "Cat"
StringUtils.capitalize("cAt") = "CAt"
StringUtils.capitalize("'cat'") = "'cat'"
str - the String to capitalize, may be nullnull if null String inputuncapitalize(String)public static String uncapitalize(String str)
Uncapitalizes a String, changing the first character to lower case as
per Character.toLowerCase(int). No other characters are changed.
StringUtils.uncapitalize(null) = null
StringUtils.uncapitalize("") = ""
StringUtils.uncapitalize("cat") = "cat"
StringUtils.uncapitalize("Cat") = "cat"
StringUtils.uncapitalize("CAT") = "cAT"
str - the String to uncapitalize, may be nullnull if null String inputcapitalize(String)public static String fromBytes(byte[] bytes, Charset charset) throws UncheckedIOException
UncheckedIOException - with a CharacterCodingException as the cause if the bytes cannot be encoded using the
provided charset.public static boolean startsWithIgnoreCase(String str, String prefix)
str - the string to be testedprefix - the prefixpublic static String replacePrefixIgnoreCase(String str, String prefix, String replacement)
The unmatched part is unchanged.
str - the string to replaceprefix - the prefix to findreplacement - the replacementCopyright © 2019. All rights reserved.