com.claritysys.util
Class Strings

java.lang.Object
  extended by com.claritysys.util.Strings

public final class Strings
extends java.lang.Object

A collection of String methods.

Version:
$Revision: 2348 $

Constructor Summary
Strings()
           
 
Method Summary
static boolean areEqual(java.lang.String s1, java.lang.String s2)
          Return true if the two String arguments are equal, where either or both of the arguments may be null.
static java.lang.String expand(java.lang.String expr, java.lang.String fmt)
          Expand the string as specified by '@' placeholders in string .
static java.lang.String[] fragmentString(java.lang.String longString, int fragmentSize)
          Rip a String into equal sized fragments.
static boolean isAlpha(java.lang.String string)
          Return a boolean true response if String is Alpha
static boolean isDecimal(java.lang.String string)
          Return a boolean true response if String is a Decimal value (Numeric, '.')
static boolean isEmpty(java.lang.String s)
          Return true if string is null, empty, or all space characters.
static boolean isMoney(java.lang.String string)
          Return a boolean true response if String is a Money value (Numeric, '.')
static boolean isNumeric(java.lang.String string)
          Return a boolean true response if String is an Integer.
static java.lang.String leftFill(java.lang.String orig, int newLength, char padChar)
          Left fill given string to newLength positions using pad character padChar.
static java.lang.String noNull(java.lang.String s)
          Return the original String if not null or an empty String if it was null.
static java.lang.String nullIfEmpty(java.lang.String s)
          Return null if the given String is null or zero length, otherwise return the String.
static java.lang.String[] paginateString(java.lang.String string, java.lang.String breakString, int columns)
          Return an array of strings representing the original string but having been: split into multiple lines based on the given breakString, word-wrapped around the number of columns on a line.
static java.lang.String replace(java.lang.String s, java.lang.String find, java.lang.String replace)
          String replace.
static java.lang.String rightFill(java.lang.String s, int l, char pad)
          Right fill string to given length.
static java.lang.String stripChar(java.lang.String s, char ch)
          Compact given string by removing specified character.
static java.lang.String stripNonDecimal(java.lang.String string)
          Return a String that represents the numbers only.
static java.lang.String stripNonLetter(java.lang.String string)
          Return a new String based on the given one but without any non-letter characters removed.
static java.lang.String stripNonLetterOrDigit(java.lang.String string)
          Return a new String based on the given one but without any non-letter or digit characters removed.
static java.lang.String stripNonNumeric(java.lang.String string)
          Return a new String based on the given one but without any non-digit characters removed.
static java.lang.String[] toArray(java.lang.String expr, java.lang.String delim)
          Convert string with delimiter into array of Strings.
static java.lang.String trim(java.lang.String s)
          Returned a whitespace trimmed version of the given String unless it is null, in which case return null.
static java.lang.String upper(java.lang.String s)
          Return the given String converted to uppercase.
static java.lang.String[] wrapString(java.lang.String string, int columns)
          Return an array of strings representing the original string 'word wrapped' around the specified column.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Strings

public Strings()
Method Detail

areEqual

public static boolean areEqual(java.lang.String s1,
                               java.lang.String s2)
Return true if the two String arguments are equal, where either or both of the arguments may be null. If they are both null they are considered to be equal.

Parameters:
s1 - The first of the two Strings.
s2 - The second of the two Strings.
Returns:
true if s1 is null && s2 is null OR s1.equals (s2).

noNull

public static java.lang.String noNull(java.lang.String s)
Return the original String if not null or an empty String if it was null.

Parameters:
s - The string to test.
Returns:
The original String or an empty String.

nullIfEmpty

public static java.lang.String nullIfEmpty(java.lang.String s)
Return null if the given String is null or zero length, otherwise return the String.

Many XML applications return an empty string instead of null, so this helps convert it to null.

Parameters:
s - The string to test for null or empty.
Returns:
The original string, or null if it was null or empty.

replace

public static java.lang.String replace(java.lang.String s,
                                       java.lang.String find,
                                       java.lang.String replace)
String replace.

Does a lazy create on the underlying StringBuffer, so that if nothing is replaced it just returns the original String. This saves CPU time by not allocating lots of little objects that won't be used.

Parameters:
s - Original string.
find - String to search for and replace if found.
replace - Replacement text.
Returns:
The original string with the 'find' text replaced with the 'replace' text. If nothing was replaced, returns s.

stripChar

public static java.lang.String stripChar(java.lang.String s,
                                         char ch)
Compact given string by removing specified character. Returns a new String object which is identical to the one passed in except that all ocurrances of ch have been removed.

This always creates a StringBuffer, it could be improved by only creating it when there are characters to remove.

Parameters:
s - The string to remove certain characters from.
ch - The character to remove from s.
Returns:
The original string with all occurances of ch removed.

leftFill

public static java.lang.String leftFill(java.lang.String orig,
                                        int newLength,
                                        char padChar)
Left fill given string to newLength positions using pad character padChar.

Example:

 leftFill ("23", 5, '0')
 
This will return "00023".

Parameters:
orig - The original String.
newLength - The desired length to make the string.
padChar - The character to left-fill orig with.
Returns:
orig left-filled to newLength characters with padChar.

isEmpty

public static boolean isEmpty(java.lang.String s)
Return true if string is null, empty, or all space characters.

Parameters:
s - The string to test.
Returns:
true if s == null, s.length() == 0, or s contains all space characters (ASCII 32): false otherwise.

rightFill

public static java.lang.String rightFill(java.lang.String s,
                                         int l,
                                         char pad)
Right fill string to given length. Pad with pad char if necessary, or truncate to given length. If input is null return space filled string.

Parameters:
s - The string to right-fill.
l - The new length.
pad -
Returns:
s right-filled to l characters with space characters.

toArray

public static java.lang.String[] toArray(java.lang.String expr,
                                         java.lang.String delim)
Convert string with delimiter into array of Strings.

Parameters:
expr - The string containing a list of delimited fields, such as "one|two|three".
delim - The delimiter text.
Returns:
A String array.

paginateString

public static java.lang.String[] paginateString(java.lang.String string,
                                                java.lang.String breakString,
                                                int columns)
Return an array of strings representing the original string but having been:
  1. split into multiple lines based on the given breakString,
  2. word-wrapped around the number of columns on a line.

Parameters:
string - The string to word-wrap.
breakString - A pattern which if matched forces a line break.
columns - The maximum line length.
Returns:
A String array, one element for each 'line'.

wrapString

public static java.lang.String[] wrapString(java.lang.String string,
                                            int columns)
Return an array of strings representing the original string 'word wrapped' around the specified column. Algorithm: * Scan backwards from columns looking for whitespace * If reach point of dimishing returns, wrap at columns.

Parameters:
string - The string to perform wrapping on.
columns - The column number to wrap on.
Returns:
A String array representing the resulting 'lines' of text.

expand

public static java.lang.String expand(java.lang.String expr,
                                      java.lang.String fmt)
Expand the string as specified by '@' placeholders in string .

eg: expandChars ("19960912", "@@@@-@@-@@") yields "1996-09-12"

Parameters:
expr - A string of input characters.
fmt - A format composed of '@' placeholders.
Returns:
The expanded string.

fragmentString

public static java.lang.String[] fragmentString(java.lang.String longString,
                                                int fragmentSize)
Rip a String into equal sized fragments.

Parameters:
longString - A long string to break apart.
fragmentSize - The size in characters of each fragment.
Returns:
A String array of the fragments.

stripNonLetterOrDigit

public static java.lang.String stripNonLetterOrDigit(java.lang.String string)
Return a new String based on the given one but without any non-letter or digit characters removed.

What's a letter? Anything for which Character.isLetter (ch) returns true! What's a digit? Anything for which Character.isDigit (ch) returns true!

Parameters:
string - A string, non null.
Returns:
A copy of string but without any non-letter or digit characters.

stripNonNumeric

public static java.lang.String stripNonNumeric(java.lang.String string)
Return a new String based on the given one but without any non-digit characters removed.

Parameters:
string - A string, non null.
Returns:
A copy of string but without any non-digits.

stripNonLetter

public static java.lang.String stripNonLetter(java.lang.String string)
Return a new String based on the given one but without any non-letter characters removed.

What's a letter? Anything for which Character.isLetter (ch) returns true!

Parameters:
string - A string, non null.
Returns:
A copy of string but without any non-letter characters.

trim

public static java.lang.String trim(java.lang.String s)
Returned a whitespace trimmed version of the given String unless it is null, in which case return null.

This is usefull mostly when accessing ResultSet String objects that may be null, and if not must be trimmed.

Parameters:
s - The String to trim if not null.
Returns:
s == null? null: s.trim ().

upper

public static java.lang.String upper(java.lang.String s)
Return the given String converted to uppercase. If null, return null.

Parameters:
s - The string to convert, possibly null.
Returns:
s == null? null: s.toUppercase.

isNumeric

public static boolean isNumeric(java.lang.String string)
Return a boolean true response if String is an Integer. NOTE -- This method is poorly defined, don't use it (or rewrite it first!).

Parameters:
string - A string, non null.
Returns:
A boolean (true if String isNumeric else false).

isAlpha

public static boolean isAlpha(java.lang.String string)
Return a boolean true response if String is Alpha

Parameters:
string - A string, non null.
Returns:
A boolean (true if String isAlpha else false).

isMoney

public static boolean isMoney(java.lang.String string)
Return a boolean true response if String is a Money value (Numeric, '.')

Parameters:
string - A string, non null.
Returns:
A boolean (true if String isMoney else false).

isDecimal

public static boolean isDecimal(java.lang.String string)
Return a boolean true response if String is a Decimal value (Numeric, '.')

Parameters:
string - A string, non null.
Returns:
A boolean (true if String isDecimal else false).

stripNonDecimal

public static java.lang.String stripNonDecimal(java.lang.String string)
Return a String that represents the numbers only.

Parameters:
string - A string, non null.
Returns:
A String.


Copyright ? 2002 Clarity Systems Group, LLC. All Rights Reserved.