org.freeshell.zs.common
Class StringManipulator

java.lang.Object
  extended by org.freeshell.zs.common.StringManipulator

public class StringManipulator
extends java.lang.Object

Perform string manipulation.


Nested Class Summary
static class StringManipulator.Token
          Represent a string token.
 
Constructor Summary
StringManipulator()
           
 
Method Summary
static java.lang.String abbreviate(java.lang.String s)
          Abbreviate a specified string, by keeping only the first letter of each word.
static java.lang.String centerJustify(java.lang.Object o, int width)
          Center-justify the string representation of a given object, padding with leading and trailing spaces so that its length is at least the specified width.
static java.lang.String deleteExtraWhitespace(java.lang.String s)
          Delete extra whitespace in a specified string, by replacing contiguous whitespace characters with a single space.
static java.lang.String deletePunctuation(java.lang.String s)
          Delete punctuation in a specified string, by deleting all punctuation characters.
static java.lang.String deleteWhitespace(java.lang.String s)
          Delete whitespace in a specified string, by deleting all whitespace characters.
static java.lang.String formattedDouble(double n)
          Return a formatted string representation of a given double number (format is locale-sensitive).
static java.lang.String formattedLong(long n)
          Return a formatted string representation of a given long number (format is locale-sensitive).
static java.lang.String leftJustify(java.lang.Object o, int width)
          Left-justify the string representation of a given object, padding with trailing spaces so that its length is at least the specified width.
static java.lang.String leftTrim(java.lang.String s)
          Trim away whitespace on the left.
static java.lang.String[] parseKeyValueString(java.lang.String s)
          Parse the specified "key:value" string.
static java.lang.String repeat(java.lang.Object o, int n)
          Repeat the string representation of a given object, a specified number of times.
static java.lang.String reverse(java.lang.String s)
          Reverse the string.
static java.lang.String rightJustify(java.lang.Object o, int width)
          Right-justify the string representation of a given object, padding with leading spaces so that its length is at least the specified width.
static java.lang.String rightTrim(java.lang.String s)
          Trim away whitespace on the right.
static java.lang.String spaceOutWords(java.lang.String s)
          Space out words in a specified string, by inserting a single space between concatenated words.
static java.lang.String substring(java.lang.String in, java.lang.String format, char rangeChar, char delimChar)
          Extract a substring from a given string, according to the specified format.
static java.lang.String swapCase(java.lang.String s)
          Swap the case of a specified string, by converting lower case characters to upper case and vice versa.
static StringManipulator.Token[] tokenize(java.lang.String in, java.lang.String regexDelim, boolean includeDelimiters)
          Tokenize the given string, using the specified regex delimiter pattern.
static java.lang.String toTitleCase(java.lang.String s)
          Convert a specified string to title case, by capitalizing only the first letter of each word.
static java.lang.String trimLeadingStrings(java.lang.String s, java.lang.String[] trimStrings)
          Return a modified version of the input string with the specified strings trimmed off from its head, if found.
static java.lang.String trimStrings(java.lang.String s, java.lang.String[] trimStrings)
          Return a modified version of the input string with the specified strings trimmed off from its head and tail, if found.
static java.lang.String trimTrailingStrings(java.lang.String s, java.lang.String[] trimStrings)
          Return a modified version of the input string with the specified strings trimmed off from its tail, if found.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StringManipulator

public StringManipulator()
Method Detail

tokenize

public static StringManipulator.Token[] tokenize(java.lang.String in,
                                                 java.lang.String regexDelim,
                                                 boolean includeDelimiters)
Tokenize the given string, using the specified regex delimiter pattern.

Parameters:
in - String to be tokenized
regexDelim - Regex delimiter pattern
includeDelim - If true, then delimiter tokens are returned too; otherwise, only non-delimiter tokens are returned
Returns:
Tokens in an array of strings; null, if the string to be tokenized is null

substring

public static java.lang.String substring(java.lang.String in,
                                         java.lang.String format,
                                         char rangeChar,
                                         char delimChar)
Extract a substring from a given string, according to the specified format.

Parameters:
in - String from which the substring is to be extracted
format - Format string describing the sequence of characters in the substring
rangeChar - Range character to be used
delimChar - Delimiter character to be used
Returns:
Substring of the given string

formattedLong

public static java.lang.String formattedLong(long n)
Return a formatted string representation of a given long number (format is locale-sensitive).

Parameters:
n - Long number to be formatted
Returns:
Formatted string representation of the given long number

formattedDouble

public static java.lang.String formattedDouble(double n)
Return a formatted string representation of a given double number (format is locale-sensitive).

Parameters:
n - Double number to be formatted
Returns:
Formatted string representation of the given double number

centerJustify

public static java.lang.String centerJustify(java.lang.Object o,
                                             int width)
Center-justify the string representation of a given object, padding with leading and trailing spaces so that its length is at least the specified width.

Parameters:
o - Object to be center-justified
width - Width of the resulting center-justified string
Returns:
Center-justified string representation

leftJustify

public static java.lang.String leftJustify(java.lang.Object o,
                                           int width)
Left-justify the string representation of a given object, padding with trailing spaces so that its length is at least the specified width.

Parameters:
o - Object for to be left-justified
width - Width of the resulting left-justified string
Returns:
Left-justified string representation

rightJustify

public static java.lang.String rightJustify(java.lang.Object o,
                                            int width)
Right-justify the string representation of a given object, padding with leading spaces so that its length is at least the specified width.

Parameters:
o - Object to be right-justified
width - Width of the resulting right-justified string
Returns:
Right-justified string representation

repeat

public static java.lang.String repeat(java.lang.Object o,
                                      int n)
Repeat the string representation of a given object, a specified number of times.

Parameters:
o - Object to be repeated
n - Number of times to repeat
Returns:
Repeated string representation

toTitleCase

public static java.lang.String toTitleCase(java.lang.String s)
Convert a specified string to title case, by capitalizing only the first letter of each word.

Parameters:
s - Input string
Returns:
Output string

abbreviate

public static java.lang.String abbreviate(java.lang.String s)
Abbreviate a specified string, by keeping only the first letter of each word.

Parameters:
s - Input string
Returns:
Output string

reverse

public static java.lang.String reverse(java.lang.String s)
Reverse the string.

Parameters:
s - Input string
Returns:
Output string

leftTrim

public static java.lang.String leftTrim(java.lang.String s)
Trim away whitespace on the left.

Parameters:
s - Input string
Returns:
Output string

rightTrim

public static java.lang.String rightTrim(java.lang.String s)
Trim away whitespace on the right.

Parameters:
s - Input string
Returns:
Output string

deleteExtraWhitespace

public static java.lang.String deleteExtraWhitespace(java.lang.String s)
Delete extra whitespace in a specified string, by replacing contiguous whitespace characters with a single space.

Parameters:
s - Input string
Returns:
Output string

deleteWhitespace

public static java.lang.String deleteWhitespace(java.lang.String s)
Delete whitespace in a specified string, by deleting all whitespace characters.

Parameters:
s - Input string
Returns:
Output string

deletePunctuation

public static java.lang.String deletePunctuation(java.lang.String s)
Delete punctuation in a specified string, by deleting all punctuation characters.

Parameters:
s - Input string
Returns:
Output string

spaceOutWords

public static java.lang.String spaceOutWords(java.lang.String s)
Space out words in a specified string, by inserting a single space between concatenated words.

Parameters:
s - Input string
Returns:
Output string

swapCase

public static java.lang.String swapCase(java.lang.String s)
Swap the case of a specified string, by converting lower case characters to upper case and vice versa.

Parameters:
s - Input string
Returns:
Output string

trimTrailingStrings

public static java.lang.String trimTrailingStrings(java.lang.String s,
                                                   java.lang.String[] trimStrings)
Return a modified version of the input string with the specified strings trimmed off from its tail, if found. The returned string is guaranteed not to end with any of the specified strings.

Parameters:
s - Input string
trimStrings - Strings to be trimmed off from the end of the input string; empty strings are ignored
Returns:
Modified version of input string

trimLeadingStrings

public static java.lang.String trimLeadingStrings(java.lang.String s,
                                                  java.lang.String[] trimStrings)
Return a modified version of the input string with the specified strings trimmed off from its head, if found. The returned string is guaranteed not to start with any of the specified strings.

Parameters:
s - Input string
trimStrings - Strings to be trimmed off from the head of the input string; empty strings are ignored
Returns:
Modified version of input string

trimStrings

public static java.lang.String trimStrings(java.lang.String s,
                                           java.lang.String[] trimStrings)
Return a modified version of the input string with the specified strings trimmed off from its head and tail, if found. The returned string is guaranteed not to start or end with any of the specified strings.

Parameters:
s - Input string
trimStrings - Strings to be trimmed off from the head and tail of the input string; empty strings are ignored
Returns:
Modified version of input string

parseKeyValueString

public static java.lang.String[] parseKeyValueString(java.lang.String s)
Parse the specified "key:value" string. The key string is the trimmed string before the first colon (:). The value string is the trimmed string after the first colon (:); if this is enclosed in double quotes ("), then the value string is the string between them. If the specified string does not contain a colon (:), then the key string is the entire string trimmed, and the value string is the empty string.

Parameters:
s - string to be parsed
Returns:
String array containing two elements: the first is the key string, the second is the value string