com.jdroid.java.utils
Class StringUtils

java.lang.Object
  extended by com.jdroid.java.utils.StringUtils

public abstract class StringUtils
extends Object

This class contains functions for managing Strings


Field Summary
static String BANG
           
static String COMMA
           
static String DASH
           
static String DOT
           
static String ELLIPSIS
           
static String EMPTY
           
static String NEW_LINE
           
static String PIPE
           
static String SLASH
           
static String SPACE
           
static String UNDERSCORE
           
 
Constructor Summary
StringUtils()
           
 
Method Summary
static String capitalize(String text)
           
static String capitalizeWords(String str)
           Capitalizes all the whitespace separated words in a String.
static String capitalizeWords(String str, char[] delimiters)
           Capitalizes all the delimiter separated words in a String.
static String defaultString(String str)
           Returns either the passed in String, or if the String is null, an empty String ("").
static String defaultString(String str, String defaultString)
           
static Boolean equal(String text1, String text2)
           
static Set<String> extractPlaceHolders(String string)
          Extract all the placeholder's names of the string
static String getFirstToken(String string)
          Returns the first token of the string if that string can be split by a ",", else return the unmodified input string
static String getFirstToken(String string, String token)
          Returns the first token of the string if that string can be split by the token, else return the unmodified input string
static String getNotEmptyString(String text)
           
static boolean hasOnlyCharacters(String name)
           
static boolean isBlank(String str)
           Checks if a String is whitespace, empty ("") or null.
static Boolean isEmpty(String text)
           
static boolean isNotBlank(String str)
           Checks if a String is not empty (""), not null and not whitespace only.
static Boolean isNotEmpty(String text)
           
static String join(Collection<?> objectsToJoin)
          Joins all the strings in the list in a single one separated by ','.
static String join(Collection<?> objectsToJoin, String separator)
          Joins all the strings in the list in a single one separated by the separator sequence.
static String replaceValues(String template, Map<String,String> values)
           
static Collection<String> splitToCollection(String text)
           
static Collection<String> splitToCollection(String text, String separator)
           
static String toAlphanumeric(String value)
          Transform the received value removing all the not alphanumeric or spaces characters
static String truncate(String text, Integer maxCharacters)
          Truncate the text adding "...".
static String truncate(String text, Integer maxCharacters, Boolean truncateWords)
          Truncate the text adding "..." if the truncateWords parameter is true.
static String wordWrapToTwoLines(String text, int minLength)
          This method word wrap a text to two lines if the text has multiple words and its length is greater than the specified minLength.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EMPTY

public static final String EMPTY
See Also:
Constant Field Values

ELLIPSIS

public static final String ELLIPSIS
See Also:
Constant Field Values

COMMA

public static final String COMMA
See Also:
Constant Field Values

SPACE

public static final String SPACE
See Also:
Constant Field Values

DASH

public static final String DASH
See Also:
Constant Field Values

SLASH

public static final String SLASH
See Also:
Constant Field Values

DOT

public static final String DOT
See Also:
Constant Field Values

NEW_LINE

public static final String NEW_LINE
See Also:
Constant Field Values

UNDERSCORE

public static final String UNDERSCORE
See Also:
Constant Field Values

BANG

public static final String BANG
See Also:
Constant Field Values

PIPE

public static final String PIPE
See Also:
Constant Field Values
Constructor Detail

StringUtils

public StringUtils()
Method Detail

getNotEmptyString

public static String getNotEmptyString(String text)

equal

public static Boolean equal(String text1,
                            String text2)

isEmpty

public static Boolean isEmpty(String text)

isNotEmpty

public static Boolean isNotEmpty(String text)

isBlank

public static boolean isBlank(String str)

Checks if a String is whitespace, empty ("") or null.

 StringUtils.isBlank(null)      = true
 StringUtils.isBlank("")        = true
 StringUtils.isBlank(" ")       = true
 StringUtils.isBlank("bob")     = false
 StringUtils.isBlank("  bob  ") = false
 

Parameters:
str - the String to check, may be null
Returns:
true if the String is null, empty or whitespace

isNotBlank

public static boolean isNotBlank(String str)

Checks if a String is not empty (""), not null and not whitespace only.

 StringUtils.isNotBlank(null)      = false
 StringUtils.isNotBlank("")        = false
 StringUtils.isNotBlank(" ")       = false
 StringUtils.isNotBlank("bob")     = true
 StringUtils.isNotBlank("  bob  ") = true
 

Parameters:
str - the String to check, may be null
Returns:
true if the String is not empty and not null and not whitespace

defaultString

public static String defaultString(String str)

Returns either the passed in String, or if the String is null, an empty String ("").

 StringUtils.defaultString(null)  = ""
 StringUtils.defaultString("")    = ""
 StringUtils.defaultString("bat") = "bat"
 

Parameters:
str - the String to check, may be null
Returns:
the passed in String, or the empty String if it was null

defaultString

public static String defaultString(String str,
                                   String defaultString)

capitalize

public static String capitalize(String text)

capitalizeWords

public static String capitalizeWords(String str)

Capitalizes all the whitespace separated words in a String. Only the first letter of each word is changed. To convert the rest of each word to lowercase at the same time, use capitalize(String).

Whitespace is defined by Character.isWhitespace(char). A null input String returns null. Capitalization uses the unicode title case, normally equivalent to upper case.

 WordUtils.capitalize(null)        = null
 WordUtils.capitalize("")          = ""
 WordUtils.capitalize("i am FINE") = "I Am FINE"
 

Parameters:
str - the String to capitalize, may be null
Returns:
capitalized String, null if null String input

capitalizeWords

public static String capitalizeWords(String str,
                                     char[] delimiters)

Capitalizes all the delimiter separated words in a String. Only the first letter of each word is changed.

The delimiters represent a set of characters understood to separate words. The first string character and the first non-delimiter character after a delimiter will be capitalized.

A null input String returns null. Capitalization uses the unicode title case, normally equivalent to upper case.

 WordUtils.capitalize(null, *)            = null
 WordUtils.capitalize("", *)              = ""
 WordUtils.capitalize(*, new char[0])     = *
 WordUtils.capitalize("i am fine", null)  = "I Am Fine"
 WordUtils.capitalize("i aM.fine", {'.'}) = "I aM.Fine"
 

Parameters:
str - the String to capitalize, may be null
delimiters - set of characters to determine capitalization, null means whitespace
Returns:
capitalized String, null if null String input

join

public static String join(Collection<?> objectsToJoin,
                          String separator)
Joins all the strings in the list in a single one separated by the separator sequence.

Parameters:
objectsToJoin - The objects to join.
separator - The separator sequence.
Returns:
The joined strings.

join

public static String join(Collection<?> objectsToJoin)
Joins all the strings in the list in a single one separated by ','.

Parameters:
objectsToJoin - The objects to join.
Returns:
The joined strings.

truncate

public static String truncate(String text,
                              Integer maxCharacters,
                              Boolean truncateWords)
Truncate the text adding "..." if the truncateWords parameter is true. The ellipsis will be taken into account when counting the amount of characters.

Parameters:
text - The text to truncate
maxCharacters - The maximum amount of characters allowed for the returned text
truncateWords - True if the words should be truncated
Returns:
The truncated text

truncate

public static String truncate(String text,
                              Integer maxCharacters)
Truncate the text adding "...". The ellipsis will be taken into account when counting the amount of characters.

Parameters:
text - The text to truncate
maxCharacters - The maximum amount of characters allowed for the returned text
Returns:
The truncated text

extractPlaceHolders

public static Set<String> extractPlaceHolders(String string)
Extract all the placeholder's names of the string

Parameters:
string - The whole string with placeholders
Returns:
A set with all the placeholder's names

toAlphanumeric

public static String toAlphanumeric(String value)
Transform the received value removing all the not alphanumeric or spaces characters

Parameters:
value - The string to transform
Returns:
The transformed string

splitToCollection

public static Collection<String> splitToCollection(String text)

splitToCollection

public static Collection<String> splitToCollection(String text,
                                                   String separator)

getFirstToken

public static String getFirstToken(String string,
                                   String token)
Returns the first token of the string if that string can be split by the token, else return the unmodified input string

Parameters:
string - The string to split
token - The token to use as splitter
Returns:
The resultant string

getFirstToken

public static String getFirstToken(String string)
Returns the first token of the string if that string can be split by a ",", else return the unmodified input string

Parameters:
string - The string to split
Returns:
The resultant string

wordWrapToTwoLines

public static String wordWrapToTwoLines(String text,
                                        int minLength)
This method word wrap a text to two lines if the text has multiple words and its length is greater than the specified minLength. To do the word wrap, the white space closest to the middle of the string is replaced by a "\n" character

Parameters:
text - A Sting, the text to word wrap to two lines.
minLength - The min text length to appy word wrap.
Returns:
The input text word wrapped to two lines or the original text.

replaceValues

public static String replaceValues(String template,
                                   Map<String,String> values)

hasOnlyCharacters

public static boolean hasOnlyCharacters(String name)


Copyright © 2014. All rights reserved.