public final class Strings extends Object
String utilities.| Modifier and Type | Method and Description |
|---|---|
static String |
abbreviate(String str,
int length)
Abbreviates the given
String using "..." so that the returned
String's length is equal to length. |
static String |
abbreviate(String str,
int length,
String ellipsis)
Abbreviates the given
String using the specified ellipsis so
that the returned String's length is equal to length. |
static String |
concat(String... strings)
Concatenates the given
Strings. |
static int |
countOccurrences(String str,
String sub)
Counts the occurrences of the substring
sub in str. |
static int |
distance(String str1,
String str2)
Computes and returns the Damerau-Levenshtein distance between the
given
Strings. |
static String |
emptyToNull(String str)
Returns
null if the given String is empty, returns
the (unmodified) parameter otherwise. |
static boolean |
isBlank(String str)
Returns whether the given
String only contains space, '\n',
'\r' or '\t' characters. |
static boolean |
isNullOrBlank(String str)
Returns whether the given
String is null or only
contains space, '\n', '\r' or '\t' characters. |
static boolean |
isNullOrEmpty(String str)
Returns whether the given
String is null or empty. |
static boolean |
isNullOrWhiteSpace(String str)
Returns whether the given
String is null or only
contains space characters. |
static boolean |
isWhiteSpace(String str)
Returns whether the given
String only contains space
characters. |
static String |
nullToEmpty(String str)
Returns an empty
String if the given one is null, or
returns the (unmodified) parameter otherwise. |
static String |
padLeft(String str,
int len,
char padChar)
Returns a
String of length at least len created by
prepending as many copies of padChar as necessary to reach
that length. |
static String |
padRight(String str,
int len,
char padChar)
Returns a
String of length at least len created by
appending as many copies of padChar as necessary to reach
that length. |
static String |
quote(String str)
Quotes the given
String with double quotes. |
static String |
random(int length,
char... chars)
Returns a pseudo-random
String of the specified size using
characters from the given array. |
static String |
random(int length,
Random rnd,
char... chars)
Returns a random
String using the given characters and the
specified source of randomness. |
static String |
repeat(String str,
int count)
Returns the
String obtained by repeating count times
the given String. |
static String |
reverse(String str)
Reverses the given
String. |
static String |
shuffle(String str)
Randomly permutes the characters from the given
String. |
static String |
shuffle(String str,
Random rnd)
Randomly permutes the characters from the given
String using
the specified source of randomness. |
static String |
strip(String str,
int n)
Returns the new
String obtained by stripping the first and
last n characters from the given one. |
static String |
stripLeft(String str,
int n)
Returns the new
String obtained by stripping the first
n characters from the given one. |
static String |
stripRight(String str,
int n)
Returns the new
String obtained by stripping the last
n characters from the given one. |
static String |
trim(String str)
Returns a copy of the given
String with leading and trailing
whitespaces omitted. |
static String |
trimLeft(String str)
Returns a copy of the given
String with leading whitespaces
omitted. |
static String |
trimRight(String str)
Returns a copy of the given
String with trailing whitespaces
omitted. |
static String |
unquote(String str)
Unquotes the given
String, that is, this method removes any
leading or trailing double quotes. |
public static boolean isBlank(String str)
String only contains space, '\n',
'\r' or '\t' characters.str - the String to test.String is blank.NullPointerException - if str is null.public static boolean isNullOrBlank(String str)
String is null or only
contains space, '\n', '\r' or '\t' characters.str - the String to test.String is null or blank.public static boolean isWhiteSpace(String str)
String only contains space
characters.str - the String to test.String contains only spaces.NullPointerException - if str is null.public static boolean isNullOrWhiteSpace(String str)
String is null or only
contains space characters.str - the String to test.String is null or contains
only spaces.public static boolean isNullOrEmpty(String str)
String is null or empty.str - the String to test.true if str is null or empty,
false otherwise.public static String abbreviate(String str, int length)
String using "..." so that the returned
String's length is equal to length.str - the String to abbreviate.length - the desired length for the abbreviated String.String.NullPointerException - if str is null.IllegalArgumentException - if length is too small.public static String abbreviate(String str, int length, String ellipsis)
String using the specified ellipsis so
that the returned String's length is equal to length.str - the String to abbreviate.length - the desired length for the abbreviated String.ellipsis - the ellipsis to use to mark abbreviation.String.NullPointerException - if one of the arguments is null.IllegalArgumentException - if length is too small.public static String concat(String... strings)
Strings.strings - the Strings to concatenate.String.NullPointerException - if strings is null.public static int distance(String str1, String str2)
Strings. This distance is obtained by counting the
minimum number of operations needed to transform one String
into the other, where an operation is defined as an insertion,
deletion, or substitution of a single character, or a transposition
of two adjacent characters.str1 - the first String.str2 - the second String.str1 and str2.NullPointerException - if one of the arguments is null.public static String emptyToNull(String str)
null if the given String is empty, returns
the (unmodified) parameter otherwise.str - the String to test.null if the given String is empty.public static String nullToEmpty(String str)
String if the given one is null, or
returns the (unmodified) parameter otherwise.str - the String to test.String if the given one is null.public static String random(int length, char... chars)
String of the specified size using
characters from the given array.length - the length of the String to produce.chars - the characters to use to build the String.String.NullPointerException - if chars is null.IllegalArgumentException - if length is negative or if
chars is empty.public static String random(int length, Random rnd, char... chars)
String using the given characters and the
specified source of randomness. All characters have equal likelihood
to appear in the resulting String assuming that the source of
randomness is fair.length - the length of the String to produce.rnd - the source of randomness to use.chars - the characters to use to build the String.String.NullPointerException - if one of the arguments is null.IllegalArgumentException - if length is negative or if
chars is empty.public static String repeat(String str, int count)
String obtained by repeating count times
the given String.str - the String to repeat.count - the number of times the str has to be repeared.String.IllegalArgumentException - if count is negative.NullPointerException - if str is null.public static String reverse(String str)
String.str - the String to reverse.String.NullPointerException - if str is null.public static String shuffle(String str)
String. This
implementation uses the optimized version of the Fisher-Yates shuffle
algorithm (Fisher, Yates, Durstenfeld, Knuth) and thus runs in linear
time.str - the String to be shuffled.String.NullPointerException - if str is null.public static String shuffle(String str, Random rnd)
String using
the specified source of randomness. All permutations occur with equal
likelihood assuming that the source of randomness is fair. This
implementation uses the optimized version of the Fisher-Yates shuffle
algorithm (Fisher, Yates, Durstenfeld, Knuth) and thus runs in linear
time.str - the String to be shuffled.rnd - the source of randomness to use.String.NullPointerException - if one of the arguments is null.public static String strip(String str, int n)
String obtained by stripping the first and
last n characters from the given one.str - the String to be stripped.n - the number of characters to strip.String.NullPointerException - if str is null.IllegalArgumentException - if n is negative.public static String stripLeft(String str, int n)
String obtained by stripping the first
n characters from the given one.str - the String to be stripped.n - the number of characters to strip.String.NullPointerException - if str is null.IllegalArgumentException - if n is negative.public static String stripRight(String str, int n)
String obtained by stripping the last
n characters from the given one.str - the String to be stripped.n - the number of characters to strip.String.NullPointerException - if str is null.IllegalArgumentException - if n is negative.public static String trim(String str)
String with leading and trailing
whitespaces omitted.str - the String to trim.String.NullPointerException - if str is null.public static String trimLeft(String str)
String with leading whitespaces
omitted.str - the String to trim.String.NullPointerException - if str is null.public static String trimRight(String str)
String with trailing whitespaces
omitted.str - the String to trim.String.NullPointerException - if str is null.public static String padLeft(String str, int len, char padChar)
String of length at least len created by
prepending as many copies of padChar as necessary to reach
that length.str - the String to pad.len - the result's minimum length.padChar - the padding character.String.IllegalArgumentException - if len is negative.NullPointerException - if str is null.public static String padRight(String str, int len, char padChar)
String of length at least len created by
appending as many copies of padChar as necessary to reach
that length.str - the String to pad.len - the result's minimum length.padChar - the padding character.String.NullPointerException - if str is null.IllegalArgumentException - if len is negative.public static String quote(String str)
String with double quotes. If the given
String has multiple quotes, only one of them is kept.str - the String to quote.String.NullPointerException - if str is null.public static String unquote(String str)
String, that is, this method removes any
leading or trailing double quotes. If the given String is not
quoted, returns it unmodified.str - the String to unquote.String.NullPointerException - if str is null.public static int countOccurrences(String str, String sub)
sub in str.str - String to search in.sub - String to search for.sub in str.NullPointerException - if one of the arguments is null.Copyright © 2012–2015. All rights reserved.