de.matthiasmann.twl.utils
Class TextUtil

java.lang.Object
  extended by de.matthiasmann.twl.utils.TextUtil

public final class TextUtil
extends java.lang.Object

Utilities for handling texts


Field Summary
static int MAX_ROMAN_INTEGER
          The largest number which can be converted into a Roman number.
 
Method Summary
static int countElements(java.lang.String str)
          Counts the comma separated elements.
static int countNumLines(java.lang.CharSequence str)
          Counts the number of lines in the text.
static java.lang.String createString(char ch, int len)
           
static int indexOf(java.lang.CharSequence cs, char ch, int start)
          Searches for a specific character.
static int indexOf(java.lang.CharSequence cs, char ch, int start, int end)
          Searches for a specific character.
static int indexOf(java.lang.String str, char ch, int start)
          Searches for a specific character.
static boolean isInteger(java.lang.String str)
          Checks if the passed string is an integer.
static java.lang.String limitStringLength(java.lang.String str, int length)
           
static java.lang.String notNull(java.lang.String str)
           
static int[] parseIntArray(java.lang.String str)
          Parse a list of comma separated integers.
static int skipSpaces(java.lang.CharSequence s, int start)
           
static int skipSpaces(java.lang.CharSequence s, int start, int end)
           
static java.lang.String stripNewLines(java.lang.String str)
           
static java.lang.String toCharListNumber(int value, java.lang.String list)
          Create a string which represents the specified value using the specified list of characters.
static java.lang.String toPrintableString(char ch)
          Converts the specified character to a string.
static java.lang.String toRomanNumberString(int value)
          Creates an upper case Roman number string for the given value.
static java.lang.String trim(java.lang.CharSequence s, int start)
          Returns a whitespace trimmed substring.
static java.lang.String trim(java.lang.CharSequence s, int start, int end)
          Returns a whitespace trimmed substring.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAX_ROMAN_INTEGER

public static final int MAX_ROMAN_INTEGER
The largest number which can be converted into a Roman number.

See Also:
toRomanNumberString(int), Constant Field Values
Method Detail

countNumLines

public static int countNumLines(java.lang.CharSequence str)
Counts the number of lines in the text. Lines are split with '\n'

Parameters:
str - the text to count lines in
Returns:
the number of lines - 0 for an empty string

stripNewLines

public static java.lang.String stripNewLines(java.lang.String str)

limitStringLength

public static java.lang.String limitStringLength(java.lang.String str,
                                                 int length)

notNull

public static java.lang.String notNull(java.lang.String str)

indexOf

public static int indexOf(java.lang.CharSequence cs,
                          char ch,
                          int start)
Searches for a specific character.

Parameters:
cs - the CharSequence to search in
ch - the character to search
start - the start index. must be >= 0.
Returns:
the index of the character or cs.length().

indexOf

public static int indexOf(java.lang.CharSequence cs,
                          char ch,
                          int start,
                          int end)
Searches for a specific character.

Parameters:
cs - the CharSequence to search in
ch - the character to search
start - the start index. must be >= 0.
end - the end index. must be >= start and <= cs.length.
Returns:
the index of the character or end.

indexOf

public static int indexOf(java.lang.String str,
                          char ch,
                          int start)
Searches for a specific character.

Parameters:
str - the String to search in
ch - the character to search
start - the start index. must be >= 0.
Returns:
the index of the character or str.length().

skipSpaces

public static int skipSpaces(java.lang.CharSequence s,
                             int start)

skipSpaces

public static int skipSpaces(java.lang.CharSequence s,
                             int start,
                             int end)

trim

public static java.lang.String trim(java.lang.CharSequence s,
                                    int start)
Returns a whitespace trimmed substring. This method is mostly equivant to
s.subSequence(start).toString().trim() 

Parameters:
s - the sequence
start - the start index (inclusive)
Returns:
the sub string without leading or trailing whitespace
See Also:
Character.isWhitespace(char)

trim

public static java.lang.String trim(java.lang.CharSequence s,
                                    int start,
                                    int end)
Returns a whitespace trimmed substring. This method is mostly equivant to
s.subSequence(start, end).toString().trim() 

Parameters:
s - the sequence
start - the start index (inclusive)
end - the end index (exclusive)
Returns:
the sub string without leading or trailing whitespace
See Also:
Character.isWhitespace(char)

createString

public static java.lang.String createString(char ch,
                                            int len)

parseIntArray

public static int[] parseIntArray(java.lang.String str)
                           throws java.lang.NumberFormatException
Parse a list of comma separated integers. No space is allowed between comma and numbers

Parameters:
str - the string to parse
Returns:
the parsed integer array. Contains at least 1 element.
Throws:
java.lang.NumberFormatException - if the string could not be parsed

isInteger

public static boolean isInteger(java.lang.String str)
Checks if the passed string is an integer.

It checks the following regular expression:

-?[0-9]+

Parameters:
str - the string to check
Returns:
true if the string matches the above condition

countElements

public static int countElements(java.lang.String str)
Counts the comma separated elements.

Parameters:
str - the string to analyze
Returns:
the number of comma separated parts. Always >= 1.

toPrintableString

public static java.lang.String toPrintableString(char ch)
Converts the specified character to a string. If the character is an Character.isISOControl(char) then it's unicode value returned as octal escape code: "\xxx"

Parameters:
ch - the character
Returns:
the string

toRomanNumberString

public static java.lang.String toRomanNumberString(int value)
                                            throws java.lang.IllegalArgumentException
Creates an upper case Roman number string for the given value. Values above 3999 need characters from unicode block "Number Forms" to be displayed correctly.

Parameters:
value - the value. Must be >= 1 and <= MAX_ROMAN_INTEGER
Returns:
the Roman number string in upper case
Throws:
java.lang.IllegalArgumentException - if the value is out of range

toCharListNumber

public static java.lang.String toCharListNumber(int value,
                                                java.lang.String list)
                                         throws java.lang.IllegalArgumentException
Create a string which represents the specified value using the specified list of characters.

Parameters:
value - the value. Must be >= 1.
list - the character list to use. Must contain at least 4 characters.
Returns:
the formated number
Throws:
java.lang.IllegalArgumentException - if the value is < 1