UC Berkeley Group for User Interface Research
Updated November 17, 2000

edu.berkeley.guir.lib.util
Class StringLib

java.lang.Object
  |
  +--edu.berkeley.guir.lib.util.StringLib

public final class StringLib
extends Object

Various String utilities and routines for manipulating Strings, as well as laying them out and rendering them on screen.

Basically, everytime I needed a String utility that didn't exist, I went ahead and wrote it up, placing it in here. There's lots of stuff here.

The methods here can be roughly grouped into six categories:

This software is distributed under the Berkeley Software License.

 Revisions:  - GUIRLib-v1.0-1.0.0, Apr 19 1997, JH
               Created class
             - GUIRLib-v1.2-1.0.0, Jun 22 2000, JH
               Touched for GUIRLib release
             - GUIRLib-v1.3-1.0.0, Aug 11 2000, JH
               Touched for GUIRLib release
             - GUIRLib-v1.4-1.0.0, Aug 31 2000, JH
               Touched for GUIRLib release
 

Since:
1.2
Version:
GUIRLib-v1.4-1.0.0, Aug 31 2000
Author:
Jason Hong ( jasonh@cs.berkeley.edu)

Field Summary
static int DEFAULT_FOLD_LENGTH
          The maximum number of characters per line.
static int foldLength
          How many spaces to fold on.
static String foldtokens
          What characters to fold lines on.
static String SPACES
          81 space characters.
 
Method Summary
static String align(String str, char ch)
          Align text in a String into two columns.
static boolean containsChars(String str, char[] chars)
          See if a given String contains any of the specified characters.
static boolean containsChars(String str, String strChars)
          See if a given String contains any of the specified characters.
static int countNumOfChar(char chSearch, String strLine)
          Count the number of occurrences of a character in a String.
static int countNumOfString(String strSearch, String strLine)
          Count the number of occurrences of a String in a String.
static String fold(Object obj1, Object obj2)
           
static String fold(String strText, int numCols)
          See fold().
static String fold(String strPrefix, String strText)
          See fold().
static String fold(String strPrefix, String strText, int numCols)
          See fold().
static String fold(String strPrefix, String strText, String eoln, int numCols)
          Fold the text around to the next line correctly.
static String getBarredLine(char ch)
          Repeat the character 80 times and return it as a String.
static String getBarredLine(String strText, char ch)
          Given a String like "example" and a char like '=', return
static int getMaxNumCols(String str)
          After laying out the String into rows, count the number of characters in the longest line.
static int getMaxNumCols(String str, int numCols)
          After laying out the String into rows, count the number of characters in the longest line.
static int getMaxNumCols(String str, String eoln)
          After laying out the String into rows, count the number of characters in the longest line.
static int getMaxNumCols(String str, String eoln, int numCols)
          After laying out the String into rows, count the number of characters in the longest line.
static int getNumRows(String str)
          Count the number of rows this String fills up, based on where the carriage returns '\n' are.
static int getNumRows(String str, int numCols)
          Count the number of rows this String fills up, taking into account that it may have a column limit (ie max num of chars per line).
static String getSegmentType(int val)
          Given a segment type (in PathIterator, return a String with the kind of segment.
static String indent(String str, int spaces)
          Indent the String by pushing all of the rows of the String the specified number of spaces.
static char interpretEscapeSequence(String str)
          Given a String containing an escape sequence, interpret what it is.
static String interpretString(String str)
          Given a String literally containing escape sequences (including unicode ones), convert it into a String with the interpreted escape sequences in place.
 boolean isDecimalNumber(String str)
          See if a given String is a decimal number or not.
static void main(String[] argv)
           
static String padAtEnd(String str, int len)
          Pad this String at the end so that it is at least the specified length.
static String padAtFront(String str, int len)
          Pad this String at the front so that it is at least the specified length.
static String removeSubstring(char chFrom, char chTo, String strLine)
          Remove all characters from a String starting from the first location of one character to the first location of another character.
static String replaceSubstring(String str, String strSearch, String strReplace)
          Given a String, find all instances of the specified substring and replace it with the specified String.
static String reverseInterpretEscapeSequence(byte b)
          Convert non-printing characters into their escape sequence equivalents.
static String reverseInterpretEscapeSequence(char ch)
          Convert non-printing characters into their escape sequence equivalents.
static String reverseInterpretString(String str)
          Convert all non-printing characters in the specified String into their escape sequence equivalents.
static void setFoldLength(int val)
          Set the number of spaces to fold on.
static void setFoldTokens(String tokens)
          Set the tokens to fold text on.
static String spaces(int len)
          Return a String full of spaces, up to 80.
static String toString(BasicStroke bstk)
          Convert a BasicStroke object into a String.
static String toString(boolean[] array)
          Turn this array into something suitable for printing.
static String toString(byte[] array)
          Turn this array into something suitable for printing.
static String toString(char[] array)
          Turn this array into something suitable for printing.
static String toString(double[] array)
          Turn this array into something suitable for printing.
static String toString(Ellipse2D ellipse)
          Print out an ellipse.
static String toString(Enumeration enum)
          Turn this Enumeration into something suitable for printing.
static String toString(float[] array)
          Turn this array into something suitable for printing.
static String toString(int[] array)
          Turn this array into something suitable for printing.
static String toString(Iterator it)
          Turn this Iterator into something suitable for printing.
static String toString(long[] array)
          Turn this array into something suitable for printing.
static String toString(MouseEvent evt)
           
static String toString(Object obj)
           
static String toString(Object[] array)
          Turn this array into something suitable for printing.
static String toString(PathIterator it)
          Convert a path iterator into a human readable string.
static String toString(Polygon poly)
          This is because polygon is stupid.
static String toString(Rectangle2D rect)
          Print out a Rectangle decently.
static String toString(short[] array)
          Turn this array into something suitable for printing.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_FOLD_LENGTH

public static int DEFAULT_FOLD_LENGTH
The maximum number of characters per line.

SPACES

public static String SPACES
81 space characters.

foldtokens

public static String foldtokens
What characters to fold lines on. Currently whitespace only.

foldLength

public static int foldLength
How many spaces to fold on.
Method Detail

setFoldTokens

public static void setFoldTokens(String tokens)
Set the tokens to fold text on.
Parameters:
tokens - is a String containing each of the characters to fold on.

setFoldLength

public static void setFoldLength(int val)
Set the number of spaces to fold on.
Parameters:
val - is the number of spaces to fold on.

padAtFront

public static String padAtFront(String str,
                                int len)
Pad this String at the front so that it is at least the specified length.

padAtEnd

public static String padAtEnd(String str,
                              int len)
Pad this String at the end so that it is at least the specified length.

getNumRows

public static int getNumRows(String str)
Count the number of rows this String fills up, based on where the carriage returns '\n' are. (Yes, it does the last row correctly, if it has a '\n' or not).
Parameters:
str - is the String whose rows we will count.
Returns:
the number of rows this String will take to layout.
See Also:
getNumRows(java.lang.String, int)

getNumRows

public static int getNumRows(String str,
                             int numCols)
Count the number of rows this String fills up, taking into account that it may have a column limit (ie max num of chars per line).
Parameters:
str - is the String whose rows we will count.
numCols - is the number of columns (chars / row) to layout on.
Returns:
the number of rows this String will take to layout.
See Also:
getNumRows(java.lang.String)

getMaxNumCols

public static int getMaxNumCols(String str,
                                String eoln)
After laying out the String into rows, count the number of characters in the longest line.
Parameters:
str - is the String whose max columns we will count.
eoln - specifies what the end-of-line is. Should be either "\n" or "\r\n".
Returns:
the number of characters in the longest row.

getMaxNumCols

public static int getMaxNumCols(String str,
                                String eoln,
                                int numCols)
After laying out the String into rows, count the number of characters in the longest line. Calls the other getMaxNumCols() after folding the String according to the numCols.
Parameters:
str - is the String whose max columns we will count.
eoln - specifies what the end-of-line is. Should be either "\n" or "\r\n".
numCols - is the number of columns (chars / row) to layout on.
Returns:
the number of characters in the longest row.
See Also:
getMaxNumCols(java.lang.String, java.lang.String)

getMaxNumCols

public static int getMaxNumCols(String str)
After laying out the String into rows, count the number of characters in the longest line. Calls the other getMaxNumCols() using the system specific line separator.
Parameters:
str - is the String whose max columns we will count.
Returns:
the number of characters in the longest row.
See Also:
getMaxNumCols(java.lang.String, java.lang.String)

getMaxNumCols

public static int getMaxNumCols(String str,
                                int numCols)
After laying out the String into rows, count the number of characters in the longest line. Calls the other getMaxNumCols() using the system specific line separator.
Parameters:
str - is the String whose max columns we will count.
numCols - is the number of columns (chars / row) to layout on.
Returns:
the number of characters in the longest row.
See Also:
getMaxNumCols(java.lang.String, java.lang.String)

fold

public static String fold(Object obj1,
                          Object obj2)
Parameters:
strPrefix - is the description line.
strText - is the text to fold.
See Also:
setFoldTokens(java.lang.String), fold(java.lang.String, java.lang.String, java.lang.String, int)

fold

public static String fold(String strText,
                          int numCols)
See fold(). Uses a blank String as the prefix, and the system specific line separator as end of line..
Parameters:
strText - is the text to fold.
numCols - is the max number of characters per line.
See Also:
setFoldTokens(java.lang.String), setFoldLength(int), fold(java.lang.String, java.lang.String, java.lang.String, int)

fold

public static String fold(String strPrefix,
                          String strText,
                          int numCols)
See fold(). Uses the system specific line separator and the default fold length.
Parameters:
strPrefix - is the description line to prepend to every new line.
strText - is the text to fold.
numCols - is the max number of characters per line.
See Also:
setFoldTokens(java.lang.String), setFoldLength(int), fold(java.lang.String, java.lang.String, java.lang.String, int)

fold

public static String fold(String strPrefix,
                          String strText)
See fold(). Uses the system specific line separator and the default fold length.
Parameters:
strPrefix - is the description line to prepend to every new line.
strText - is the text to fold.
See Also:
setFoldTokens(java.lang.String), setFoldLength(int), fold(java.lang.String, java.lang.String, java.lang.String, int)

fold

public static String fold(String strPrefix,
                          String strText,
                          String eoln,
                          int numCols)
Fold the text around to the next line correctly. For example, given a prefix of "Estragon: " and text "Would you like a radish? It would help pass the time." the output would look something like:

    Estragon: Would you like a radish? It would help
              pass the time.
 
Parameters:
strPrefix - is the description line to prepend to every new line.
strText - is the text to fold.
eoln - is the end-of-line character to append.
numCols - is the max number of characters per line.
See Also:
setFoldTokens(java.lang.String)

align

public static final String align(String str,
                                 char ch)
Align text in a String into two columns. For example, we can convert:
    Date: 08/02/1999
    Name: John
    Occupation: Programmer
    Address: blah
 
...into...
    Date:       08/02/1999
    Name:       John
    Occupation: Programmer
    Address:    blah
 

containsChars

public static boolean containsChars(String str,
                                    String strChars)
See if a given String contains any of the specified characters.
Parameters:
str - is the String to look through.
strChars - is the set of characters to look for in str.

containsChars

public static boolean containsChars(String str,
                                    char[] chars)
See if a given String contains any of the specified characters.
Parameters:
str - is the String to look through.
chars - is the set of characters to look for in str.

isDecimalNumber

public boolean isDecimalNumber(String str)
See if a given String is a decimal number or not.
Parameters:
str - is the String to check.
Returns:
true if the String is a decimal number, false otherwise.

interpretString

public static String interpretString(String str)
Given a String literally containing escape sequences (including unicode ones), convert it into a String with the interpreted escape sequences in place.

For example, given the String "\r\n" which has 4 characters, convert it into a String containing the interpreted meanings of '\r' and '\n' with 2 characters.

Parameters:
str - is the String containing the escape sequences to interpret.

interpretEscapeSequence

public static char interpretEscapeSequence(String str)
Given a String containing an escape sequence, interpret what it is.
Parameters:
str - is a String containing an escape sequence.
Returns:
the character represented by the escape sequence.

reverseInterpretString

public static String reverseInterpretString(String str)
Convert all non-printing characters in the specified String into their escape sequence equivalents.
Parameters:
str - is the String that might contain some escape characters.
Returns:
a String with special characters replaced by escape sequence equivalents.

reverseInterpretEscapeSequence

public static String reverseInterpretEscapeSequence(byte b)
Convert non-printing characters into their escape sequence equivalents.
Parameters:
b - is a byte that might be an escape sequence character.
Returns:
a String containing the character or its escape sequence.

reverseInterpretEscapeSequence

public static String reverseInterpretEscapeSequence(char ch)
Convert non-printing characters into their escape sequence equivalents.
Parameters:
ch - is the character that might be an escape sequence character.
Returns:
a String containing the character or its escape sequence.

countNumOfChar

public static int countNumOfChar(char chSearch,
                                 String strLine)
Count the number of occurrences of a character in a String.
Parameters:
chSearch - is the character to search for.
strLine - is the String to search through.
Returns:
The number of times the character was found in the String.

countNumOfString

public static int countNumOfString(String strSearch,
                                   String strLine)
Count the number of occurrences of a String in a String.
Parameters:
strSearch - is the String to search for.
strLine - is the String to search through.
Returns:
The number of times the character was found in the String.

getBarredLine

public static String getBarredLine(char ch)
Repeat the character 80 times and return it as a String.

getBarredLine

public static String getBarredLine(String strText,
                                   char ch)
Given a String like "example" and a char like '=', return
 ===   example   =========...
 
where the line is exactly 80 chars long.

spaces

public static String spaces(int len)
Return a String full of spaces, up to 80.
Parameters:
len - is the number of spaces to return.

removeSubstring

public static String removeSubstring(char chFrom,
                                     char chTo,
                                     String strLine)
Remove all characters from a String starting from the first location of one character to the first location of another character.
Parameters:
chFrom - is the character to start deleting from.
chTo - is the character to end deleting.
strLine - is the String to remove characters from.
Returns:
A String with characters removed, unmodified if the delimiting characters do not exist in the String.

replaceSubstring

public static String replaceSubstring(String str,
                                      String strSearch,
                                      String strReplace)
Given a String, find all instances of the specified substring and replace it with the specified String.
Parameters:
str - is the String to search through.
strSearch - is the non-empty String to search for.
strReplace - is the String to replace strSearch with.

indent

public static String indent(String str,
                            int spaces)
Indent the String by pushing all of the rows of the String the specified number of spaces.
Parameters:
str - is the String to push over.
spaces - is the number of spaces to indent.

toString

public static final String toString(Object obj)

toString

public static final String toString(MouseEvent evt)

toString

public static final String toString(Iterator it)
Turn this Iterator into something suitable for printing.

toString

public static final String toString(Enumeration enum)
Turn this Enumeration into something suitable for printing.

toString

public static final String toString(Object[] array)
Turn this array into something suitable for printing.

toString

public static final String toString(boolean[] array)
Turn this array into something suitable for printing.

toString

public static final String toString(char[] array)
Turn this array into something suitable for printing.

toString

public static final String toString(byte[] array)
Turn this array into something suitable for printing.

toString

public static final String toString(short[] array)
Turn this array into something suitable for printing.

toString

public static final String toString(int[] array)
Turn this array into something suitable for printing.

toString

public static final String toString(long[] array)
Turn this array into something suitable for printing.

toString

public static final String toString(float[] array)
Turn this array into something suitable for printing.

toString

public static final String toString(double[] array)
Turn this array into something suitable for printing.

toString

public static final String toString(Rectangle2D rect)
Print out a Rectangle decently.

toString

public static final String toString(Polygon poly)
This is because polygon is stupid.

toString

public static final String toString(Ellipse2D ellipse)
Print out an ellipse.

getSegmentType

public static String getSegmentType(int val)
Given a segment type (in PathIterator, return a String with the kind of segment.

toString

public static final String toString(PathIterator it)
Convert a path iterator into a human readable string.

toString

public static final String toString(BasicStroke bstk)
Convert a BasicStroke object into a String.

main

public static void main(String[] argv)
                 throws Exception

Copyright Information