Poseidon for UMLTM
  UMLdoc
 PREV  CLASSIFIER    NEXT  CLASSIFIER     FRAMES    NO FRAMES      
SUMMARY:   INNER | ATTR | ASSOC | CONSTR | METHOD     DETAIL:   INNER | ATTR | ASSOC | CONSTR | METHOD     DIAGRAMS:   COLLAB | SEQ | STATE | ACTIV

com.gentleware.services.util

Class Strings

com.gentleware.services.util.Strings

public final class Strings


Strings provides static helper methods that act on string instances.

Created : kelvin, 24.01.2003, 13:33:16

Authors:
kelvin


 Attribute Summary
private static final int  APPROXIMATE_JOIN_STRING_LENGTH
 
private static String  ASCII
 
public static int  CONCATENATION_UNLIMITED
 
private static ToStringMapper  DEFAULT_TO_STRING_MAPPER
 
public static String  ELLIPSIS
 
public static char[]  HEX_DIGITS
 
private static String  UTF8
 
arrow_up
 

 Constructor Summary
private   Strings()
Do not allow this class to be instantiated: It has static methods only. 
arrow_up
 

 Method Summary
public static String   center(String text, int width)
Center a string using spaces. 
public static String   center(String text, int width, char fillChar)
Center a string using a custom fill character. 
public static int   compare(String a, String b, boolean caseSensitive)
Compares two strings, null and empty Strings are considered equal  
public static boolean   containsAny(String text, char[] testChars)
 
public static boolean   containsOnly(String text, char[] validChars)
 
public static int   countMatches(String text, String pattern)
 
public static String   createConcatenation(Collection collectionOfObjects, String separator)
 
public static String   createConcatenation(Collection collectionOfObjects, String separator, int limit)
 
public static String   createConcatenation(Collection collectionOfObjects, String separator, int limit, ToStringMapper toStringMapper)
Creates a String-list of the given objects, separated by the given separator. 
public static String   createStringFromAsciiBytes(byte[] bytes)
Converts a byte array that holds ISO-8859-1 encoded characters to a string. 
public static String   createStringFromAsciiBytes(byte[] bytes, int offset, int length)
Converts a byte array that holds ISO-8859-1 encoded characters to a string. 
public static String   createStringFromUtf8Bytes(byte[] bytes)
Converts a byte array that holds utf-8 (Unicode) encoded characters to a string. 
public static String   encodeForXml(String unicode)
 
public static boolean   equals(String text1, String text2)
 
public static boolean   equalsIgnoreCase(String text1, String text2)
 
public static String   escape(String text, boolean escape8Bit)
Escapes any values it finds into their String form. 
public static String   getAsciiArt(Font font, String text)
 
public static byte[]   getAsciiBytes(String text)
Converts a string to a byte array using ISO-8859-1 (Ascii) encoding. 
private static int   getHexDigitValue(char c)
 
public static byte[]   getUtf8Bytes(String text)
Converts a string to a byte array using utf-8 encoding. 
public static int   indexOf(StringBuffer text, String pattern, int startPos)
Returns the index of the first occurence of pattern in text starting at startPos. 
public static boolean   isAlpha(String text)
 
public static boolean   isAlpha(String text, String otherValidChars)
 
public static boolean   isAlphanumeric(String text)
 
public static boolean   isAlphanumeric(String text, String otherValidChars)
 
public static boolean   isEmpty(String text)
 
public static boolean   isNumeric(String text)
 
public static boolean   isNumeric(String text, String otherValidChars)
 
public static String   join(Collection strings, String delimiter)

Joins the strings in the collection separated by the delimiter-string. 

public static String   left(String text, int count)
 
public static String   leftPad(String text, int width)
Left-pad a string with spaces. 
public static String   leftPad(String text, int width, char fillChar)
Left-pad a string with a custom fill character. 
public static String   lowerFirstChar(String text)
Returns a String with the first character converted to lower case. 
public static List   mapToString(Collection collectionOfObjects, ToStringMapper toStringMapper)
Maps the collection of objects to a list of strings (keeping the order given by the collection). 
public static String   mid(String text, int startIndex, int length)
Note that it's ok if length < 0. 
public static String   noNull(String text)
 
public static String   noNull(String text, String defaultString)
 
private static String   quoteUnicodeChar(char c)
 
public static String   repeat(char c, int count)
 
public static String   repeat(String text, int count)
Returns a new string that consists of count copies of text. 
public static String   replaceAll(String text, String original, String replacement)
Returns a String where all occurences of a substring are replaced with another string. 
public static void   replaceAll(StringBuffer text, String original, String replacement)
Modifies a StringBuffer by replacing each occurence of a substring with another string. 
public static String   replaceFirst(String text, String original, String replacement)
Returns a String where the first occurence of a substring is replaced with another string. 
public static String   reverse(String text)
 
public static String   right(String text, int count)
 
public static String   rightPad(String text, int width)
Right-pad a string with spaces. 
public static String   rightPad(String text, int width, char fillChar)
Right-pad a string with a custom fill character. 
public static String   substring(String text, int startIndex)
Like text.substring(int) but returns an empy string if startIndex > text.length. 
public static String   substring(String text, int startIndex, int endIndex)
Like text.substring(startIndex, endIndex), but returns an empty string if either startIndex > text.length() or startIndex >= endIndex. 
public static char   toHexDigit(int nibble)
 
public static String   trimLeft(String text)
 
public static String   trimRight(String text)
 
public static String   unescape(String escapedString)
 
public static String   upperFirstChar(String text)
Returns a String with the first character converted to upper case. 
public static String   wrap(String text, int columns)
Inserts a line break "\n" every columns characters  
arrow_up
 

Tagged Values
static false
 

 Attribute Detail

APPROXIMATE_JOIN_STRING_LENGTH

private static int APPROXIMATE_JOIN_STRING_LENGTH

arrow_up


ASCII

private static String ASCII

arrow_up


CONCATENATION_UNLIMITED

public static int CONCATENATION_UNLIMITED

arrow_up


DEFAULT_TO_STRING_MAPPER

private static ToStringMapper DEFAULT_TO_STRING_MAPPER

arrow_up


ELLIPSIS

public static String ELLIPSIS

arrow_up


HEX_DIGITS

public static char[] HEX_DIGITS

arrow_up


UTF8

private static String UTF8

arrow_up
 Constructor Detail

Strings

private Strings Strings()


Do not allow this class to be instantiated: It has static methods only.  
Returns:
arrow_up
 Method Detail

center

public static String center(String text, int width)


Center a string using spaces.  
Parameters:
text -
width -
Returns:
text if text.length() >= width else a copy of string surrounded with width - text.length() spaces, so that the the number of spaces at both sides is equal or is there is a single space more at the right.
arrow_up


center

public static String center(String text, int width, char fillChar)


Center a string using a custom fill character.  
Parameters:
text -
width -
fillChar -
Returns:
text if text.length() >= width else a copy of string surrounded width - text.length() fill characters, so that the the number of spaces at both sides is equal or is there is a fill character more at the right.
arrow_up


compare

public static int compare(String a, String b, boolean caseSensitive)


Compares two strings, null and empty Strings are considered equal  
Parameters:
a -
b -
caseSensitive -
Returns:
arrow_up


containsAny

public static boolean containsAny(String text, char[] testChars)

Parameters:
text -
testChars -
Returns:
true if any character from testChars is in text
arrow_up


containsOnly

public static boolean containsOnly(String text, char[] validChars)

Parameters:
text -
validChars -
Returns:
true if all characters from text are in validChars
arrow_up


countMatches

public static int countMatches(String text, String pattern)

Parameters:
text -
pattern -
Returns:
the number of occurences of pattern in text (0 if pattern.length() == 0)
arrow_up


createConcatenation

public static String createConcatenation(Collection collectionOfObjects, String separator)

Parameters:
collectionOfObjects -
separator -
Returns:
arrow_up


createConcatenation

public static String createConcatenation(Collection collectionOfObjects, String separator, int limit)

Parameters:
collectionOfObjects -
separator -
limit -
Returns:
arrow_up


createConcatenation

public static String createConcatenation(Collection collectionOfObjects, String separator, int limit, ToStringMapper toStringMapper)


Creates a String-list of the given objects, separated by the given separator.  
Parameters:
collectionOfObjects - the objects to list
separator - the separator to fill between the objects
limit - if this limit is reached, list-generation breaks and an ellipsis is appended (1 means: one element and then the ellipsis)
toStringMapper - used to map the objects to strings. By default, i. e. if null is given, the objects {@link Object#toString()}-method is used.
Returns:
the list separated by the separator
Exceptions:
NullPointerException if collectionOfObjects is null
arrow_up


createStringFromAsciiBytes

public static String createStringFromAsciiBytes(byte[] bytes)


Converts a byte array that holds ISO-8859-1 encoded characters to a string.  
Parameters:
bytes - array of ISO-8859-1 encoded characters
Returns:
a string created from the byte array using ascii encoding
arrow_up


createStringFromAsciiBytes

public static String createStringFromAsciiBytes(byte[] bytes, int offset, int length)


Converts a byte array that holds ISO-8859-1 encoded characters to a string.  
Parameters:
bytes - array of ISO-8859-1 encoded characters
offset -
length -
Returns:
a string created from the byte array using ascii encoding
arrow_up


createStringFromUtf8Bytes

public static String createStringFromUtf8Bytes(byte[] bytes)


Converts a byte array that holds utf-8 (Unicode) encoded characters to a string.  
Parameters:
bytes - array of utf-8 encoded characters
Returns:
a string created from the byte array using utf-8 encoding
arrow_up


encodeForXml

public static String encodeForXml(String unicode)

Parameters:
unicode -
Returns:
arrow_up


equals

public static boolean equals(String text1, String text2)

Parameters:
text1 -
text2 -
Returns:
true if both text1 and text2 are null or if text1.equals(text2)
arrow_up


equalsIgnoreCase

public static boolean equalsIgnoreCase(String text1, String text2)

Parameters:
text1 -
text2 -
Returns:
true if both text1 and text2 are null or if text1.equalsIgnoreCase(text2)
arrow_up


escape

public static String escape(String text, boolean escape8Bit)


Escapes any values it finds into their String form. So a tab becomes the characters '\' and 't' and non-ascii characters are converted to '\' + 'u' + hex value.  
Parameters:
text - String to escape values in
escape8Bit - determine if character in the range (0x7f, 0xff) are converted to a unicode escape sequence
Returns:
String with escaped values
arrow_up


getAsciiArt

public static String getAsciiArt(Font font, String text)

Parameters:
font -
text -
Returns:
the text as 'ASCII art' in the given font using a '#' character for each pixel (the text itself if any runtime exception occurs)
arrow_up


getAsciiBytes

public static byte[] getAsciiBytes(String text)


Converts a string to a byte array using ISO-8859-1 (Ascii) encoding. The behaviour is unspecified if the string contains non-Ascii characters.  
Parameters:
text - a string to encode in ascii
Returns:
a byte array representing the string
arrow_up


getHexDigitValue

private static int getHexDigitValue(char c)

Parameters:
c -
Returns:
arrow_up


getUtf8Bytes

public static byte[] getUtf8Bytes(String text)


Converts a string to a byte array using utf-8 encoding.  
Parameters:
text - a string to encode in utf-8
Returns:
a byte array representing the string
arrow_up


indexOf

public static int indexOf(StringBuffer text, String pattern, int startPos)


Returns the index of the first occurence of pattern in text starting at startPos. Returns -1 if the pattern could not be found. Note that Java 1.4 has StringBuffer.indexOf() (but it's almost as slow as this naive implementation). A great text library is http://oss.software.ibm.com/icu4j but that would be overkill for now.  
Parameters:
text - the String that is examined
pattern - the String that is searched for
startPos - search will start at this index, i.e. occurences starting at a smaller index will be ignored
Returns:
the index of the first occurence of pattern in text where index is greater than or equal to startPos or -1 if there is no such occurence
arrow_up


isAlpha

public static boolean isAlpha(String text)

Parameters:
text -
Returns:
true if for each character c in text: Character.isLetter(c)
arrow_up


isAlpha

public static boolean isAlpha(String text, String otherValidChars)

Parameters:
text -
otherValidChars -
Returns:
true if for each character c in text: c is contained in otherValidChars or Character.isLetter(c)
arrow_up


isAlphanumeric

public static boolean isAlphanumeric(String text)

Parameters:
text -
Returns:
true if for each character c in text: Character.isLetterOrDigit(c)
arrow_up


isAlphanumeric

public static boolean isAlphanumeric(String text, String otherValidChars)

Parameters:
text -
otherValidChars -
Returns:
true if for each character c in text: c is contained in otherValidChars or Character.isLetterOrDigit(c)
arrow_up


isEmpty

public static boolean isEmpty(String text)

Parameters:
text -
Returns:
true if text is null or has zero length
arrow_up


isNumeric

public static boolean isNumeric(String text)

Parameters:
text -
Returns:
true if for each character c in text: Character.isDigit(c)
arrow_up


isNumeric

public static boolean isNumeric(String text, String otherValidChars)

Parameters:
text -
otherValidChars -
Returns:
true if for each character c in text: c is contained in otherValidChars or Character.isDigit(c)
arrow_up


join

public static String join(Collection strings, String delimiter)


Joins the strings in the collection separated by the delimiter-string.

 
Parameters:
strings - a list of strings (or anything which has a useful toString()-method)
delimiter - the delimiter which will be added between two strings
Returns:
the joined string
Exceptions:
NullPointerException if strings is null
arrow_up


left

public static String left(String text, int count)

Parameters:
text -
count -
Returns:
the first count characters of text (text itself if count >= text.length())
arrow_up


leftPad

public static String leftPad(String text, int width)


Left-pad a string with spaces.  
Parameters:
text -
width -
Returns:
text if text.length() >= width else a copy of string with width - text.length() spaces prepended
arrow_up


leftPad

public static String leftPad(String text, int width, char fillChar)


Left-pad a string with a custom fill character.  
Parameters:
text -
width -
fillChar -
Returns:
text if text.length() >= width else a copy of string with width - text.length() fill characters prepended
arrow_up


lowerFirstChar

public static String lowerFirstChar(String text)


Returns a String with the first character converted to lower case.  
Parameters:
text - the String to convert
Returns:
text if text has zero-length or already starts with a lower case character or else a new String which is a copy of text with the first character converted to lower case.
arrow_up


mapToString

public static List mapToString(Collection collectionOfObjects, ToStringMapper toStringMapper)


Maps the collection of objects to a list of strings (keeping the order given by the collection).  
Parameters:
collectionOfObjects - the objects to map
toStringMapper - the mapper to use; null will just use {@link Object#toString()} to map the objects
Returns:
a {@link List} of {@link String Strings} with the objects mapped to strings
Exceptions:
NullPointerException if collectionOfObjects is null
arrow_up


mid

public static String mid(String text, int startIndex, int length)


Note that it's ok if length < 0.  
Parameters:
text -
startIndex -
length -
Returns:
a substring of consisting of the characters of text with indices [startIndex ... startIndex + length] (a shorter string if some of the indices aren't in [0, text.length() - 1]
arrow_up


noNull

public static String noNull(String text)

Parameters:
text -
Returns:
an empty string if text is null else text
arrow_up


noNull

public static String noNull(String text, String defaultString)

Parameters:
text -
defaultString -
Returns:
defaultString if text is null else text
arrow_up


quoteUnicodeChar

private static String quoteUnicodeChar(char c)

Parameters:
c -
Returns:
arrow_up


repeat

public static String repeat(char c, int count)

Parameters:
c -
count -
Returns:
a string with length count where each charachter is c
arrow_up


repeat

public static String repeat(String text, int count)


Returns a new string that consists of count copies of text. Throws IllegalArgumentException if count is negative. If count is zero an empty string is returned.  
Parameters:
text -
count -
Returns:
a string consisiting of count copies of text
arrow_up


replaceAll

public static String replaceAll(String text, String original, String replacement)


Returns a String where all occurences of a substring are replaced with another string. Note that JDK 1.4 has String.replaceAll() using regular expressions.  
Parameters:
text - the text to convert
original - the substring that should be replaced.
replacement - the replacement string for original.
Returns:
a string that is a copy of text but has each occurence of original replaced by replacement
arrow_up


replaceAll

public static void replaceAll(StringBuffer text, String original, String replacement)


Modifies a StringBuffer by replacing each occurence of a substring with another string. Note that Java 1.4 has String.replaceAll() using regular expressions.  
Parameters:
text - the text to convert
original - the substring that should be replaced.
replacement - the replacement string for original.
arrow_up


replaceFirst

public static String replaceFirst(String text, String original, String replacement)


Returns a String where the first occurence of a substring is replaced with another string. Avoids copying the String if it does not contain original. Note that Java 1.4 has String.replaceFirst() using regular expressions.  
Parameters:
text - the text to convert
original - the substring that should be replaced.
replacement - the replacement string for original.
Returns:
a string that is a copy of text but has each occurence of original replaced by replacement
arrow_up


reverse

public static String reverse(String text)

Parameters:
text -
Returns:
arrow_up


right

public static String right(String text, int count)

Parameters:
text -
count -
Returns:
the last count characters of text (text itself if count >= text.length())
arrow_up


rightPad

public static String rightPad(String text, int width)


Right-pad a string with spaces.  
Parameters:
text -
width -
Returns:
text if text.length() >= width else a copy of string with width - text.length() spaces appended
arrow_up


rightPad

public static String rightPad(String text, int width, char fillChar)


Right-pad a string with a custom fill character.  
Parameters:
text -
width -
fillChar -
Returns:
text if text.length() >= width else a copy of string with width - text.length() fill characters appended
arrow_up


substring

public static String substring(String text, int startIndex)


Like text.substring(int) but returns an empy string if startIndex > text.length.  
Parameters:
text -
startIndex -
Returns:
arrow_up


substring

public static String substring(String text, int startIndex, int endIndex)


Like text.substring(startIndex, endIndex), but returns an empty string if either startIndex > text.length() or startIndex >= endIndex. If endIndex > text.length() it is supposed to mean text.length().  
Parameters:
text -
startIndex -
endIndex -
Returns:
arrow_up


toHexDigit

public static char toHexDigit(int nibble)

Parameters:
nibble -
Returns:
arrow_up


trimLeft

public static String trimLeft(String text)

Parameters:
text -
Returns:
arrow_up


trimRight

public static String trimRight(String text)

Parameters:
text -
Returns:
arrow_up


unescape

public static String unescape(String escapedString)

Parameters:
escapedString -
Returns:
arrow_up


upperFirstChar

public static String upperFirstChar(String text)


Returns a String with the first character converted to upper case.  
Parameters:
text - the String to convert
Returns:
text if text has zero-length or already starts with an upper case character or else a new String which is a copy of text with the first character converted to upper case.
arrow_up


wrap

public static String wrap(String text, int columns)


Inserts a line break "\n" every columns characters  
Parameters:
text -
columns -
Returns:
arrow_up
Created  2004 - 10 - 21

    Poseidon for UMLTM
  UMLdoc
 PREV  CLASSIFIER    NEXT  CLASSIFIER     FRAMES    NO FRAMES      
SUMMARY:   INNER | ATTR | ASSOC | CONSTR | METHOD     DETAIL:   INNER | ATTR | ASSOC | CONSTR | METHOD     DIAGRAMS:   COLLAB | SEQ | STATE | ACTIV

Generated with Poseidon for UMLTM.
Poseidon is a registered trademark of Gentleware AG in Germany, USA and other countries. Copyright 2003-2004 Gentleware AG, Schanzenstr. 70, 20357 Hamburg, Germany. All Rights Reserved.

UML is a trademark or registered trademark of Object Management Group, Inc. in the U.S. and other countries.