com.cleancode.util
Class StringPlus

java.lang.Object
  extended by com.cleancode.util.StringPlus

public final class StringPlus
extends Object

Convenience methods for string handling.

Since:
CleanCode 0.9
Version:
$Revision: 380 $
Author:
Michael Sorens

Field Summary
static char DEFAULT_BORDER_CHAR
          Default character replicated to create a top and bottom border.
static int DEFAULT_WIDTH
          Default width of borders for various hilight methods.
static String NL
          System dependent line separator.
static String VERSION
          Current version of this class.
 
Method Summary
static String delimit(String s)
          Wraps a string with double quotes.
static double dollarToNumber(String amt)
          Converts a monetary string to a number.
static String getClassName(Object anObject)
          Returns the class name for an object, without the package path.
static String getThreadName()
          Returns the thread name of the current thread.
static String hilight(String s)
          Delimits a text block with a top and bottom border using a default length and default border character.
static String hilight(String s, int len)
          Delimits a String with a top and bottom border using a default border character.
static String hilight(String s, int len, char borderChar)
          Delimits a text block with a top and bottom border.
static String hilight(String title, String s)
          Delimits a text block with a top and bottom border, including title, using a default length and default border character.
static String hilight(String title, String s, int len)
          Delimits a text block with a top and bottom border, including title, using a default border character.
static String hilight(String title, String s, int len, char borderChar)
          Delimits a text block with a top and bottom border, including title.
static String hilightInline(String s)
          Adds border lines around text (for a single line of output) using a default length and default border character.
static String hilightInline(String s, int len)
          Adds border lines around text (for a single line of output) using a default border character.
static String hilightInline(String s, int len, char borderChar)
          Adds border lines around text (for a single line of output).
static String replicate(char c, int len)
          Replicates a character into a string of a specified length.
static String showKeys(Properties p)
          Creates a comma separated list of keys of a Properties object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_BORDER_CHAR

public static final char DEFAULT_BORDER_CHAR
Default character replicated to create a top and bottom border.

See Also:
Constant Field Values

DEFAULT_WIDTH

public static final int DEFAULT_WIDTH
Default width of borders for various hilight methods.

See Also:
Constant Field Values

NL

public static final String NL
System dependent line separator.


VERSION

public static final String VERSION
Current version of this class.

Method Detail

delimit

public static String delimit(String s)
Wraps a string with double quotes. If the String is null, returns the empty string.

Parameters:
s - a string
Returns:
the string delimited with double quotes

getClassName

public static String getClassName(Object anObject)
Returns the class name for an object, without the package path.

Parameters:
anObject - any Object
Returns:
class name of the Object

getThreadName

public static String getThreadName()
Returns the thread name of the current thread.

Returns:
thread name of the current thread

hilight

public static String hilight(String s,
                             int len,
                             char borderChar)
Delimits a text block with a top and bottom border. The resultant string consists of a border, a line separator, the string, a border, and a line separator. If the string does not contain any line separators, one is inserted at the end, so that the bottom border starts at the beginning of a line. If the string contains a line separator, no additional one is added to its end. Example: hilight("my list of stuff", 25, "+") yields
   +++++++++++++++++++++++++
    my list of stuff
   +++++++++++++++++++++++++
 

Parameters:
s - a string to delimit
len - length of border
borderChar - character to use for border
Returns:
string highlighted with borders
See Also:
hilight

hilight

public static String hilight(String title,
                             String s,
                             int len,
                             char borderChar)
Delimits a text block with a top and bottom border, including title. The resultant string consists of a border with a title, a line separator, the string, a border, and a line separator. If the string does not contain any line separators, one is inserted at the end, so that the bottom border starts at the beginning of a line. If the string contains a line separator, no additional one is added to its end. Example: hilight("Category A", "my list of stuff", 25, "+") yields
   +++++++ Category A ++++++
   my list of stuff
   +++++++++++++++++++++++++
 

Parameters:
s - a string to delimit
len - length of border
borderChar - character to use for border
title - string to use in top border
Returns:
string highlighted with borders
See Also:
hilight

hilight

public static String hilight(String s)
Delimits a text block with a top and bottom border using a default length and default border character. The defaults are: 75 characters long, and the equals (=) character.

Parameters:
s - a string to delimit
Returns:
string highlighted with borders
See Also:
hilight

hilight

public static String hilight(String s,
                             int len)
Delimits a String with a top and bottom border using a default border character. The default is the equals (=) character.

Parameters:
s - a string to delimit
len - length of border
Returns:
string highlighted with borders
See Also:
hilight

hilight

public static String hilight(String title,
                             String s)
Delimits a text block with a top and bottom border, including title, using a default length and default border character. The defaults are: 75 characters long, and the equals (=) character.

Parameters:
title - string to use in top border
s - a string to delimit
Returns:
string highlighted with borders
See Also:
hilight

hilight

public static String hilight(String title,
                             String s,
                             int len)
Delimits a text block with a top and bottom border, including title, using a default border character. The default is the equals (=) character.

Parameters:
title - string to use in top border
s - a string to delimit
len - length of border
Returns:
string highlighted with borders
See Also:
hilight

hilightInline

public static String hilightInline(String s,
                                   int len,
                                   char borderChar)
Adds border lines around text (for a single line of output). Example: hilight("my title", 25, "+") yields
    +++++++ my title ++++++++
 
Effectively a string of the borderChar characters is constructed of length len, then the msg is overlaid in the middle with a space on either side. If the msg itself is longer than the specified len, then just a single borderChar plus a space is added to both ends.

Parameters:
s - a string to delimit
len - length of border
borderChar - character to use for border
Returns:
string highlighted with borders

hilightInline

public static String hilightInline(String s,
                                   int len)
Adds border lines around text (for a single line of output) using a default border character. The default is the equals (=) character.

Parameters:
s - a string to delimit
len - length of border
Returns:
string highlighted with borders
See Also:
hilightInLine

hilightInline

public static String hilightInline(String s)
Adds border lines around text (for a single line of output) using a default length and default border character. The defaults are: 75 characters long, and the equals (=) character.

Parameters:
s - a string to delimit
Returns:
string highlighted with borders
See Also:
hilightInLine

replicate

public static String replicate(char c,
                               int len)
Replicates a character into a string of a specified length. Operates similar to the Perl replicate operator "x".

Parameters:
c - character to replicate
len - length of string to create
Returns:
string of character c

showKeys

public static String showKeys(Properties p)
Creates a comma separated list of keys of a Properties object.

Parameters:
p - Properties object
Returns:
comma-separated list as a string

dollarToNumber

public static double dollarToNumber(String amt)
Converts a monetary string to a number. Strips dollar sign, parentheses, and commas, if present, to convert the string representing a dollar figure into a raw number. The amt may or may not include any of these monetary characters: hyphen or parentheses to indicate a negative value, a dollar sign ($), a decimal point, and commas. Hence any of the following will return the relevant number (assuming "d" is any digit): $dd.dd, -.dd, -$dd, $dd,ddd.dd, and ($dd.dd). This method tries to be as forgiving as possible, so even a string such as "$101.45 / EA" will return a number. That is, the first possible run of contiguous monetary characters (specified above) will be parsed and returned.

Parameters:
amt - string representing a dollar amount
Returns:
float; zero if no recognizable number


CleanCode Java Libraries Copyright © 2001-2012 Michael Sorens - Revised 2012.12.10 Get CleanCode at SourceForge.net. Fast, secure and Free Open Source software downloads