com.jgoodies.common.base

Class Strings

public class Strings extends Object

Provides frequently used static null-safe String testing methods .

Version: $Revision: 1.5 $

Author: Karsten Lentzsch

Constructor Summary
protected Strings()
Method Summary
static StringabbreviateCenter(String str, int maxLength)
Abbreviates the given string if it exceeds the given maximum length by replacing its center part with an ellipsis ('…').
static booleanisBlank(String str)
Checks if the given string is whitespace, empty ("") or {@code null}.
static booleanisEmpty(String str)
Checks if the given string is empty ("") or {@code null}.
static booleanisNotBlank(String str)
Checks if the given string is not empty (""), not {@code null} and not whitespace only.
static booleanisNotEmpty(String str)
Checks if the given string is not empty ("") and not {@code null}.
static booleanstartsWithIgnoreCase(String str, String prefix)
Checks if {@code str} starts with the given prefix ignoring cases. {@code null} is handled safely; if both arguments are null, true is returned, false otherwise.

Constructor Detail

Strings

protected Strings()

Method Detail

abbreviateCenter

public static String abbreviateCenter(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 is {@code null} 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 string maxLength the maximum length of the result string

Returns: {@code str} if its length is less than or equal to {@code maxLength}, an abbreviated string with length {@code maxLength} where the center is replaced by an ellipsis

isBlank

public static boolean isBlank(String str)
Checks if the given string is whitespace, empty ("") or {@code null}.
 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 be {@code null}

Returns: {@code true} if the string is whitespace, empty or {@code null}

See Also: isEmpty

isEmpty

public static boolean isEmpty(String str)
Checks if the given string is empty ("") or {@code null}.
 Strings.isEmpty(null)  == true
 Strings.isEmpty("")    == true
 Strings.isEmpty(" ")   == false
 Strings.isEmpty("Hi ") == false
 

Parameters: str the string to check, may be {@code null}

Returns: {@code true} if the string is empty or {@code null}

See Also: isBlank

isNotBlank

public static boolean isNotBlank(String str)
Checks if the given string is not empty (""), not {@code null} 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 be {@code null}

Returns: {@code true} if the string is not empty and not {@code null} and not whitespace only

See Also: isEmpty

isNotEmpty

public static boolean isNotEmpty(String str)
Checks if the given string is not empty ("") and not {@code null}.
 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 be {@code null}

Returns: {@code true} if the string is not empty and not {@code null}

See Also: isBlank

startsWithIgnoreCase

public static boolean startsWithIgnoreCase(String str, String prefix)
Checks if {@code str} starts with the given prefix ignoring cases. {@code 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 null prefix the prefix to check for, may be null

Returns: {@code true}, if the string starts with the prefix, ignoring cases, {@code false} otherwise

See Also: String#startsWith(java.lang.String)

Copyright © 2009-2010 JGoodies Karsten Lentzsch. All Rights Reserved.