Package com.jgoodies.common.base
Class Strings
- java.lang.Object
-
- com.jgoodies.common.base.Strings
-
public class Strings extends java.lang.Object
Provides frequently used static null-safe String testing methods .
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
ELLIPSIS_STRING
The correct ellipsis string.static java.lang.String
NO_ELLIPSIS_STRING
A string with three dots that should is often meant to be the ellipsis string "…" or character '…'.
-
Constructor Summary
Constructors Modifier Constructor Description protected
Strings()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.String
abbreviateCenter(java.lang.String str, int maxLength)
Abbreviates the given string if it exceeds the given maximum length by replacing its center part with an ellipsis ('…').static java.lang.String
get(java.lang.String str, java.lang.Object... args)
If no arguments are provided, the plain String is returned.static boolean
isBlank(java.lang.String str)
Checks if the given string is whitespace, empty ("") ornull
.static boolean
isEmpty(java.lang.String str)
Checks if the given string is empty ("") ornull
.static boolean
isNotBlank(java.lang.String str)
Checks if the given string is not empty (""), notnull
and not whitespace only.static boolean
isNotEmpty(java.lang.String str)
Checks if the given string is not empty ("") and notnull
.static boolean
isTrimmed(java.lang.String str)
Checks if the given string isnull
, empty (""), or the first and last characters are not whitespace.static boolean
startsWithIgnoreCase(java.lang.String str, java.lang.String prefix)
Checks ifstr
starts with the given prefix ignoring cases.
-
-
-
Field Detail
-
NO_ELLIPSIS_STRING
public static final java.lang.String NO_ELLIPSIS_STRING
A string with three dots that should is often meant to be the ellipsis string "…" or character '…'.- Since:
- 1.7
- See Also:
ELLIPSIS_STRING
, Constant Field Values
-
ELLIPSIS_STRING
public static final java.lang.String ELLIPSIS_STRING
The correct ellipsis string.- See Also:
NO_ELLIPSIS_STRING
, Constant Field Values
-
-
Method Detail
-
isBlank
public static boolean isBlank(java.lang.String str)
Checks if the given string is whitespace, empty ("") ornull
.Strings.isBlank(null) == true Strings.isBlank("") == true Strings.isBlank(" ") == true Strings.isBlank(" abc") == false Strings.isBlank("abc ") == false Strings.isBlank(" abc ") == false
- Parameters:
str
- the string to check, may benull
- Returns:
true
if the string is whitespace, empty ornull
- See Also:
isEmpty(String)
-
isNotBlank
public static boolean isNotBlank(java.lang.String str)
Checks if the given string is not empty (""), notnull
and not whitespace only.Strings.isNotBlank(null) == false Strings.isNotBlank("") == false Strings.isNotBlank(" ") == false Strings.isNotBlank(" abc") == true Strings.isNotBlank("abc ") == true Strings.isNotBlank(" abc ") == true
- Parameters:
str
- the string to check, may benull
- Returns:
true
if the string is not empty and notnull
and not whitespace only- See Also:
isEmpty(String)
-
isEmpty
public static boolean isEmpty(java.lang.String str)
Checks if the given string is empty ("") ornull
.Strings.isEmpty(null) == true Strings.isEmpty("") == true Strings.isEmpty(" ") == false Strings.isEmpty("Hi ") == false
- Parameters:
str
- the string to check, may benull
- Returns:
true
if the string is empty ornull
- See Also:
isBlank(String)
-
isNotEmpty
public static boolean isNotEmpty(java.lang.String str)
Checks if the given string is not empty ("") and notnull
.Strings.isNotEmpty(null) == false Strings.isNotEmpty("") == false Strings.isNotEmpty(" ") == true Strings.isNotEmpty("Hi") == true Strings.isNotEmpty("Hi ") == true
- Parameters:
str
- the string to check, may benull
- Returns:
true
if the string is not empty and notnull
- See Also:
isBlank(String)
-
isTrimmed
public static boolean isTrimmed(java.lang.String str)
Checks if the given string isnull
, empty (""), or the first and last characters are not whitespace.Strings.isTrimmed(null) == true Strings.isTrimmed("") == true Strings.isTrimmed(" ") == false Strings.isTrimmed("Hi") == true Strings.isTrimmed("Hi ") == false Strings.isTrimmed(" Hi") == false
- Parameters:
str
- the string to check, may benull
- Returns:
true
if the string isnull
, empty, or the first and last characters are not whitespace.- Since:
- 1.3
-
startsWithIgnoreCase
public static boolean startsWithIgnoreCase(java.lang.String str, java.lang.String prefix)
Checks ifstr
starts with the given prefix ignoring cases.null
is handled safely; if both arguments are null, true is returned, false otherwise.Strings.startsWithIgnoreCase(null, null) == true Strings.startsWithIgnoreCase("a", null) == false Strings.startsWithIgnoreCase(null, "a") == false Strings.startsWithIgnoreCase("", "") == true Strings.startsWithIgnoreCase(" ", "") == true Strings.startsWithIgnoreCase("John", "J") == true Strings.startsWithIgnoreCase("John", "Jo") == true Strings.startsWithIgnoreCase("John", "Joh") == true Strings.startsWithIgnoreCase("John", "joh") == true Strings.startsWithIgnoreCase("john", "Joh") == true Strings.startsWithIgnoreCase("john", "joh") == true Strings.startsWithIgnoreCase("John", "John") == true Strings.startsWithIgnoreCase("John", "john") == true Strings.startsWithIgnoreCase("John", "Jonny") == false
- Parameters:
str
- the test string to check, may be nullprefix
- the prefix to check for, may be null- Returns:
true
, if the string starts with the prefix, ignoring cases,false
otherwise- See Also:
String.startsWith(java.lang.String)
-
abbreviateCenter
public static java.lang.String abbreviateCenter(java.lang.String str, int maxLength)
Abbreviates the given string if it exceeds the given maximum length by replacing its center part with an ellipsis ('…'). If the string isnull
or shorter than the limit, it is returned as is.Strings.abbreviateCenter(null, 3) == null Strings.abbreviateCenter("", 3) == "" Strings.abbreviateCenter(" ", 3) == " " Strings.abbreviateCenter("a", 3) == "a" Strings.abbreviateCenter("ab", 3) == "ab" Strings.abbreviateCenter("abc", 3) == "abc" Strings.abbreviateCenter("abcd", 3) == "a…d" Strings.abbreviateCenter("abcde", 3) == "a…e" Strings.abbreviateCenter("abcde", 4) == "ab…e" Strings.abbreviateCenter("abcdef", 4) == "ab…f" Strings.abbreviateCenter("abcdefg", 5) == "ab…fg"
- Parameters:
str
- the source stringmaxLength
- the maximum length of the result string- Returns:
str
if its length is less than or equal tomaxLength
, an abbreviated string with lengthmaxLength
where the center is replaced by an ellipsis
-
get
public static java.lang.String get(java.lang.String str, java.lang.Object... args)
If no arguments are provided, the plain String is returned. Otherwise the string will be formatted usingString.format
with the given arguments.Strings.get(null) == null Strings.get("") == "" Strings.get(" ") == " " Strings.get("hello") == "hello" Strings.get("a %s c", "b") == "a b c" Strings.get("%1$s %2$s %3$s", "a", "b", "c") == "a b c"
- Parameters:
args
- optional format arguments forwarded toString#format
- Returns:
- the String formatted with the optional arguments - if any
- Since:
- 1.8
- See Also:
String.format(String, Object...)
-
-