erland.util
Class StringUtil

java.lang.Object
  extended by erland.util.StringUtil

public class StringUtil
extends Object

A helper class for various String and StringBuffer methods

Author:
Erland Isaksson

Constructor Summary
StringUtil()
           
 
Method Summary
static void addFixLength(StringBuffer sb, long number, int length)
          Converts a number to string with a specified length and append it to a StringBuffer, if the number is shorter than the specified length it will be filled out with 0 in the beginning
static void addFixLength(StringBuffer sb, String str, int length, boolean fillAtEnd)
          Converts a String to a String with specified length and append it to a StringBuffer object, if the string is shorter you can choose if it should be filled with spaces in the end or in the beginning
static Boolean asBoolean(String value, Boolean defaultValue)
           
static Date asDate(String value, Date defaultValue)
           
static Date asDate(String value, Date defaultValue, DateFormat format)
           
static Date asDate(String value, Date defaultValue, Locale locale)
           
static Double asDouble(String value, Double defaultValue)
           
static String asEmpty(String str)
          Returns "" if string is null
static Float asFloat(String value, Float defaultValue)
           
static Integer asInteger(String value, Integer defaultValue)
           
static Long asLong(String value, Long defaultValue)
           
static String asNull(String str)
          Returns null if string is null or if String.trim().length()==0
static String asString(Boolean value, String defaultValue)
           
static String asString(Date value, String defaultValue)
           
static String asString(Date value, String defaultValue, DateFormat format)
           
static String asString(Date value, String defaultValue, Locale locale)
           
static String asString(Double value, String defaultValue)
           
static String asString(Float value, String defaultValue)
           
static String asString(Integer value, String defaultValue)
           
static String asString(Long value, String defaultValue)
           
static String beanToString(Object object, String exclude, Class excludeFrom, boolean shortNames)
          Generates a string with all properties of an object which has a getXXX or isXXX method In the case that the methods return a value that is not a String it will be converted to a String by using the toString() method of the object
static String getFixLength(long number, int length)
          Converts a number to a String with specified length, if the number is shorter than the specified length it will be filled out with 0 in the beginning
static String getFixLength(String str, int length, boolean fillAtEnd)
          Converts a String to a String with specified length, if the string is shorter you can choose if it should be filled with spaces in the end or in the beginning
static long getNumber(String str)
          Converts a string to a number
static long getNumber(StringBuffer sb)
          Converts a string to a number
static long getNumber(StringBuffer sb, int beginIndex, int endIndex)
          Converts a sub string between a start and end position to a number
static long getNumber(String str, int beginIndex, int endIndex)
          Converts a sub string between a start and end position to a number
static String objectToString(Object object, String exclude, Class excludeFrom, boolean shortNames)
          Generates a string with all attributes of an object In the case that the methods return a value that is not a String it will be converted to a String by using the toString() method of the object
static StringBuffer replaceChar(StringBuffer sb, char oldChar, char newChar)
          Replace all occurrances of a specifiec character in a StringBuffer with another character
static StringBuffer trim(StringBuffer sb)
          Removes space and newline characters in the beginning and end of a StringBuffer
static String wordWrap(String str, int width)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StringUtil

public StringUtil()
Method Detail

getFixLength

public static String getFixLength(long number,
                                  int length)
Converts a number to a String with specified length, if the number is shorter than the specified length it will be filled out with 0 in the beginning

Parameters:
number - The number to convert
length - The string length requested
Returns:
The number converted to a String object

addFixLength

public static void addFixLength(StringBuffer sb,
                                long number,
                                int length)
Converts a number to string with a specified length and append it to a StringBuffer, if the number is shorter than the specified length it will be filled out with 0 in the beginning

Parameters:
sb - The StringBuffer to append the converted number to
number - The number to convert
length - The string length requested

getFixLength

public static String getFixLength(String str,
                                  int length,
                                  boolean fillAtEnd)
Converts a String to a String with specified length, if the string is shorter you can choose if it should be filled with spaces in the end or in the beginning

Parameters:
str - The string to convert
length - The string length requested
fillAtEnd - Indicates that the string should be filled at end. True = filled at end, False = filled at begining
Returns:
The number converted to a String object

addFixLength

public static void addFixLength(StringBuffer sb,
                                String str,
                                int length,
                                boolean fillAtEnd)
Converts a String to a String with specified length and append it to a StringBuffer object, if the string is shorter you can choose if it should be filled with spaces in the end or in the beginning

Parameters:
sb - The StringBuffer to append the string to
str - The string to convert
length - The string length requested
fillAtEnd - Indicates that the string should be filled at end. True = filled at end, False = filled at begining

getNumber

public static long getNumber(String str)
Converts a string to a number

Parameters:
str - The string containing the number
Returns:
The number created from the string

getNumber

public static long getNumber(String str,
                             int beginIndex,
                             int endIndex)
Converts a sub string between a start and end position to a number

Parameters:
str - The string containg the number
beginIndex - The start position of the sub string
endIndex - The end position of the sub string
Returns:
The number created from the sub string

getNumber

public static long getNumber(StringBuffer sb)
Converts a string to a number

Parameters:
sb - The StringBuffer containing the number
Returns:
The number created from the string

getNumber

public static long getNumber(StringBuffer sb,
                             int beginIndex,
                             int endIndex)
Converts a sub string between a start and end position to a number

Parameters:
sb - The StringBuffer containg the number
beginIndex - The start position of the sub string
endIndex - The end position of the sub string
Returns:
The number created from the sub string

replaceChar

public static StringBuffer replaceChar(StringBuffer sb,
                                       char oldChar,
                                       char newChar)
Replace all occurrances of a specifiec character in a StringBuffer with another character

Parameters:
sb - The StringBuffer containing the string
oldChar - The character to replace
newChar - The new character to insert
Returns:
The StringBuffer object sent as in-parameter sb

trim

public static StringBuffer trim(StringBuffer sb)
Removes space and newline characters in the beginning and end of a StringBuffer

Parameters:
sb - StringBuffer to trim
Returns:
The StringBuffer object sent as in-parameter sb

asNull

public static String asNull(String str)
Returns null if string is null or if String.trim().length()==0

Parameters:
str - The string to check
Returns:
null if str is null or if str.trim().length()==0, else unmodified str

asEmpty

public static String asEmpty(String str)
Returns "" if string is null

Parameters:
str - The string to check
Returns:
"" if str is null else unmodified str

beanToString

public static String beanToString(Object object,
                                  String exclude,
                                  Class excludeFrom,
                                  boolean shortNames)
Generates a string with all properties of an object which has a getXXX or isXXX method In the case that the methods return a value that is not a String it will be converted to a String by using the toString() method of the object

Parameters:
object - The object to print
exclude - A regular expression, if a getXXX or isXXX method matches this it will not be printed
excludeFrom - If the getXXX or isXXX method is declared in this class or a subclass of this class it will not be printed
shortNames - Indicates that short class names should be used instead of full names which include the package
Returns:
A string like MyClass1(attr1=value1,attr2=MyClass2[5],attr3=List(5))

objectToString

public static String objectToString(Object object,
                                    String exclude,
                                    Class excludeFrom,
                                    boolean shortNames)
Generates a string with all attributes of an object In the case that the methods return a value that is not a String it will be converted to a String by using the toString() method of the object

Parameters:
object - The object to print
exclude - A regular expression, if an attribute name matches this it will not be printed
excludeFrom - If the attribute is declared in this class or a subclass of this class it will not be printed
shortNames - Indicates that short class names should be used instead of full names which include the package
Returns:
A string like MyClass1(attr1=value1,attr2=MyClass2[5],attr3=List(5))

asBoolean

public static Boolean asBoolean(String value,
                                Boolean defaultValue)

asInteger

public static Integer asInteger(String value,
                                Integer defaultValue)

asLong

public static Long asLong(String value,
                          Long defaultValue)

asDouble

public static Double asDouble(String value,
                              Double defaultValue)

asFloat

public static Float asFloat(String value,
                            Float defaultValue)

asDate

public static Date asDate(String value,
                          Date defaultValue)

asDate

public static Date asDate(String value,
                          Date defaultValue,
                          Locale locale)

asDate

public static Date asDate(String value,
                          Date defaultValue,
                          DateFormat format)

asString

public static String asString(Integer value,
                              String defaultValue)

asString

public static String asString(Long value,
                              String defaultValue)

asString

public static String asString(Boolean value,
                              String defaultValue)

asString

public static String asString(Float value,
                              String defaultValue)

asString

public static String asString(Double value,
                              String defaultValue)

asString

public static String asString(Date value,
                              String defaultValue)

asString

public static String asString(Date value,
                              String defaultValue,
                              Locale locale)

asString

public static String asString(Date value,
                              String defaultValue,
                              DateFormat format)

wordWrap

public static String wordWrap(String str,
                              int width)


Copyright © 2003-2005 Erland Isaksson. All Rights Reserved.