com.silverpeas.util
Class StringUtil

java.lang.Object
  extended by org.apache.commons.lang3.StringUtils
      extended by com.silverpeas.util.StringUtil

public class StringUtil
extends org.apache.commons.lang3.StringUtils


Field Summary
static String newline
           
 
Fields inherited from class org.apache.commons.lang3.StringUtils
EMPTY, INDEX_NOT_FOUND
 
Method Summary
static boolean areStringEquals(String s1, String s2)
          Indicates if two Strings are equals, managing null.
static String asBase64(byte[] binaryData)
          Encodes the specified binary data into a text of Base64 characters.
static String asHex(byte[] binaryData)
          Encodes the specified binary data into a String representing the hexadecimal values of each byte in order.
static float convertFloat(String value)
           
static String convertToEncoding(String toConvert, String encoding)
           
static String defaultStringIfNotDefined(String string)
          Returns either the passed in String, or if the String is not defined, an empty String ("").
static String defaultStringIfNotDefined(String string, String defaultString)
          Returns either the passed in String, or if the String is not defined, the value of defaultString.
static String detectEncoding(byte[] data, String declaredEncoding)
          Method for trying to detect encoding
static Set<String> detectMaybeEncoding(byte[] data, String declaredEncoding)
          Method for trying to detect encoding
static String detectStringEncoding(byte[] data, String declaredEncoding)
          Method for trying to detect encoding
static String escapeQuote(String text)
           
static float floatValue(String value)
          Parse a String into a float using the default locale.
static float floatValue(String value, String language)
          Parse a String into a float using the specified locale.
static String format(String label, Map<String,?> values)
          Format a string by extending the principle of the the method format() of the class java.text.MessageFormat to string arguments.
static byte[] fromBase64(String base64Text)
          Decodes the specified text with Base64 characters in binary.
static byte[] fromHex(String hexText)
          Decodes the specified text with hexadecimal values in bytes of those same values.
static boolean getBooleanValue(String expression)
          Evaluate the expression and return true if expression equals "true", "yes", "y", "1" or "oui".
static boolean isDefined(String parameter)
           
static boolean isFloat(String id)
           
static boolean isInteger(String id)
           
static boolean isLong(String id)
           
static boolean isNotDefined(String parameter)
           
static boolean isValidEmailAddress(String aEmailAddress)
          Validate the form of an email address.
static boolean isValidHour(String time)
           
static String regexReplace(String source, String regex, String replacement)
          Replace parts of a text by an one replacement string.
static Iterable<String> splitString(String str)
          Splits the provided text into an array, using whitespace as the separator.
static Iterable<String> splitString(String str, char separatorChar)
          Splits the provided text into an array, separator specified.
static String toAcceptableFilename(String name)
          Replaces
static String truncate(String text, int maxLength)
           
 
Methods inherited from class org.apache.commons.lang3.StringUtils
abbreviate, abbreviate, abbreviateMiddle, capitalize, center, center, center, chomp, chomp, chop, contains, contains, containsAny, containsAny, containsIgnoreCase, containsNone, containsNone, containsOnly, containsOnly, containsWhitespace, countMatches, defaultIfBlank, defaultIfEmpty, defaultString, defaultString, deleteWhitespace, difference, endsWith, endsWithAny, endsWithIgnoreCase, equals, equalsIgnoreCase, getCommonPrefix, getLevenshteinDistance, getLevenshteinDistance, indexOf, indexOf, indexOf, indexOf, indexOfAny, indexOfAny, indexOfAny, indexOfAnyBut, indexOfAnyBut, indexOfDifference, indexOfDifference, indexOfIgnoreCase, indexOfIgnoreCase, isAllLowerCase, isAllUpperCase, isAlpha, isAlphanumeric, isAlphanumericSpace, isAlphaSpace, isAsciiPrintable, isBlank, isEmpty, isNotBlank, isNotEmpty, isNumeric, isNumericSpace, isWhitespace, join, join, join, join, join, join, join, join, join, lastIndexOf, lastIndexOf, lastIndexOf, lastIndexOf, lastIndexOfAny, lastIndexOfIgnoreCase, lastIndexOfIgnoreCase, lastOrdinalIndexOf, left, leftPad, leftPad, leftPad, length, lowerCase, lowerCase, mid, normalizeSpace, ordinalIndexOf, overlay, remove, remove, removeEnd, removeEndIgnoreCase, removeStart, removeStartIgnoreCase, repeat, repeat, repeat, replace, replace, replaceChars, replaceChars, replaceEach, replaceEachRepeatedly, replaceOnce, reverse, reverseDelimited, right, rightPad, rightPad, rightPad, split, split, split, split, splitByCharacterType, splitByCharacterTypeCamelCase, splitByWholeSeparator, splitByWholeSeparator, splitByWholeSeparatorPreserveAllTokens, splitByWholeSeparatorPreserveAllTokens, splitPreserveAllTokens, splitPreserveAllTokens, splitPreserveAllTokens, splitPreserveAllTokens, startsWith, startsWithAny, startsWithIgnoreCase, strip, strip, stripAccents, stripAll, stripAll, stripEnd, stripStart, stripToEmpty, stripToNull, substring, substring, substringAfter, substringAfterLast, substringBefore, substringBeforeLast, substringBetween, substringBetween, substringsBetween, swapCase, toString, trim, trimToEmpty, trimToNull, uncapitalize, upperCase, upperCase
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

newline

public static String newline
Method Detail

isDefined

public static boolean isDefined(String parameter)

isNotDefined

public static boolean isNotDefined(String parameter)

defaultStringIfNotDefined

public static String defaultStringIfNotDefined(String string)

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

 StringUtil.defaultStringIfNotDefined(null)   = ""
 StringUtil.defaultStringIfNotDefined("")     = ""
 StringUtil.defaultStringIfNotDefined("    ") = ""
 StringUtil.defaultStringIfNotDefined("bat")  = "bat"
 

Parameters:
string - the String to check, may be null, blank or filled by spaces if the input is not defined, may be null, blank or filled by spaces
Returns:
the passed in String, or the default if it was null
See Also:
isNotDefined(String), StringUtils.defaultString(String, String)

defaultStringIfNotDefined

public static String defaultStringIfNotDefined(String string,
                                               String defaultString)

Returns either the passed in String, or if the String is not defined, the value of defaultString.

 StringUtil.defaultStringIfNotDefined(null, "NULL")   = "NULL"
 StringUtil.defaultStringIfNotDefined("", "NULL")     = "NULL"
 StringUtil.defaultStringIfNotDefined("    ", "NULL") = "NULL"
 StringUtil.defaultStringIfNotDefined("bat", "NULL")  = "bat"
 

Parameters:
string - the String to check, may be null, blank or filled by spaces
defaultString - the default String to return if the input is not defined, may be null, blank or filled by spaces
Returns:
the passed in String, or the default if it was null
See Also:
isNotDefined(String), StringUtils.defaultString(String, String)

isInteger

public static boolean isInteger(String id)

isLong

public static boolean isLong(String id)

convertFloat

public static float convertFloat(String value)

isFloat

public static boolean isFloat(String id)

escapeQuote

public static String escapeQuote(String text)
Parameters:
text -
Returns:
a String with all quotes replaced by spaces

toAcceptableFilename

public static String toAcceptableFilename(String name)
Replaces

Parameters:
name -
Returns:
a String with all quotes replaced by spaces

format

public static String format(String label,
                            Map<String,?> values)
Format a string by extending the principle of the the method format() of the class java.text.MessageFormat to string arguments. For instance, the string '{key}' contained in the original string to format will be replaced by the value corresponding to this key contained into the values map.

Parameters:
label - The string to format
values - The values to insert into the string
Returns:
The formatted string, filled with values of the map.

truncate

public static String truncate(String text,
                              int maxLength)
Parameters:
text - The string to truncate if its size is greater than the maximum length given as parameter.
maxLength - The maximum length required.
Returns:
The truncated string followed by '...' if needed. Returns the string itself if its length is smaller than the required maximum length.

regexReplace

public static String regexReplace(String source,
                                  String regex,
                                  String replacement)
Replace parts of a text by an one replacement string. The text to replace is specified by a regex.

Parameters:
source - the original text
regex - the regex that permits to identify parts of text to replace
replacement - the replacement text
Returns:
The source text modified

isValidEmailAddress

public static boolean isValidEmailAddress(String aEmailAddress)
Validate the form of an email address.

Return true only if


isValidHour

public static boolean isValidHour(String time)

convertToEncoding

public static String convertToEncoding(String toConvert,
                                       String encoding)

getBooleanValue

public static boolean getBooleanValue(String expression)
Evaluate the expression and return true if expression equals "true", "yes", "y", "1" or "oui".

Parameters:
expression - the expression to be evaluated
Returns:
true if expression equals "true", "yes", "y", "1" or "oui".

detectEncoding

public static String detectEncoding(byte[] data,
                                    String declaredEncoding)
Method for trying to detect encoding

Parameters:
data - some data to try to detect the encoding.
declaredEncoding - expected encoding.
Returns:

detectStringEncoding

public static String detectStringEncoding(byte[] data,
                                          String declaredEncoding)
                                   throws UnsupportedEncodingException
Method for trying to detect encoding

Parameters:
data - some data to try to detect the encoding.
declaredEncoding - expected encoding.
Returns:
Throws:
UnsupportedEncodingException

detectMaybeEncoding

public static Set<String> detectMaybeEncoding(byte[] data,
                                              String declaredEncoding)
Method for trying to detect encoding

Parameters:
data - some data to try to detect the encoding.
declaredEncoding - expected encoding.
Returns:

areStringEquals

public static boolean areStringEquals(String s1,
                                      String s2)
Indicates if two Strings are equals, managing null.

Parameters:
s1 - the first String.
s2 - the second String.
Returns:
true ifthe two Strings are equals.

floatValue

public static float floatValue(String value)
                        throws ParseException
Parse a String into a float using the default locale.

Parameters:
value - the string to be parsed into a float.
Returns:
the float value.
Throws:
ParseException

floatValue

public static float floatValue(String value,
                               String language)
                        throws ParseException
Parse a String into a float using the specified locale.

Parameters:
value - the string to be parsed into a float
language - the language for defining the locale
Returns:
the float value.
Throws:
ParseException

asBase64

public static String asBase64(byte[] binaryData)
Encodes the specified binary data into a text of Base64 characters.

Parameters:
binaryData - the binary data to convert in Base64-based String.
Returns:
a String representation of the binary data in Base64 characters.

fromBase64

public static byte[] fromBase64(String base64Text)
Decodes the specified text with Base64 characters in binary.

Parameters:
base64Text - the text in Base64.
Returns:
the binary representation of the text.

asHex

public static String asHex(byte[] binaryData)
Encodes the specified binary data into a String representing the hexadecimal values of each byte in order. The String is in the UTF-8 charset.

Parameters:
binaryData - the binary data to concert in hexadecimal-based String.
Returns:
a String representation of the binary data in Hexadecimal characters.

fromHex

public static byte[] fromHex(String hexText)
                      throws ParseException
Decodes the specified text with hexadecimal values in bytes of those same values. The text is considered to be in the UTF-8 charset.

Parameters:
hexText - the text with hexadecimal-based characters.
Returns:
the binary representation of the text.
Throws:
ParseException - if an odd number or illegal of characters is supplied.

splitString

public static Iterable<String> splitString(String str)

Splits the provided text into an array, using whitespace as the separator. Whitespace is defined by Character.isWhitespace(char).

The separator is not included in the returned String array. Adjacent separators are treated as one separator. For more control over the split use the StrTokenizer class.

A null input String returns null.

 StringUtils.split(null)       = null
 StringUtils.split("")         = empty list
 StringUtils.split("abc def")  = ["abc", "def"]
 StringUtils.split("abc  def") = ["abc", "def"]
 StringUtils.split(" abc ")    = ["abc"]
 

Parameters:
str - the String to parse, may be null
Returns:
an array of parsed Strings, null if null String input

splitString

public static Iterable<String> splitString(String str,
                                           char separatorChar)

Splits the provided text into an array, separator specified. This is an alternative to using StringTokenizer.

The separator is not included in the returned String array. Adjacent separators are treated as one separator. For more control over the split use the StrTokenizer class.

A null input String returns null.

 StringUtils.split(null, *)         = null
 StringUtils.split("", *)           = empty list
 StringUtils.split("a.b.c", '.')    = ["a", "b", "c"]
 StringUtils.split("a..b.c", '.')   = ["a", "b", "c"]
 StringUtils.split("a:b:c", '.')    = ["a:b:c"]
 StringUtils.split("a b c", ' ')    = ["a", "b", "c"]
 

Parameters:
str - the String to parse, may be null
separatorChar - the character used as the delimiter
Returns:
an array of parsed Strings, null if null String input


Copyright © 2016 Silverpeas. All Rights Reserved.