Project Wonder 2.0

er.extensions
Class ERXStringUtilities

java.lang.Object
  extended byer.extensions.ERXStringUtilities

public class ERXStringUtilities
extends Object

Collection of String utilities. Contains the base localization support.


Field Summary
protected static double adjustement
          holds the base adjustment for fuzzy matching
static char[] HEX_CHARS
          Holds the chars for hex enconding
static NSArray SORT_ASCENDING
          Holds the ascending EOSortOrderings
static NSArray SORT_DESCENDING
          Holds the ascending EOSortOrderings
 
Constructor Summary
ERXStringUtilities()
           
 
Method Summary
static void appendSeparatorIfLastNot(char separator, char not, StringBuffer sb)
          Utility method to append a character to a StringBuffer if the last character is not a certain character.
static String byteArrayToHexString(byte[] block)
           
static String capitalize(String value)
          Capitalizes a given string.
static String capitalizeAllWords(String value)
          Capitalizes all the strings in a given string.
static String displayNameForKey(String key)
          Calculates a default display name for a given key path.
static double distance(String a, String b)
          Java port of the distance algorithm.
static String emptyStringForNull(String s)
          Simple utility method that will return the string "" if the string passed in is null otherwise it will return the passed in string.
static String escapeNonBasicLatinChars(char c)
           
static String escapeNonBasicLatinChars(String str)
           
static String escapeNonXMLChars(String str)
           
static String escapeSpace(String aString)
          Removes the spaces in a given String
static String firstPropertyKeyInKeyPath(String keyPath)
           
static NSArray fuzzyMatch(String name, String entityName, String propertyKey, String synonymsKey, EOEditingContext ec, ERXFuzzyMatchCleaner cleaner, NSArray sortOrderings)
          Fuzzy matching is useful for catching user entered typos.
static NSArray fuzzyMatch(String name, String entityName, String propertyKey, String synonymsKey, EOEditingContext ec, ERXFuzzyMatchCleaner cleaner, String comparisonString)
          Deprecated. use fuzzyMatch(String name, String entityName, String propertyKey, String synonymsKey, EOEditingContext ec, ERXFuzzyMatchCleaner cleaner, NSArray sortOrderings ) instead
static int indexOfNumericInString(String str)
          Locate the the first numeric character in the given string.
static int indexOfNumericInString(String str, int fromIndex)
          Locate the the first numeric character after fromIndex in the given string.
static Integer integerWithString(String s)
          Calculates an Integer for a given string.
static String keyPathWithoutFirstProperty(String keyPath)
           
static String keyPathWithoutLastProperty(String keyPath)
           
static String lastPropertyKeyInKeyPath(String keyPath)
           
static String localizedStringForKey(String key)
          Gets a localized string for a given key in the application's Localizable strings file for the default language (English).
static String localizedStringForKey(String key, String framework)
          Gets a localized string for a given key in a given framework's Localizable strings file for the default language (English).
static String localizedStringForKey(String key, String framework, NSArray languages)
          Gets a localized string for a given key in a given framework's Localizable strings file using the array of languages as the search order for the key.
static String localizedTemplateStringWithObjectForKey(Object o, String key, String framework, NSArray languages)
          Uses the method localizedStringForKey to retreive a template that is then parsed using the passed in object to produce a resulting string.
static String nullForEmptyString(String s)
          Simple utility method that will return null if the string passed in is equal to "" otherwise it will return the passed in string.
static int numberOfOccurrencesOfCharInString(char c, String s)
          Counts the number of occurrences of a particular char in a given string.
static String removeExtraDotsFromVersionString(String version)
          Cleans up the given version string by removing extra dots(.), for example, 5.1.3 becomes 5.13, so that the string can be converted to a double or BigDecimal type easily.
static String replaceStringByStringInString(String old, String newString, String buffer)
          Replaces a given string by another string in a string.
static void setAdjustement(double newAdjustement)
          Sets the base adjustment used for fuzzy matching
static boolean stringEqualsString(String s1, String s2)
           
static String stringFromResource(String name, String extension, NSBundle bundle)
          Retrives a given string for a given name, extension and bundle.
static boolean stringIsNullOrEmpty(String s)
          Simple test if the string is either null or equal to "".
static String stringWithContentsOfFile(String path)
          Reads the contents of a file given by a path into a string.
static String stringWithNtimesString(int n, String s)
          String multiplication.
static String toHexString(char c)
           
static String toHexString(String str)
           
static String toLowerCase(String str)
          This method runs about 20 times faster than java.lang.String.toLowerCase (and doesn't waste any storage when the result is equal to the input).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

HEX_CHARS

public static final char[] HEX_CHARS
Holds the chars for hex enconding


SORT_ASCENDING

public static final NSArray SORT_ASCENDING
Holds the ascending EOSortOrderings


SORT_DESCENDING

public static final NSArray SORT_DESCENDING
Holds the ascending EOSortOrderings


adjustement

protected static double adjustement
holds the base adjustment for fuzzy matching

Constructor Detail

ERXStringUtilities

public ERXStringUtilities()
Method Detail

distance

public static double distance(String a,
                              String b)
Java port of the distance algorithm. The code below comes from the following post on http://mail.python.org Fuzzy string matching Magnus L. Hetland mlh@idt.ntnu.no 27 Aug 1999 15:51:03 +0200 Explanation of the distance algorithm... The algorithm: def distance(a,b): c = {} n = len(a); m = len(b) for i in range(0,n+1): c[i,0] = i for j in range(0,m+1): c[0,j] = j for i in range(1,n+1): for j in range(1,m+1): x = c[i-1,j]+1 y = c[i,j-1]+1 if a[i-1] == b[j-1]: z = c[i-1,j-1] else: z = c[i-1,j-1]+1 c[i,j] = min(x,y,z) return c[n,m] It calculates the following: Given two strings, a and b, and three operations, adding, subtracting and exchanging single characters, what is the minimal number of steps needed to translate a into b? The method is based on the following idea: We want to find the distance between a[:x] and b[:y]. To do this, we first calculate 1) the distance between a[:x-1] and b[:y], adding the cost of a subtract-operation, used to get from a[:x] to a[:z-1]; 2) the distance between a[:x] and b[:y-1], adding the cost of an addition-operation, used to get from b[:y-1] to b[:y]; 3) the distance between a[:x-1] and b[:y-1], adding the cost of a *possible* exchange of the letter b[y] (with a[x]). The cost of the subtraction and addition operations are 1, while the exchange operation has a cost of 1 if a[x] and b[y] are different, and 0 otherwise. After calculating these costs, we choose the least one of them (since we want to use the best solution.) Instead of doing this recursively, i.e. calculating ourselves "back" from the final value, we build a cost-matrix c containing the optimal costs, so we can reuse them when calculating the later values. The costs c[i,0] (from string of length n to empty string) are all i, and correspondingly all c[0,j] (from empty string to string of length j) are j. Finally, the cost of translating between the full strings a and b (c[n,m]) is returned. I guess that ought to cover it... --------------------------

Parameters:
a - first string
b - second string
Returns:
the distance between the two strings

setAdjustement

public static void setAdjustement(double newAdjustement)
Sets the base adjustment used for fuzzy matching


fuzzyMatch

public static NSArray fuzzyMatch(String name,
                                 String entityName,
                                 String propertyKey,
                                 String synonymsKey,
                                 EOEditingContext ec,
                                 ERXFuzzyMatchCleaner cleaner,
                                 NSArray sortOrderings)
Fuzzy matching is useful for catching user entered typos. For example if a user is search for a company named 'Aple' within your application they aren't going to find it. Thus the idea of fuzzy matching, meaning you can define a threshold of 'how close can they be' type of thing.

Parameters:
name - to be matched against
entityName - name of the entity to perform the match against.
synonymsKey - allows objects to have additional values to be matched against in addition to just the value of the propertyKey
ec - context to fetch data in
cleaner - object used to clean a string, for example the cleaner might strip out the words 'The' and 'Inc.'
Returns:
an array of objects that match in a fuzzy manner the name passed in.

fuzzyMatch

public static NSArray fuzzyMatch(String name,
                                 String entityName,
                                 String propertyKey,
                                 String synonymsKey,
                                 EOEditingContext ec,
                                 ERXFuzzyMatchCleaner cleaner,
                                 String comparisonString)
Deprecated. use fuzzyMatch(String name, String entityName, String propertyKey, String synonymsKey, EOEditingContext ec, ERXFuzzyMatchCleaner cleaner, NSArray sortOrderings ) instead


localizedStringForKey

public static String localizedStringForKey(String key)
Gets a localized string for a given key in the application's Localizable strings file for the default language (English).

Parameters:
key - to be lookup in the strings file
Returns:
string value of the looked up key

localizedStringForKey

public static String localizedStringForKey(String key,
                                           String framework)
Gets a localized string for a given key in a given framework's Localizable strings file for the default language (English).

Parameters:
key - to be lookup in the strings file
framework - name, specify app or null to perform the lookup in the application's resources.
Returns:
string value of the looked up key

localizedStringForKey

public static String localizedStringForKey(String key,
                                           String framework,
                                           NSArray languages)
Gets a localized string for a given key in a given framework's Localizable strings file using the array of languages as the search order for the key.

Parameters:
key - to be lookup in the strings file
framework - name, specify app or null to perform the lookup in the application's resources.
languages - array to search for the key in
Returns:
string value of the looked up key

localizedTemplateStringWithObjectForKey

public static String localizedTemplateStringWithObjectForKey(Object o,
                                                             String key,
                                                             String framework,
                                                             NSArray languages)
Uses the method localizedStringForKey to retreive a template that is then parsed using the passed in object to produce a resulting string. The template parser used is ERXSimpleTemplateParser.

Parameters:
o - object used to resolve keys in the localized template
key - to be lookup in the strings file
framework - name, specify app or null to perform the lookup in the application's resources.
languages - array to search for the key in
Returns:
localized template parsed and resolved with the given object.

stringWithContentsOfFile

public static String stringWithContentsOfFile(String path)
Reads the contents of a file given by a path into a string.

Parameters:
path - to the file in the file system
Returns:
the contents of the file in a string

integerWithString

public static Integer integerWithString(String s)
Calculates an Integer for a given string. The only advantage that this method has is to not throw a number format exception if the string is not correctly formatted.

Parameters:
s - string to caclulate an Integer from
Returns:
parsed Integer from the string or null if the string is not correctly formed.

stringFromResource

public static String stringFromResource(String name,
                                        String extension,
                                        NSBundle bundle)
Retrives a given string for a given name, extension and bundle.

Parameters:
name - of the resource
extension - of the resource, example: txt or rtf
bundle - to look for the resource in
Returns:
string of the given file specified in the bundle

firstPropertyKeyInKeyPath

public static final String firstPropertyKeyInKeyPath(String keyPath)

lastPropertyKeyInKeyPath

public static final String lastPropertyKeyInKeyPath(String keyPath)

keyPathWithoutLastProperty

public static final String keyPathWithoutLastProperty(String keyPath)

keyPathWithoutFirstProperty

public static final String keyPathWithoutFirstProperty(String keyPath)

displayNameForKey

public static String displayNameForKey(String key)
Calculates a default display name for a given key path. For instance for the key path: "foo.bar" the display name would be "Bar".

Parameters:
key - to calculate the display name
Returns:
display name for the given key

indexOfNumericInString

public static int indexOfNumericInString(String str)
Locate the the first numeric character in the given string.

Returns:
position in int. -1 for not found.

indexOfNumericInString

public static int indexOfNumericInString(String str,
                                         int fromIndex)
Locate the the first numeric character after fromIndex in the given string.

Returns:
position in int. -1 for not found.

appendSeparatorIfLastNot

public static void appendSeparatorIfLastNot(char separator,
                                            char not,
                                            StringBuffer sb)
Utility method to append a character to a StringBuffer if the last character is not a certain character. Useful for determining if you need to add an '&' to the end of a form value string.

Parameters:
separator - character to add to potentially add to the StringBuffer.
not - character to test if the given StringBuffer ends in it.
sb - StringBuffer to test and potentially append to.

replaceStringByStringInString

public static String replaceStringByStringInString(String old,
                                                   String newString,
                                                   String buffer)
Replaces a given string by another string in a string.

Parameters:
old - string to be replaced
newString - to be inserted
buffer - string to have the replacement done on it
Returns:
string after having all of the replacement done.

escapeSpace

public static String escapeSpace(String aString)
Removes the spaces in a given String

Returns:
string removing all spaces in it.

toLowerCase

public static String toLowerCase(String str)
This method runs about 20 times faster than java.lang.String.toLowerCase (and doesn't waste any storage when the result is equal to the input). Warning: Don't use this method when your default locale is Turkey. java.lang.String.toLowerCase is slow because (a) it uses a StringBuffer (which has synchronized methods), (b) it initializes the StringBuffer to the default size, and (c) it gets the default locale every time to test for name equal to "tr".

See Also:
tolower

stringWithNtimesString

public static String stringWithNtimesString(int n,
                                            String s)
String multiplication.

Parameters:
n - the number of times to concatinate a given string
s - string to be multipled
Returns:
multiplied string

numberOfOccurrencesOfCharInString

public static int numberOfOccurrencesOfCharInString(char c,
                                                    String s)
Counts the number of occurrences of a particular char in a given string.

Parameters:
c - char to count in string
s - string to look for specified char in.
Returns:
number of occurences of a char in the string

stringIsNullOrEmpty

public static boolean stringIsNullOrEmpty(String s)
Simple test if the string is either null or equal to "".

Parameters:
s - string to test
Returns:
result of the above test

nullForEmptyString

public static String nullForEmptyString(String s)
Simple utility method that will return null if the string passed in is equal to "" otherwise it will return the passed in string.

Parameters:
s - string to test
Returns:
null if the string is "" else the string.

emptyStringForNull

public static String emptyStringForNull(String s)
Simple utility method that will return the string "" if the string passed in is null otherwise it will return the passed in string.

Parameters:
s - string to test
Returns:
"" if the string is null else the string

escapeNonXMLChars

public static String escapeNonXMLChars(String str)

escapeNonBasicLatinChars

public static String escapeNonBasicLatinChars(char c)

escapeNonBasicLatinChars

public static String escapeNonBasicLatinChars(String str)

toHexString

public static String toHexString(char c)

toHexString

public static String toHexString(String str)

byteArrayToHexString

public static String byteArrayToHexString(byte[] block)

removeExtraDotsFromVersionString

public static String removeExtraDotsFromVersionString(String version)
Cleans up the given version string by removing extra dots(.), for example, 5.1.3 becomes 5.13, so that the string can be converted to a double or BigDecimal type easily.

Parameters:
version - string
Returns:
cleaned-up string that only contains the first dot(.) as the floating point indicator.

capitalize

public static String capitalize(String value)
Capitalizes a given string.

Parameters:
value - to be capitalized
Returns:
capitalized string

capitalizeAllWords

public static String capitalizeAllWords(String value)
Capitalizes all the strings in a given string.

Parameters:
value - to be capitalized
Returns:
capitalized string

stringEqualsString

public static boolean stringEqualsString(String s1,
                                         String s2)

Last updated: Do, Dez 9, 2004 • 12:46 PM CET

Copyright © 2002 – 2004 Project Wonder.