Sharkysoft home

lava.string
Class StringToolbox

java.lang.Object
  |
  +--lava.string.StringToolbox

public class StringToolbox
extends java.lang.Object

Miscellaneous string utilities.

Details: StringToolbox is a miscellaneous collection of functions for manipulating strings. Functions in this class are place here because they do not fit neatly into the function spaces represented by the other classes in this package.

Changes:

2000.12.21
Added looksLikeAnEmailAddress (String).

Author:
Sharky
See Also:
NumberString, PathToolbox, StringDecoder, StringEncoder, StringIndenter

Constructor Summary
StringToolbox()
           
 
Method Summary
static boolean contains(java.lang.String string, char c)
          Determines whether a string contains a character.
static boolean contains(java.lang.String string, java.lang.String substring)
          Determines whether a string contains a substring.
static int count(java.lang.String string, char c)
          Counts character occurrences.
static int count(java.lang.String string, java.lang.String substring)
          Counts substring occurences.
static int findDifference(java.lang.String s1, java.lang.String s2)
          Finds first difference.
static java.lang.String getCommonSuffix(java.lang.String s1, java.lang.String s2)
          Determines common suffix.
static java.lang.String left(java.lang.String s, int len)
           
static boolean looksLikeAnEmailAddress(java.lang.String s)
          Determines if string looks like an email address.
static java.lang.String minimizeSpaces(java.lang.String s)
          Removes excess spaces.
static java.lang.String repeat(char s, int n)
          Creates string by repeating character.
static java.lang.String repeat(java.lang.String s, int n)
          Creates string by repeating string.
static java.lang.String replace(java.lang.String source, java.lang.String before, java.lang.String after)
          Search and replace.
static java.lang.String right(java.lang.String s, int len)
           
static java.lang.String[] splitString(java.lang.String s, java.lang.String d)
          Divides string into tokens.
static java.lang.String toInitialCaps(java.lang.String s)
          Converts string to "title" case.
static java.lang.String trim(java.lang.String s, char c)
          Trims characters from both ends of string.
static java.lang.String trimTrailing(java.lang.String s, char c)
          Trims trailing characters.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StringToolbox

public StringToolbox()
Method Detail

contains

public static final boolean contains(java.lang.String string,
                                     char c)
Determines whether a string contains a character.

Details: contains determines whether c is contained within string, returning true if it is, false otherwise.

Parameters:
string - the string
c - the character
Returns:
true iff the string contains the substring
Since:
2001.03.09

contains

public static final boolean contains(java.lang.String string,
                                     java.lang.String substring)
Determines whether a string contains a substring.

Details: contains determines whether substring is contained within string, returning true if it is, false if it is not.

Parameters:
string - the string
substring - the substring
Returns:
true iff the string contains the substring
Since:
1999.04.16

count

public static final int count(java.lang.String string,
                              char c)
Counts character occurrences.

Details: count determines the number of occurrences of a character in a string.

Parameters:
string - the string to search
c - the character
Returns:
the number of occurrences

count

public static final int count(java.lang.String string,
                              java.lang.String substring)
Counts substring occurences.

Details: count determines the number of occurrences of a substring string inside another string. Overlapping substrings are counted. For example, the call

count ("banana", "ana")

returns 2.

Parameters:
string - the string to search
substring - the substring
Returns:
the number of occurrences

findDifference

public static final int findDifference(java.lang.String s1,
                                       java.lang.String s2)
Finds first difference.

Details: findDifference compares two strings, character for character, starting with the first character, and scanning until a character mismatch is encountered, or until the shorter string has run out of characters. If a mismatch is encountered, the index of the mismatch is returned. If no mismatches are found but the strings have unequal lengths, the length of the shorter string is returned. If no mismatch is found and the strings have equal length, then the strings are identical and -1 is returned.

Parameters:
s1 - the first string
s2 - the second string
Returns:
the index of mismatch
Since:
2000.02.16

getCommonSuffix

public static final java.lang.String getCommonSuffix(java.lang.String s1,
                                                     java.lang.String s2)
Determines common suffix.

Details: getCommonSuffix returns the longest string that is a suffix to both given strings.

Parameters:
s1 - the first string
s2 - the second string
Returns:
the suffix
Since:
2001.03.09

left

public static java.lang.String left(java.lang.String s,
                                    int len)

minimizeSpaces

public static final java.lang.String minimizeSpaces(java.lang.String s)
Removes excess spaces.

Details: minimizeSpaces removes excess white spaces from the ends and middle of the given string. Space characters are identified as StringTokenizer identifies them. A space is considered excess if any of the following conditions are true:

All white spaces are converted to normal spaces in the output string.

Parameters:
s - the string to prune
Returns:
the pruned string

repeat

public static final java.lang.String repeat(char s,
                                            int n)
Creates string by repeating character.

Details: repeat creates a new string by repeating a character. For example, the call

repeat ('#', 3)

produces the string "###". A zero-length string is returned if the repeat count is less than 1.

Parameters:
s - the character to repeat
n - the repeat count
Returns:
the new string containing repeated characters

repeat

public static final java.lang.String repeat(java.lang.String s,
                                            int n)
Creates string by repeating string.

Details: repeat creates a new String by repeating and concatenating the contents of another String (s). For example, the call

repeat ("Hello", 3)

produces the string "HelloHelloHello". repeat returns a zero-length string if the repeat count (n) is less than 1.

Parameters:
s - the string to repeat
n - the repeat count
Returns:
the new string containing repeated substrings

replace

public static java.lang.String replace(java.lang.String source,
                                       java.lang.String before,
                                       java.lang.String after)
Search and replace.

Details: replace scans source for substrings matching before, from left to right, and replaces all occurrences found with after. If a matching substring is found, the replacement is made and then the scan picks up after the last character of the substring. The result is returned in a new string. If no replacements were made, the original String instance is returned.

Parameters:
source - the source string
before - the substring to search for
after - what to replace before with
Returns:
the new string
Since:
2000.01.30

right

public static java.lang.String right(java.lang.String s,
                                     int len)

splitString

public static final java.lang.String[] splitString(java.lang.String s,
                                                   java.lang.String d)
Divides string into tokens.

Details: splitString breaks a string (s) into an array of substrings with the delimeters removed. The delimeter characters are listed in a separate string (d).

java.util.StringTokenizer offers similar functionality. This implementation differs by the fact that it returns all of the tokens at once, using a String array, and does not require the explicit creation of a tokenizer object or enumeration handling.

Parameters:
s - the string to split
d - the delimeters
Returns:
the substrings of s

toInitialCaps

public static final java.lang.String toInitialCaps(java.lang.String s)
Converts string to "title" case.

Details: This method converts the first character of every run of letters in the given string (s) to upper case, making the string appear in "title" format. For the purposes of conversion, apostrophes occuring immediately after letters are also treated as letters. This method may not be appropriate for some strings, particularly those containing Roman numerals and other words where more than one character should be capitalized.

Examples:

MacDonald's Macdonald's
yo-yos Yo-Yos
part Part
III Iii
Parameters:
s - the string to convert
Returns:
the converted string
Since:
2000.01.15

trim

public static final java.lang.String trim(java.lang.String s,
                                          char c)
Trims characters from both ends of string.

Details: trim removes the given character (c) from both ends of the given string (s). For example, the call

trimTrailing ("sizes", 's')

returns the string "ize".

Parameters:
s - the string to edit
n - the character to trim
Returns:
the trimmed string

trimTrailing

public static final java.lang.String trimTrailing(java.lang.String s,
                                                  char c)
Trims trailing characters.

Details: trimTrailing trims the specified character (c) from the end of the given string (s) if the string ends with one or more repetitions of that character. For example, the call

removeTrailing ("Tennessee", 'e')

returns the string "Tenness".

Parameters:
s - the string to edit
n - the trailing character to remove
Returns:
a new string with the trailing characters removed

looksLikeAnEmailAddress

public static final boolean looksLikeAnEmailAddress(java.lang.String s)
Determines if string looks like an email address.

Details: looksLikeAnEmailAddress analyzes the given string (s) and determines whether it looks like an email address. If it does, looksLikeAnEmailAddress returns true, false otherwise.

See lava.io.StreamParser.tryEmailAddress for more information on the forms of email addresses accepted by this function.

Parameters:
s - the string to test
Returns:
true iff s looks like an email address
Since:
2000.12.21

Sharkysoft home