public class StringUtil extends Object
null
as an
empty string (but see JavaDoc).Constructor and Description |
---|
StringUtil() |
Modifier and Type | Method and Description |
---|---|
static boolean |
contains(String str,
String segment)
Determines whether the first string contains the second.
|
static boolean |
containsIgnoreCase(String str,
String segment)
Determines whether the first string contains the second, ignoring
case of individual letters.
|
static boolean |
equalOrEmpty(String s1,
String s2)
Tests for equality, where null is equivalent to an empty string.
|
static String |
extractLeft(String source,
String target)
Returns a substring of the source string, from its start up to but not
including the first occurrence of the target string.
|
static String |
extractLeftOfLast(String source,
String target)
Returns a substring of the source string, from its start up to but not
including the last occurrence of the target string.
|
static String |
extractRight(String source,
String target)
Returns a substring of the source string, from immediately after the
first occurrence of the target string, to the end of the source.
|
static String |
extractRightOfLast(String source,
String target)
Returns a substring of the source string, from immediately after the
last occurrence of the target string, to the end of the source.
|
static String |
fromUTF8(byte[] bytes)
Converts the passed byte array to a string, using UTF-8 encoding, and
turning the checked exception (which should never happen) into a runtime
exception.
|
static String |
intern(String str)
Interns a string using a static instance of
StringCanon . |
static boolean |
isBlank(String str)
A null-safe check for strings that contain only whitespace (if
anything).
|
static boolean |
isEmpty(String str)
A null-safe check for empty strings, where
null is
considered an empty string. |
static boolean |
isIn(String str,
String... targets)
Returns
true if the passed string is equal to one of
the target strings, false otherwise. |
static char |
lastChar(String str)
Returns the last character in the passed string, '\0' if passed
null or an empty string.
|
static int |
length(String str)
Returns the length of the passed string, 0 if the string is null.
|
static String |
padLeft(String str,
int len,
char c)
Pads a string that is < N characters by adding the specified character
on the left side.
|
static String |
padRight(String str,
int len,
char c)
Pads a string that is < N characters by adding the specified character
on the right side.
|
static int |
parseDigit(char c,
int base)
Parses the passed character as a digit in the specified base,
returning its value.
|
static String |
randomAlphaString(int minLength,
int maxLength)
Generates a string containing (non-cryptographicaly-) random ASCII alphabetic
characters (A-Za-z).
|
static String |
randomString(String chars,
int minLength,
int maxLength)
Generates a (non-cryptographicaly-) random string consisting of characters
from the passed string.
|
static String |
repeat(char c,
int count)
Creates a string that consists of a single character, repeated N times.
|
static String |
substr(String src,
int off,
int len)
A flexible substring that can return left or right substrings, and returns a
"best effort" result if offset or length are outside the bounds of the string.
|
static byte[] |
toUTF8(String str)
Converts the string to a UTF-8 byte array, turning the checked exception
(which should never happen) into a runtime exception.
|
static String |
trim(String str)
Removes all whitespace characters (per
Character.isWhitespace()
) from either side of a string. |
static String |
trimToNull(String str)
Invokes
trim(java.lang.String) , and returns null if the result is an empty string. |
static String |
unescape(String src)
Un-escapes the passed string, replacing the standard slash escapes
with their corresponding unicode character value.
|
static String |
unicodeEscape(String src)
Escapes the passed string, replacing all characters outside the range
32..126, as well as quotes and backslashes, with unicode escapes.
|
static String |
valueOf(Object obj)
A replacement for
String.valueOf() that returns an empty
string for null. |
public static int length(String str)
public static boolean equalOrEmpty(String s1, String s2)
public static char lastChar(String str)
public static boolean isEmpty(String str)
null
is
considered an empty string.public static boolean isBlank(String str)
public static String trim(String str)
Character.isWhitespace()
) from either side of a string. If passed null, will return an
empty string. Will return the original string if it doesn't need
trimming.public static String trimToNull(String str)
trim(java.lang.String)
, and returns null if the result is an empty string.
Otherwise returns the trimmed string. This is useful to create flag values.public static String padLeft(String str, int len, char c)
null
as a zero-length string (ie,
returned string will just consist of padding).public static String padRight(String str, int len, char c)
null
as a zero-length string (ie,
returned string will just consist of padding).public static boolean contains(String str, String segment)
Returns true
if the second string is an empty string
or the two strings are equal. Returns false
if either
of the strings are null
. Does not care how many times
the second string appears in the first; only that it appears.
public static boolean containsIgnoreCase(String str, String segment)
Returns true
if the second string is an empty string
or the two strings are equal. Returns false
if either
of the strings are null
. Does not care how many times
the second string appears in the first; only that it appears.
public static String repeat(char c, int count)
public static byte[] toUTF8(String str)
If passed null
, returns an empty array.
public static String fromUTF8(byte[] bytes)
If passed null
, returns an empty string.
public static String unicodeEscape(String src)
If passed null
, returns null
.
public static String unescape(String src)
public static int parseDigit(char c, int base)
public static String intern(String str)
StringCanon
.
This is often more useful than creating a task-specific instance.public static String randomString(String chars, int minLength, int maxLength)
Warning: not threadsafe; uses a shared instance of java.util.Random
.
chars
- Defines the set of characters used to create the
returned string.minLength
- Minimum length of the returned string.maxLength
- Maximum length of the returned string.public static String randomAlphaString(int minLength, int maxLength)
Warning: not threadsafe; uses a shared instance of java.util.Random
.
minLength
- Minimum length of the returned string.maxLength
- Maximum length of the returned string.public static String extractLeft(String source, String target)
This method is used to divide a string based on an optional delimiter; for example, dividing "host:port" at ":".
See also extractLeftOfLast(java.lang.String, java.lang.String)
.
public static String extractRight(String source, String target)
This method is used to divide a string based on an optional delimiter; for example, dividing "host:port" at ":".
See also extractRightOfLast(java.lang.String, java.lang.String)
.
public static String extractLeftOfLast(String source, String target)
This method is used to incrementally partition a string, for example, extracting components from a filepath (eg: "/foo/bar/baz.txt", with a target of "/" results in "/foo/bar").
public static String extractRightOfLast(String source, String target)
This method is used to incrementally partition a string, for example, extracting components from a filepath (eg: "/foo/bar/baz.txt", with a target of "/" results in "baz.txt").
public static String substr(String src, int off, int len)
src
- The source string; null
is treated as an
empty string.off
- The starting offset of the substring. If negative, offset is
measured from the end of the string (where -1 is the last
character).len
characters.public static boolean isIn(String str, String... targets)
true
if the passed string is equal to one of
the target strings, false
otherwise. This will typically
be invoked with a variable str
and literal values for
target
.