org.xnap.commons.util
Class StringHelper

java.lang.Object
  extended by org.xnap.commons.util.StringHelper

public class StringHelper
extends Object

Provides a set of static methods that help with string parsing and modifying.


Field Summary
static String ALPHA_NUM
           
static String ALPHABET_LOWER
           
static String ALPHABET_UPPER
           
static String ANYTHING
           
static String EMAIL
           
static String HOST
           
static String MONEY_USD
           
static String NUMBERS_DECIMAL
           
static String NUMBERS_INT
           
static String REGULAR_STRING
           
static String[] SIZES
           
 
Constructor Summary
StringHelper()
           
 
Method Summary
static void append(StringBuffer sb, String text, int count)
          Appends text to sb count times.
static String firstToken(String text, String separator)
          Splits text at the first occurence of separator and returns the left part, excluding the separator.
static String formatLength(long i)
          Formats number of seconds in appropriate time unit
static String formatSize(long size)
          Returns value (byte) formatted as a file size.
static int indexOfDigit(String s)
          Returns the index of the first digit in s.
static String lastPrefix(String text, String separator)
          Splits text at the last occurence of separator and returns the left part, excluding the separator.
static String lastToken(String text, String separator)
          Splits text at the last occurence of separator and returns the right part, excluding the separator.
static String pad(String text, int padding)
          Prepends and appends padding whitespaces to text and returns the result.
static String pad(String text, int lpadding, int rpadding)
          Prepends lpadding and appends rpadding whitespaces to text and returns the result.
static String randomString(int length)
          Returns a random lower case letter-only string with length characters.
static String replaceAll(String s, String oldChars, String newChars)
          Replaces all occurences of oldChars in s by newChars.
static String stripExtra(String s)
          Removes all characters from s that are not letters.
static String[] toArray(String value, String separators)
          Uses a StringTokenizer to split value.
static String toFirstUpper(String value)
          Returns a new String that has the first letter of value set to upper case.
static int[] toIntArray(String value, String separators)
           
static List<String> toList(String value, String separators)
           
static String toString(byte[] data)
          Tries to autodect encoding.
static String toString(Collection c, String separator)
           
static String toString(int[] array, String separator)
           
static String toString(String[] array, String separator)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SIZES

public static final String[] SIZES

ALPHABET_LOWER

public static final String ALPHABET_LOWER
See Also:
Constant Field Values

ALPHABET_UPPER

public static final String ALPHABET_UPPER
See Also:
Constant Field Values

NUMBERS_INT

public static final String NUMBERS_INT
See Also:
Constant Field Values

NUMBERS_DECIMAL

public static final String NUMBERS_DECIMAL
See Also:
Constant Field Values

MONEY_USD

public static final String MONEY_USD
See Also:
Constant Field Values

ALPHA_NUM

public static final String ALPHA_NUM
See Also:
Constant Field Values

EMAIL

public static final String EMAIL
See Also:
Constant Field Values

HOST

public static final String HOST
See Also:
Constant Field Values

REGULAR_STRING

public static final String REGULAR_STRING
See Also:
Constant Field Values

ANYTHING

public static final String ANYTHING
Constructor Detail

StringHelper

public StringHelper()
Method Detail

firstToken

public static String firstToken(String text,
                                String separator)
Splits text at the first occurence of separator and returns the left part, excluding the separator.

Parameters:
text - the haystack
separator - the needle
Returns:
the empty string, if no occurence can be found

indexOfDigit

public static int indexOfDigit(String s)
Returns the index of the first digit in s.

Returns:
-1, if s does not contain digits; the index of the first digit, otherwise
See Also:
String.indexOf(String)

lastPrefix

public static String lastPrefix(String text,
                                String separator)
Splits text at the last occurence of separator and returns the left part, excluding the separator.

Parameters:
text - the haystack
separator - the needle
Returns:
the empty string, if no occurence can be found

lastToken

public static String lastToken(String text,
                               String separator)
Splits text at the last occurence of separator and returns the right part, excluding the separator.

Parameters:
text - the haystack
separator - the needle
Returns:
the empty string, if no occurence can be found

randomString

public static String randomString(int length)
Returns a random lower case letter-only string with length characters.


replaceAll

public static String replaceAll(String s,
                                String oldChars,
                                String newChars)
Replaces all occurences of oldChars in s by newChars.

Returns:
the modified instance of s

stripExtra

public static String stripExtra(String s)
Removes all characters from s that are not letters. Returns a new String which can be for instance used as search text.

Returns:
a stripped instance of s

toArray

public static String[] toArray(String value,
                               String separators)
Uses a StringTokenizer to split value.

Parameters:
value - the String to split
separators - the separators
Returns:
the tokens
See Also:
StringTokenizer

toFirstUpper

public static String toFirstUpper(String value)
Returns a new String that has the first letter of value set to upper case.


toIntArray

public static int[] toIntArray(String value,
                               String separators)

toList

public static List<String> toList(String value,
                                  String separators)

toString

public static String toString(byte[] data)
Tries to autodect encoding.


toString

public static String toString(Collection c,
                              String separator)

toString

public static String toString(int[] array,
                              String separator)

toString

public static String toString(String[] array,
                              String separator)

formatSize

public static String formatSize(long size)
Returns value (byte) formatted as a file size. For example value=2048 returns "2 kb".

Parameters:
size - filesize to be formatted
Returns:
formatted number as string

formatLength

public static String formatLength(long i)
Formats number of seconds in appropriate time unit

Parameters:
i - number of seconds
Returns:
formatted duration as string

pad

public static final String pad(String text,
                               int padding)
Prepends and appends padding whitespaces to text and returns the result.

Parameters:
text - the text
padding - the number of spaces to prepend and append
Returns:
the padded text, lenght will be text.length() + 2 * padding
Throws:
IllegalArgumentException - if padding < 0

pad

public static final String pad(String text,
                               int lpadding,
                               int rpadding)
Prepends lpadding and appends rpadding whitespaces to text and returns the result.

Parameters:
text - the text
lpadding - the number of spaces to prepend
rpadding - the number of spaces to append
Returns:
the padded text, lenght will be text.length() + lpadding + rpadding
Throws:
IllegalArgumentException - if lpadding < 0 or rpadding < 0

append

public static final void append(StringBuffer sb,
                                String text,
                                int count)
Appends text to sb count times.

Parameters:
sb - the buffer the text is appended to
text - the text
count - the number of times text is appenden to buffer


Copyright © 2004-2007 XNap Commons Team. All Rights Reserved.