com.bristle.javalib.util
Class StrUtil

java.lang.Object
  extended by com.bristle.javalib.util.StrUtil

public class StrUtil
extends Object

This class contains utility routines for manipulating Java Strings.

Usage:
   - Typical scenarios for using this class are:
     - Put quotes around comma-separated substrings.
       String strOut = StrUtil.quoteDelimitedSubstrings(strIn, ',', "'");
     - Put spaces after commas.
       String strOut = StrUtil.insertAfterDelimiters(strIn, ',', " ");
     - Create a string of 5 stars (*):
       String strOut = StrUtil.makeStringOfChars('*', 5);
     - Pad string on left with zeros to a total length of 10 chars.
       String strOut = StrUtil.lpad(strIn, '0', 10);
     - Pad string on right with blanks to a total length of 7 chars.
       String strOut = StrUtil.rpad(strIn, ' ', 7);
     - Get the leftmost 7 chars:
       String strOut = StrUtil.left(strIn, 7);
     - Get the rightmost 7 chars:
       String strOut = StrUtil.right(strIn, 7);

   - See the source code of the inner Tester class for more examples.
  
Assumptions:
Effects:
       - None.
Anticipated Changes:
Notes:
Implementation Notes:
Portability Issues:
Revision History:
   $Log$


Nested Class Summary
static class StrUtil.Tester
          Each class contains a Tester inner class with a main() for easier unit testing.
 
Constructor Summary
StrUtil()
           
 
Method Summary
static boolean containsAnyChar(String strString, String strChars)
          Return true if strString contains any of the chars in strChars; false otherwise.
static boolean containsAnyOtherChar(String strString, String strChars)
          Return true if strString contains any char other than those in strChars; false otherwise.
static boolean equalsIgnoreCaseOrBothNull(String str1, String str2)
          Return true if the specified Strings are equal or both null; false otherwise.
static boolean equalsIgnoreCaseOrBothNullOrEmpty(String str1, String str2)
          Return true if the specified Strings are equalsIgnoreCase() or if both of them are null or empty; false otherwise.
static boolean equalsOrBothNullOrEmpty(String str1, String str2)
          Return true if the specified Strings are equal or if both of them are null or empty; false otherwise.
static String insertAfterDelimiters(String strString, char chDelim, String strInsert)
          Return a string that is a copy of strString with strInsert inserted after each occurrence of chDelim.
static String insertCommas(String strString)
          Returns the specified string with commas inserted every 3 chars counting from the right -- the traditional formatting of a large number, broken into ones, thousands, millions, etc.
static boolean isNullOrEmpty(String strIn)
          Return true if the specified String is null or empty; false otherwise.
static String left(String strString, int intLength)
          Returns a string containing the specified number of chars copied from the beginning of a string, or less if the specified string is too short.
static String lpad(String strString, char chPad, int intLength)
          Left pad a string.
static String ltrim(String strString, String strMatch)
          Left trim a string.
static String makeStringOfChars(char chChar, int intLength)
          Return a string containing intLength occurrences of the character chChar.
static String mapEmptyToNull(String strIn)
          Returns null if the specified string, when trimmed, is null or empty.
static String quoteDelimitedSubstrings(String strString, char chDelim, String strQuote)
          Return a string that is a copy of strString with strQuote inserted before and after each substring that is delimited by chDelim.
static String replaceAll(String strIn, String strFrom, String strTo)
          Return a String that is a copy of strIn with all occurrences of strFrom replaced with strTo.
static String replaceFirst(String strIn, String strFrom, String strTo)
          Return a String that is a copy of strIn with the first occurrences, if any, of strFrom replaced with strTo.
static String right(String strString, int intLength)
          Returns a string containing the specified number of chars copied from the end of a string, or less if the specified string is too short.
static String rpad(String strString, char chPad, int intLength)
          Right pad a string.
static String rtrim(String strString, String strMatch)
          Right trim a string.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StrUtil

public StrUtil()
Method Detail

isNullOrEmpty

public static boolean isNullOrEmpty(String strIn)
Return true if the specified String is null or empty; false otherwise.

Parameters:
strIn - String to compare.
Returns:
true if equal or both null or empty; false otherwise.

equalsOrBothNullOrEmpty

public static boolean equalsOrBothNullOrEmpty(String str1,
                                              String str2)
Return true if the specified Strings are equal or if both of them are null or empty; false otherwise.

Parameters:
str1 - String to compare.
str2 - String to compare.
Returns:
true if equal or both null or empty; false otherwise.

equalsIgnoreCaseOrBothNullOrEmpty

public static boolean equalsIgnoreCaseOrBothNullOrEmpty(String str1,
                                                        String str2)
Return true if the specified Strings are equalsIgnoreCase() or if both of them are null or empty; false otherwise.

Parameters:
str1 - String to compare.
str2 - String to compare.
Returns:
true if equal or both null or empty; false otherwise.

equalsIgnoreCaseOrBothNull

public static boolean equalsIgnoreCaseOrBothNull(String str1,
                                                 String str2)
Return true if the specified Strings are equal or both null; false otherwise.

Parameters:
str1 - String to compare.
str2 - String to compare.
Returns:
true if equal or both null; false otherwise.

mapEmptyToNull

public static String mapEmptyToNull(String strIn)
Returns null if the specified string, when trimmed, is null or empty. Otherwise, returns the specified string.

Parameters:
strIn - The input string.
Returns:
Null or the value of strIn.

replaceAll

public static String replaceAll(String strIn,
                                String strFrom,
                                String strTo)
Return a String that is a copy of strIn with all occurrences of strFrom replaced with strTo. If strFrom is the empty string, return strIn. Note: The advantage of this over the native Java String.replaceAll() is that it does not try to interpret strFrom as a regular expression.

Parameters:
strIn - String to start with.
strFrom - String to change from.
strTo - String to change to.
Returns:
Copy of strIn with updates.

replaceFirst

public static String replaceFirst(String strIn,
                                  String strFrom,
                                  String strTo)
Return a String that is a copy of strIn with the first occurrences, if any, of strFrom replaced with strTo. If strFrom is the empty string, return strIn. Note: The advantage of this over the native Java String.replaceFirst() is that it does not try to interpret strFrom as a regular expression.

Parameters:
strIn - String to start with.
strFrom - String to change from.
strTo - String to change to.
Returns:
Copy of strIn with updates.

quoteDelimitedSubstrings

public static String quoteDelimitedSubstrings(String strString,
                                              char chDelim,
                                              String strQuote)
Return a string that is a copy of strString with strQuote inserted before and after each substring that is delimited by chDelim.

Parameters:
strString - String to search for delimited substrings.
chDelim - Delimiter character.
strQuote - String to insert before and after each substring.
Returns:
String with quotes inserted.

insertAfterDelimiters

public static String insertAfterDelimiters(String strString,
                                           char chDelim,
                                           String strInsert)
Return a string that is a copy of strString with strInsert inserted after each occurrence of chDelim.

Parameters:
strString - String to search for delimited substrings.
chDelim - Delimiter character.
strInsert - String to insert after each delimiter.
Returns:
String with insertions.

makeStringOfChars

public static String makeStringOfChars(char chChar,
                                       int intLength)
Return a string containing intLength occurrences of the character chChar. If intLength is zero or negative, return the empty string ("");

Parameters:
chChar - Character to put in the string.
intLength - Desired length of the result string.
Returns:
Generated string.

lpad

public static String lpad(String strString,
                          char chPad,
                          int intLength)
Left pad a string. Return a string that is a copy of strString preceded by enough copies of chPad to make the length of the resulting string equal intLength. If strString is already too long, return it unchanged.

Parameters:
strString - String to pad.
chPad - Character to pad with.
intLength - Desired length of the result string.
Returns:
Padded string.

rpad

public static String rpad(String strString,
                          char chPad,
                          int intLength)
Right pad a string. Return a string that is a copy of strString followed by enough copies of chPad to make the length of the resulting string equal intLength. If strString is already too long, return it unchanged.

Parameters:
strString - String to pad.
chPad - Character to pad with.
intLength - Desired length of the result string.
Returns:
Padded string.

ltrim

public static String ltrim(String strString,
                           String strMatch)
Left trim a string. Return a string that is a copy of strString with all consecutive leading occurrences of the specified substring removed.

Parameters:
strString - String to trim.
strMatch - Substring to remove.
Returns:
Padded string.

rtrim

public static String rtrim(String strString,
                           String strMatch)
Right trim a string. Return a string that is a copy of strString with all consecutive trailing occurrences of the specified substring removed.

Parameters:
strString - String to trim.
strMatch - Substring to remove.
Returns:
Padded string.

left

public static String left(String strString,
                          int intLength)
Returns a string containing the specified number of chars copied from the beginning of a string, or less if the specified string is too short.

Parameters:
strString - String to get substring from.
intLength - Max length of the substring to return. Negative values rounded up to zero.
Returns:
Substring.

right

public static String right(String strString,
                           int intLength)
Returns a string containing the specified number of chars copied from the end of a string, or less if the specified string is too short.

Parameters:
strString - String to get substring from.
intLength - Max length of the substring to return. Negative values rounded up to zero.
Returns:
Substring.

insertCommas

public static String insertCommas(String strString)
Returns the specified string with commas inserted every 3 chars counting from the right -- the traditional formatting of a large number, broken into ones, thousands, millions, etc.

Parameters:
strString - String to insert commas into.
Returns:
String with commas.

containsAnyChar

public static boolean containsAnyChar(String strString,
                                      String strChars)
Return true if strString contains any of the chars in strChars; false otherwise.

Parameters:
strString - String to search.
strChars - String of chars to search for.
Returns:
true if strIn contains any of the chars in strChars; false otherwise.

containsAnyOtherChar

public static boolean containsAnyOtherChar(String strString,
                                           String strChars)
Return true if strString contains any char other than those in strChars; false otherwise.

Parameters:
strString - String to search.
strChars - String of chars to search for.
Returns:
true if strString contains any char other than those in strChars; false otherwise.