Class StrUtil

java.lang.Object
  |
  +--StrUtil

public class StrUtil
extends java.lang.Object

This class contains utility routines for manipulating Java Strings.

Usage:
   - The typical scenario for using this class is:
     - 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);
Assumptions:
Effects:
       - None.
Anticipated Changes:
Notes:
Implementation Notes:
Portability Issues:

Since:
1.0.0
Version:
$Revision: 9 $
Author:
$Author: FStluka $

Inner Class Summary
static class StrUtil.Tester
          Each class contains a Tester inner class with a main() for easier unit testing.
 
Constructor Summary
StrUtil()
          Constructor.
 
Method Summary
static java.lang.String insertAfterDelimiters(java.lang.String strString, char chDelim, java.lang.String strInsert)
          Return a string that is a copy of strString with strInsert inserted after each occurrence of chDelim.
static java.lang.String insertCommas(java.lang.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 java.lang.String left(java.lang.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 java.lang.String lpad(java.lang.String strString, char chPad, int intLength)
          Left pad a string.
static java.lang.String makeStringOfChars(char chChar, int intLength)
          Return a string containing intLength occurrences of the character chChar.
static java.lang.String quoteDelimitedSubstrings(java.lang.String strString, char chDelim, java.lang.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 java.lang.String replaceAll(java.lang.String strIn, java.lang.String strFrom, java.lang.String strTo)
          Return a String that is a copy of strIn with all occurrences of strFrom replaced with strTo.
static java.lang.String right(java.lang.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 java.lang.String rpad(java.lang.String strString, char chPad, int intLength)
          Right pad a string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StrUtil

public StrUtil()
Constructor.
Parameters:
None. -  
Throws:
Exceptions - None.
Method Detail

replaceAll

public static java.lang.String replaceAll(java.lang.String strIn,
                                          java.lang.String strFrom,
                                          java.lang.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.
Parameters:
strIn - String to start with.
strFrom - String to change from.
strTo - String to change to.
Returns:
Copy of strIn with updates.
Throws:
Exceptions - None.

quoteDelimitedSubstrings

public static java.lang.String quoteDelimitedSubstrings(java.lang.String strString,
                                                        char chDelim,
                                                        java.lang.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.
Throws:
Exceptions - None.

insertAfterDelimiters

public static java.lang.String insertAfterDelimiters(java.lang.String strString,
                                                     char chDelim,
                                                     java.lang.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.
Throws:
Exceptions - None.

makeStringOfChars

public static java.lang.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.
Throws:
Exceptions - None.

lpad

public static java.lang.String lpad(java.lang.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.
Throws:
Exceptions - None.

rpad

public static java.lang.String rpad(java.lang.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.
Throws:
Exceptions - None.

left

public static java.lang.String left(java.lang.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.
Throws:
Exceptions - None.

right

public static java.lang.String right(java.lang.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.
Throws:
Exceptions - None.

insertCommas

public static java.lang.String insertCommas(java.lang.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.
Throws:
Exceptions - None.