com.cafesoft.core.util
Class StringUtils

java.lang.Object
  |
  +--com.cafesoft.core.util.StringUtils

public class StringUtils
extends Object

Provides convenience methods for string manipulations.

Since:
2/13/02

Constructor Summary
StringUtils()
           
 
Method Summary
static String capitalize(String string)
          Capitalize the start of the given String.
static int charOccurences(String string, char c)
          Return the number of occurrences of a given character within a String.
static String[] getStringArray(String s, String token)
          Parse a string for values separated by a single input character.
static boolean hasNonAlpha(String s)
          Check a String for a non-alphabetic character: ("0123456789~!
static boolean isFloat(String s)
          Determine if a String is a valid float.
static String padString(String s, int length, String direction, String fill)
          Pads the right or left of a string with the specified character to a specified length.
static String[] parseStringArray(String string, String delim)
          Parse a String into an array of Strings.
static String replaceTokens(String text, HashMap map)
          Return input text with substitutions for matched values where the matches are designated by names in curley brackets.
static String squeezeAdjacentChars(String value, char adjChar)
          Squeeze adjacent duplicate characters within a String (in place).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StringUtils

public StringUtils()
Method Detail

hasNonAlpha

public static boolean hasNonAlpha(String s)
Check a String for a non-alphabetic character: ("0123456789~!@#$%^&*()_-+=).

Parameters:
s - the string to evaluate
Returns:
true if the String contains at least one non-alphabetic character

isFloat

public static boolean isFloat(String s)
Determine if a String is a valid float.

Parameters:
s - the string to evaluate
Returns:
true if the String is a valid float

capitalize

public static String capitalize(String string)
Capitalize the start of the given String.

Parameters:
string - the string to capitalize the first character for
Returns:
the new capitalized String

charOccurences

public static int charOccurences(String string,
                                 char c)
Return the number of occurrences of a given character within a String.

Parameters:
string - the String.
c - the character.
Returns:
the number of occurrences of the character within the String.

parseStringArray

public static String[] parseStringArray(String string,
                                        String delim)
Parse a String into an array of Strings.

For example, if string is: value1,value2,value3 and the delimiter is ',', then return an array of 3 Strings.

Parameters:
string - the String be parsed.
delim - a String containing the characters than can separate String values.
Returns:
the a String array. If the input String is null an empty is is returned. If the input String is empty, an array of length 1 is returned and with the zero length String as the value.

squeezeAdjacentChars

public static String squeezeAdjacentChars(String value,
                                          char adjChar)
Squeeze adjacent duplicate characters within a String (in place).

NOTE: This function looks for the duplicate adjacent character within a string and keep just a single instance of the character. For example, if the slash character '/' is the character to be squeezed:

 Original Value               Squeezed Value       Comments
 ----------------------------------------------------------------------
 //                           /
 /dir1/index.html             /dir1/index.html     No change
 /dir1//index.html            /dir1/index.html
 /dir1///index.html           /dir1/index.html
 /dir1////index.html          /dir1/index.html
 /dir1//dir2//////index.html  /dir1/dir2/index.html
 /dir1//dir2//////            /dir1/dir2/
 

Parameters:
value - the String containing adjacent duplicate characters to "squeeze".
adjChar - the character within the String to be squeezed.
Returns:
a new String with the adjacent duplicate characters removed or the original String if no duplicates are found.

replaceTokens

public static String replaceTokens(String text,
                                   HashMap map)
Return input text with substitutions for matched values where the matches are designated by names in curley brackets. For example: input text: "I am a {age} year old named {first}." names/values: "first"/"Goober", "age"/"8" return text: "I am a 8 year old named Goober."

Parameters:
text - The text with match names found in curly brackets
map - A HashMap of the names and values to replace
Returns:
The text with all matched tokens now replaced

padString

public static String padString(String s,
                               int length,
                               String direction,
                               String fill)
Pads the right or left of a string with the specified character to a specified length.

Parameters:
s - the string to evaluate
length - the size to make the String will be.
direction - fill the right (default) or left side ("right" or "left")
fill - the character to use for padding
Returns:
true if the String contains at least one non-alphabetic character.

getStringArray

public static String[] getStringArray(String s,
                                      String token)
Parse a string for values separated by a single input character. For example, "foo,goober,bar" could be parsed by specifying "," as the token. It would return a String[3] with foo, goober, and bar as the values.

Parameters:
s - the string to evaluate
token - the token that separates the String values
Returns:
a String array


Generated on 8:41:50 AM June 06, 2005, © 1996-2005 Cafésoft LLC. All rights reserved.