mckay.utilities.staticlibraries
Class StringMethods

java.lang.Object
  extended by mckay.utilities.staticlibraries.StringMethods

public class StringMethods
extends java.lang.Object

A holder class for static methods relating to strings.


Constructor Summary
StringMethods()
           
 
Method Summary
static boolean areAllEntriesIdentical(java.lang.String[] test_array)
          Checks the given array of strings to see if all of the contents are identical.
static java.lang.String[] breakIntoTokens(java.lang.String input, java.lang.String delimiter)
          Breaks the given input string into tokens based on the given delimiter, and returns an array where each entry is a token.
static int calculateLevenshteinDistance(java.lang.String first_string, java.lang.String second_string)
          Calculates the Levenshtein Distance (edit distance) between the two given strings.
static int[][] calculateLevenshteinDistances(java.lang.String[] strings)
          Calculates the Levenshtein Distance (edit distance) between each pair of strings in the given array.
static java.lang.String concatenateArrayOfStrings(java.lang.String[] words)
          Combines all the Strings of the given array into one String.
static java.lang.String convertFilePathToFileName(java.lang.String file_path)
          Returns the name of the file referred to by the given path.
static boolean doStringArraysMatch(java.lang.String[] array1, java.lang.String[] array2)
          Returns true if the two given arrays contain exactly the same strings, but in any order in the array.
static java.lang.String getBeginningOfString(java.lang.String string_to_shorten, int number_characters)
          Returns a copy of the given string with all but the first number_characters eliminated.
static java.lang.String getBeginningOfStringWithHyphenFiller(java.lang.String string_to_shorten, int number_characters)
          Returns a copy of the given string with all but the first number_characters eliminated.
static java.lang.String getDirectoryName(java.lang.String file_path)
          Returns the name of the directory that the given filename is found in.
static java.lang.String getDoubleInScientificNotation(double number_to_round, int significant_digits)
          Returns a formatted version of number_to_round that has been converted to scientific notation and includes the given number of significant_digits.
static java.lang.String getExtension(java.lang.String filename)
          Returns the 2, 3 or 4 letter extension of the given file name.
static int getIndexOfLongestString(java.lang.String[] to_check)
          Return the index of the longest string in the given array.
static int getIndexOfString(java.lang.String given_name, java.lang.String[] possible_names)
          Returns the index in the possible_names array where the given_name parameter occurs.
static int getIntInLimits(java.lang.String test_string, int min, int max)
          Tests if the given string is an integer within the given bounds, and returns it as an integer if it is.
static int getLengthOfLongerString(java.lang.String first, java.lang.String second)
          Returns the length of the longer of the two given strings.
static int getLengthOfShorterString(java.lang.String first, java.lang.String second)
          Returns the length of the shorter of the two given strings.
static java.lang.String getNumberFormattedWithCommas(int number)
          Formats the given number with a comma every three digits.
static java.lang.String getNumberFormattedWithCommas(long number)
          Formats the given number with a comma every three digits.
static int getNumberOccurencesOfSubString(java.lang.String given_string, java.lang.String substring)
          Returns the number of times that the given substring occurs in the given_string.
static java.lang.String getRoundedDouble(double number_to_round, int decimal_places)
          Returns a formatted version of number_to_round.
static java.lang.String getRoundedDoubleWithCommas(double number_to_round, int decimal_places)
          Returns a formatted version of number_to_round.
static boolean isStringInArray(java.lang.String test_string, java.lang.String[] array_to_check)
          Checks to see if a given string is the same as an entry in the given array.
static java.lang.String nullifyStringIfEmpty(java.lang.String given_string)
          Returns null if the given string is empty and returns the original string if it is not empty.
static void printDoubleArray(java.lang.String[][] test_strings, int number_tabs, java.lang.String open_marker, java.lang.String close_marker)
          Prints each outer entry of the given string array to standard out on a different line and each inner entry on the same line.
static void printStringArrayContents(java.lang.String[] test_strings)
          Prints each entry of the given string array to standard out on a different line.
static void printStringArrayContentsOnOneLine(java.lang.String[] test_strings, int number_tabs, java.lang.String open_marker, java.lang.String close_marker)
          Prints each entry of the given string array to standard out on the same line.
static java.lang.String[] removeDoubles(java.lang.String[] strings)
          Returns a shortened copy of the given array of strings with all duplicate entries removed.
static java.lang.String[] removeEmptyStringsFromArray(java.lang.String[] array)
          Given an array of Strings, this method removes empty cells from the array.
static java.lang.String removeExtension(java.lang.String filename)
          Returns a copy of the given string with the extension removed.
static java.lang.String replaceExtension(java.lang.String filename, java.lang.String new_extension)
          Returns a copy of the given file name with its original extension stripped away and replaced by the new specified extension.
static void reportStringDifferences(java.lang.String key, java.lang.String first, java.lang.String second, java.util.Vector<java.lang.String[]> to_update)
          Compares two strings to see if they are the same, taking into account the possibility that they may be null.
static boolean testIfStringIsAnInt(java.lang.String test_string)
          Tests if the given string is an integer (e.g.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StringMethods

public StringMethods()
Method Detail

printDoubleArray

public static void printDoubleArray(java.lang.String[][] test_strings,
                                    int number_tabs,
                                    java.lang.String open_marker,
                                    java.lang.String close_marker)
Prints each outer entry of the given string array to standard out on a different line and each inner entry on the same line. Each inner entry is marked on the left with open_marker and on the right by close_marker. Each line begins with number_tabs tabs. NULL is printed if test_strings is null. The first thing printed is the number of outer elements.

Parameters:
test_strings - The array to print.
number_tabs - The number of tabs to start the line with.
open_marker - The marker denoting the start of an element.
close_marker - The marker denoting the end of an element.

printStringArrayContentsOnOneLine

public static void printStringArrayContentsOnOneLine(java.lang.String[] test_strings,
                                                     int number_tabs,
                                                     java.lang.String open_marker,
                                                     java.lang.String close_marker)
Prints each entry of the given string array to standard out on the same line. Each entry is marked on the left with open_marker and on the right by close_marker. Each line begins with number_tabs tabs. NULL is printed if test_strings is null.

Parameters:
test_strings - The array to print.
number_tabs - The number of tabs to start the line with.
open_marker - The marker denoting the start of an element.
close_marker - The marker denoting the end of an element.

printStringArrayContents

public static void printStringArrayContents(java.lang.String[] test_strings)
Prints each entry of the given string array to standard out on a different line. Each line is numbered by its corresponding array index, and is contained in >< markers.

Parameters:
test_strings - The array of strings to pring.

breakIntoTokens

public static java.lang.String[] breakIntoTokens(java.lang.String input,
                                                 java.lang.String delimiter)
Breaks the given input string into tokens based on the given delimiter, and returns an array where each entry is a token.

Parameters:
input - The string to break into tokens.
delimiter - The string that must occur wherever tokens are to be separated.
Returns:
The tokens. Null is returned if either input or delimiter is null.

isStringInArray

public static boolean isStringInArray(java.lang.String test_string,
                                      java.lang.String[] array_to_check)
Checks to see if a given string is the same as an entry in the given array.

Parameters:
test_string - The string to search the array_to_check array for.
array_to_check - The array to search to see if test_string is an entry.
Returns:
True if test_string is the same of at least one entry of array_to_check, false otherwise.

areAllEntriesIdentical

public static boolean areAllEntriesIdentical(java.lang.String[] test_array)
Checks the given array of strings to see if all of the contents are identical. If they are, then true is returned. If the are not, then false is returned. True is returned if the array is of size one. The given array may contain some or all entries with values of null.

Parameters:
test_array - The array of strings to test.
Returns:
Whether or not all entries are identical.

getIndexOfString

public static int getIndexOfString(java.lang.String given_name,
                                   java.lang.String[] possible_names)
                            throws java.lang.Exception
Returns the index in the possible_names array where the given_name parameter occurs. Throws an exception if it is not there.

Parameters:
given_name - The string to search for in possible_names.
possible_names - The array to search for given_names in.
Returns:
The index in possible_names that contains given_name.
Throws:
java.lang.Exception - Throws an exception if if given_name is not in the possible_names array.

getIndexOfLongestString

public static int getIndexOfLongestString(java.lang.String[] to_check)
Return the index of the longest string in the given array. If multiple strings have the same length, then the fist one is returned.

Parameters:
to_check - The strings to check.
Returns:
The index in to_check of the longest string.

getLengthOfLongerString

public static int getLengthOfLongerString(java.lang.String first,
                                          java.lang.String second)
Returns the length of the longer of the two given strings.

Parameters:
first - One string to compare.
second - The other string to compare.
Returns:
The length of the longer string. Returns -1 if either string is null.

getLengthOfShorterString

public static int getLengthOfShorterString(java.lang.String first,
                                           java.lang.String second)
Returns the length of the shorter of the two given strings.

Parameters:
first - One string to compare.
second - The other string to compare.
Returns:
The length of the shorter string. Returns -1 if either string is null.

doStringArraysMatch

public static boolean doStringArraysMatch(java.lang.String[] array1,
                                          java.lang.String[] array2)
Returns true if the two given arrays contain exactly the same strings, but in any order in the array. Returns false if this is not the case (i.e. if either of the arrays contains a string that the other does not).

Parameters:
array1 - The array to compare with array2.
array2 - The array to compare with array1.
Returns:
Whether or not the two arrays have matching strings.

removeDoubles

public static java.lang.String[] removeDoubles(java.lang.String[] strings)
Returns a shortened copy of the given array of strings with all duplicate entries removed. The original array of strings is not changed.

Parameters:
strings - The array of strings to remove duplicate entries from.
Returns:
A shortened copy of the given strings with duplicates removed.

getNumberOccurencesOfSubString

public static int getNumberOccurencesOfSubString(java.lang.String given_string,
                                                 java.lang.String substring)
Returns the number of times that the given substring occurs in the given_string. Returns 0 if either of these strings are empty.

Parameters:
given_string - The string to search.
substring - The string to search given_string for.
Returns:
The number of times that substring occurs in given_string.

concatenateArrayOfStrings

public static java.lang.String concatenateArrayOfStrings(java.lang.String[] words)
Combines all the Strings of the given array into one String. Each of the Strings that were previously stored in separate cells of the array are separated by a comma and a space in the new String.

Parameters:
words - The Strings to be concatenated.
Returns:
A single String containing all the Strings from the array separated by a comma and a space.

removeEmptyStringsFromArray

public static java.lang.String[] removeEmptyStringsFromArray(java.lang.String[] array)
Given an array of Strings, this method removes empty cells from the array. Empty is defined as either being null or having a length of zero.

Parameters:
array - Array from which to remove empty cells.
Returns:
New array containing only the cells of the given array that were not empty. Null is returned if the given array is null or if all entries are empty.

calculateLevenshteinDistance

public static int calculateLevenshteinDistance(java.lang.String first_string,
                                               java.lang.String second_string)
Calculates the Levenshtein Distance (edit distance) between the two given strings. This distance is defined as the minimum number of operations needed to transform one string into the other, where a single operation consists of an insertion, deletion or substitution of a single character. All three types of operations are assigned equal weights.

Parameters:
first_string - A string to compare to second_string.
second_string - A string to compare to first_string.
Returns:
The edit distance between first_string and second_string. If either string is null, then -1 is returned.

calculateLevenshteinDistances

public static int[][] calculateLevenshteinDistances(java.lang.String[] strings)
Calculates the Levenshtein Distance (edit distance) between each pair of strings in the given array. This distance is defined as the minimum number of operations needed to transform one string into the other, where a single operation consists of an insertion, deletion or substitution of a single character. All three types of operations are assigned equal weights.

Parameters:
strings - The strings to compare.
Returns:
The distances between each pair of strings in the given array. Both dimensions correspond to the strings in the same order as they are found in the given array. If either string being compared is null, then -1 its entry in the matrix is set to -1.

reportStringDifferences

public static void reportStringDifferences(java.lang.String key,
                                           java.lang.String first,
                                           java.lang.String second,
                                           java.util.Vector<java.lang.String[]> to_update)
Compares two strings to see if they are the same, taking into account the possibility that they may be null. Updates the provided to_update Vector with the difference if they are different.

Parameters:
key - A description of the strings being compared.
first - The first string to comare.
second - The second string to compare.
to_update - The Vector to add the difference report to. Nothing is added if first and second are the same. If they are different, then a String array of size 3 is added to to_update. The first entry is key, the second is first and the third is second.

getBeginningOfString

public static java.lang.String getBeginningOfString(java.lang.String string_to_shorten,
                                                    int number_characters)
Returns a copy of the given string with all but the first number_characters eliminated. If the given string is shorter than number_characters, then blank spaces are added to the end of the string in order to make it the full length.

Parameters:
string_to_shorten - The string to be shortened or have spaces added to its end.
number_characters - Number of characters in the new string.
Returns:
The shortened string.

getBeginningOfStringWithHyphenFiller

public static java.lang.String getBeginningOfStringWithHyphenFiller(java.lang.String string_to_shorten,
                                                                    int number_characters)
Returns a copy of the given string with all but the first number_characters eliminated. If the given string is shorter than number_characters, then hyphens are are added to the end of the string in order to make it the full length (with two blank spaces on each side.

Parameters:
string_to_shorten - The string to be shortened or have spaces added to its end.
number_characters - Number of characters in the new string.
Returns:
The shortened string.

nullifyStringIfEmpty

public static java.lang.String nullifyStringIfEmpty(java.lang.String given_string)
Returns null if the given string is empty and returns the original string if it is not empty.

Parameters:
given_string - The string to examine.
Returns:
The original string or null.

convertFilePathToFileName

public static java.lang.String convertFilePathToFileName(java.lang.String file_path)
Returns the name of the file referred to by the given path.

Parameters:
file_path - The file path from which the file name is to be extracted.
Returns:
The name of the file referred to in the parameter.

getDirectoryName

public static java.lang.String getDirectoryName(java.lang.String file_path)
                                         throws java.lang.Exception
Returns the name of the directory that the given filename is found in. Throws an exception if no valid directory separator is present.

Parameters:
file_path - The path to extract the directory from.
Returns:
The directory name.
Throws:
java.lang.Exception - Throws an exception if no valid directory separator is present.

removeExtension

public static java.lang.String removeExtension(java.lang.String filename)
Returns a copy of the given string with the extension removed. Returns null if there is no extension or if there are less than five characters in the string.

IMPORTANT: filename should consist of at least four characters.

Parameters:
filename - The name of the file from which the extension is to be removed.
Returns:
The name of the file with the extension removed.

getExtension

public static java.lang.String getExtension(java.lang.String filename)
Returns the 2, 3 or 4 letter extension of the given file name. Returns null if there is no extension or if there are less than 4 characters in the string.

IMPORTANT: filename should consist of at least four characters.

Parameters:
filename - The name of the file from which the extension is to be returned.
Returns:
The extension of the file (including the period).

replaceExtension

public static java.lang.String replaceExtension(java.lang.String filename,
                                                java.lang.String new_extension)
Returns a copy of the given file name with its original extension stripped away and replaced by the new specified extension.

Parameters:
filename - The name of the file that is to have its extension replaced.
new_extension - The replacement extension, NOT including the period.
Returns:
The file name with the new extension.

getRoundedDouble

public static java.lang.String getRoundedDouble(double number_to_round,
                                                int decimal_places)
Returns a formatted version of number_to_round. It will always show a 0 before numbers less than one, and will only show up to decimal_places decimal places.

Values of not a number, negative infinity and positive infinity will be returned as NaN, -Infinity and Infinity respectively.

Parameters:
number_to_round - The number that is to be rounded.
decimal_places - The maximum number of decimal places that will be displayed.
Returns:
The formatted number.

getRoundedDoubleWithCommas

public static java.lang.String getRoundedDoubleWithCommas(double number_to_round,
                                                          int decimal_places)
Returns a formatted version of number_to_round. It will only show up to decimal_places decimal places. Every three places to the left of the decimal will be separated by a comma.

Values of not a number, negative infinity and positive infinity will be returned as NaN, -Infinity and Infinity respectively.

Parameters:
number_to_round - The number that is to be rounded.
decimal_places - The maximum number of decimal places that will be displayed.
Returns:
The formatted number.

getDoubleInScientificNotation

public static java.lang.String getDoubleInScientificNotation(double number_to_round,
                                                             int significant_digits)
Returns a formatted version of number_to_round that has been converted to scientific notation and includes the given number of significant_digits.

Values of not a number, negative infinity and positive infinity will be returned as NaN, -Infinity and Infinity respectively.

Parameters:
number_to_round - The number that is to be formatted.
significant_digits - The number of significant digits to use.
Returns:
The formatted string.

getNumberFormattedWithCommas

public static java.lang.String getNumberFormattedWithCommas(int number)
Formats the given number with a comma every three digits.

Parameters:
number - The number to format.
Returns:
The formatted number.

getNumberFormattedWithCommas

public static java.lang.String getNumberFormattedWithCommas(long number)
Formats the given number with a comma every three digits.

Parameters:
number - The number to format.
Returns:
The formatted number.

testIfStringIsAnInt

public static boolean testIfStringIsAnInt(java.lang.String test_string)
Tests if the given string is an integer (e.g. "42" but not "I am 42.").

Parameters:
test_string - The string to test.
Returns:
Whether or not the string is an integer.

getIntInLimits

public static int getIntInLimits(java.lang.String test_string,
                                 int min,
                                 int max)
                          throws java.lang.Exception
Tests if the given string is an integer within the given bounds, and returns it as an integer if it is. If it is not, then throws an exception indicating the problem.

Parameters:
test_string - The string to convert to an integer.
min - The minimum acceptable integer value.
max - The maximum acceptable integer value.
Returns:
The value of test_strig as an int.
Throws:
java.lang.Exception - An informative exception is thrown if test_string is not an integer or does not fall between min and max.