| java.lang.Object org.isqlviewer.util.StringUtilities
StringUtilities | final public class StringUtilities (Code) | | Generic String manipulation library for converting strings or working with text.
author: Mark A. Kobold <mkobold at isqlviewer dot com> version: 1.0 |
Method Summary | |
public static int | charCount(String str, char c, boolean countEscaped) Helper method to count number of given characters in a string.
This will return number of character instances in the given string. | public static String[] | collapseToArray(String list, String delimiter) Collapses a comma delimted text to an array of strings.
This method is the opposite of the explode methods in this class.
See Also: StringUtilities.explode(Collection) Parameters: list - comma seperated list of values. Parameters: delimiter - to used to distinguish the elements in the string list, for example ','. | public static Collection<String> | collapseToCollection(String list, String delimiter) Collapses a comma delimted text to a collection of strings.
This method is the opposite of the explode methods in this class.
See Also: StringUtilities.explode(Collection) Parameters: list - comma seperated list of values. Parameters: delimiter - to used to distinguish the elements in the string list, for example ','. | public static boolean | compare(String s1, String s2) Compares to strings together to see if they are equal.
What seperates this method from a standard equals is that both strings can be null and they are still compared
safely.
Parameters: s1 - first string to compare. Parameters: s2 - second string to compare. | public static String | decodeASCII(String str) Method for decoding ASCII escaped character sequences for UNICODE characters.
This will decode text encoded in a fashion similar to the Java Properties encoding mechanisim. | public static String | decodeBase64(String text) Standard base64 decoding mechanisim.
This uses the internal sun base64 decoder.
Parameters: text - to decode using base64. | public static String | decryptText(String text, Key key, String transformation) Decrypts simple text using a key and specificed transformation.
This will get an instance of the
Cipher object using the specified transformation. | public static String | encodeASCII(String theString) Encodes method into an ASCI compatible form. | public static String | encodeBase64(String text) Standard base64 encoding mechanisim.
This uses the internal sun base64 encoder.
Parameters: text - to encode using base64. | public static String | encodeHTMLEntities(String theString) Encodes string safe for HTML. | public static String | encodeMD5(String text) Encode text using an MD5 algorithim.
Parameters: text - to encode. | public static String | encodeMD5Data(byte[] binaryData) Encodes MD5 Hashes into a conformed statisically unique identifier.
Parameters: binaryData - MD5 hash from the message digest. | public static String | encodeXMLEntities(String str) Encodes XML reserved characters to their respective entities.
This will encode/change the following raw characters:
- " = "
- > = >
- < = <
- & = &
- ' = '
Parameters: str - to be encoded. | public static String | encryptText(String text, Key key, String transformation) Encrypts simple text using a key and specified transformation.
This will get an instance of the
Cipher object using the specified transformation. | public static String | explode(Object[] list) Creates a comma seperated list of strings from a collection.
Parameters: list - of strings or objects to created csv string from. | public static String | explode(Collection list) Creates a comma seperated list of strings from a collection.
For every object in the list, this method relys on the toString() of Object to apped to, so it is allowable to
send a collection containing non-strings.
Parameters: list - of strings or objects to created csv string from. | public static String | explode(Collection list, String delimiter) Creates a comma seperated list of strings from a collection.
For every object in the list, this method relys on the toString() of Object to apped to, so it is allowable to
send a collection containing non-strings.
Parameters: list - of strings or objects to created csv string from. Parameters: delimiter - delimiter to explode the list by, if null a comma (,) is used. | public static String | forceTrim(String text) Forces the single character to be stripped from both ends of the string.
This method is mainly useful for stripping off enclosing quotation marks and the like. | public static String | formatBreak(int length, String text, String newLine, boolean htmlBreak) Utility method for wrapping text to a specific width.
This method allows you to pragmatically wrap long sections of text to a specified length. | public static String | getFullHumanReadableTime(long milliseconds) Converts a long number representing milliseconds into human readable format.
This value is mainly for formating milli-seconds into something a bit more readable much like the
getHumanReadableTime() method.
Instead of seeing large incoherent number of milli-seconds you can see something like '1.016m'= 1 minute 1 second
or '1.05h' = 1 hour 3 minutes.
Parameters: milliseconds - to format into a string. | public static String | getHTMLColor(Color c) Creates HTML friendly format for Java Color.
creates a string format of a Color object by taking the RGB color values and converting them to the hexadecimal
equivalent, for example Color.BLACK will be '#000000'
To get the color back from the value returned here, use the decode method on Color.
See Also: Color.decode(java.lang.String) Parameters: c - to encode into HTML friendly format. | public static String | getHumanReadableSize(long bytes) Converts a long number representing bytes into human readable format.
This value is mainly for formating values like a file size, memory size and so forth, so instead of seeing a
large incoherent number you can see something like '308.123KB' or '9.68MB'
Parameters: bytes - to format into a string. | public static String | getHumanReadableTime(long milliseconds) Converts a long number representing milliseconds into human readable format.
This value is mainly for formating milli-seconds into something a bit more readable much like the
getHumanReadableTime() method.
Instead of seeing large incoherent number of milli-seconds you can see something like '1.016m'= 1 minute 1 second
or '1.05h' = 1 hour 3 minutes.
Parameters: milliseconds - to format into a string. | public static String | getNormalizedClassName(String fqcn) Gets a normalized class name without a package namespace.
This will strip of the package name of a fully qualified class name that usually comes from
class.getName()
Parameters: fqcn - fully qualified class name to normalize. | public static String | getSystemProperty(String key, String def) Utility method for accessing a system property without a security permission.
This method will simply eat any security exception that could occur and return the default.
Parameters: key - of the system property to lookup. Parameters: def - the default value to use if the property cannot be read. | public static String | getTypeStringForObject(Object o) Gets an SQL Type field string based on the given object.
Basically this method gets the field name that can be easily reflected back into the java.sql.Types object to get
the integer SQL type that should be used.
See Also: Types See Also: StringUtilities.getTypeforValue(int) Parameters: o - to test. | public static String | getTypeforValue(int type) Gets the Field name corresponding to the given type value.
Simple reflection to find the field name that has the same value given. | public static String | leftFill(String original, int maxLength) Prepends whitespace to a string to a sepecified length.
if leftFill("Bork",8) = '\ \ \ \ Bork'
spaces are escaped for clarity.
Parameters: original - text to prepend data to. Parameters: maxLength - maximum size the length the text grows to. | public static Font | parseFontString(String s) Parses a font string to a Font object.
Complimentary method for re-creating the font objects formatted to a string by the getFontString(Font) of this
object.
for information on the format of the font string see getFontString(Font)
Parameters: s - to parse for the font. | public static String | removeSubString(String source, String sub) Removes a substring from a given string.
If source equals something like 'hello world' and the sub equals 'ell' the returned value will be 'ho world'.
Also it should be noted that this method is a case-insensitive operation, for example 'Hello World' with sub
equal to 'LO' would produce 'Hel World'.
Parameters: source - string to remove sub string from. Parameters: sub - to remove from source value. | public static String | rightFill(String original, int maxLength) Appends whitespace to a string to a sepecified length.
if leftFill("Bork",8) = 'Bork\ \ \ \ '
spaces are escaped for clarity.
Parameters: original - text to append data to. Parameters: maxLength - maximum size the length the text grows to. | public static int | searchStringSet(String s, String chars) Searches a string for a set of characters.
This will return the first occurrence of any character that exists in both s and chars. | public static String | stripCharacters(String s, String set) Method for removing a set of characters from a source string.
Strips characters from the given set string from the source string s. | public static String | substituteVariables(String source, Properties props) Performs variable string substitution with a set of named parameters.
In similar fashion the the JVM and Jakarta projects this will perform string substitution on a string based on a
set of properties using the names to create a dynamically formatted text, that is based on easy to create, as
well as read source string.
Using the source text of 'The quick brown ${fast animal} jumps over the lazy ${lazy animal}' you
would then set properties in the give props of 'fast animal' and 'lazy animal' with the respective values that
you want to be used as a replacement to the ${} variables. | public static Number | toNumber(String possibleNumber) Somewhat agressive number conversion method.
This will attempt to parse normal number, local currency, and or a percentage.
Parameters: possibleNumber - to change to a number. | public static String | trimToSize(String text, int maxLength) Forces a string to be trimmed to a specified length.
Makes sure that the text is <= maxLength. |
charCount | public static int charCount(String str, char c, boolean countEscaped)(Code) | | Helper method to count number of given characters in a string.
This will return number of character instances in the given string. if the given string is null then a -1 will
returned.
For example if the given string is "XXNNXXnxXnXN" and the char is 'n' it will return a value of two as this
method is case sensitive.
Another example is if given the string 'X\nXxxNnN' with count escaped set to false will return 1 otherwise it
will return 2.
Parameters: str - to check characters for. Parameters: c - character to count Parameters: countEscaped - true will included escaped characters as part of the count` number of occurrences of the character C in the given string. |
collapseToArray | public static String[] collapseToArray(String list, String delimiter)(Code) | | Collapses a comma delimted text to an array of strings.
This method is the opposite of the explode methods in this class.
See Also: StringUtilities.explode(Collection) Parameters: list - comma seperated list of values. Parameters: delimiter - to used to distinguish the elements in the string list, for example ','. collection of string from tokens in the list. |
collapseToCollection | public static Collection<String> collapseToCollection(String list, String delimiter)(Code) | | Collapses a comma delimted text to a collection of strings.
This method is the opposite of the explode methods in this class.
See Also: StringUtilities.explode(Collection) Parameters: list - comma seperated list of values. Parameters: delimiter - to used to distinguish the elements in the string list, for example ','. collection of string from tokens in the list. |
compare | public static boolean compare(String s1, String s2)(Code) | | Compares to strings together to see if they are equal.
What seperates this method from a standard equals is that both strings can be null and they are still compared
safely.
Parameters: s1 - first string to compare. Parameters: s2 - second string to compare. true if both of the given strings are equal. |
decodeASCII | public static String decodeASCII(String str)(Code) | | Method for decoding ASCII escaped character sequences for UNICODE characters.
This will decode text encoded in a fashion similar to the Java Properties encoding mechanisim. Such that \\uxxxx
are converted to the proper characters.
In fact most of this code came from that private method contained there.
See Also: StringUtilities.encodeASCII(String) Parameters: str - to decode ASCII escaped character sequences from. decoded string. |
decodeBase64 | public static String decodeBase64(String text)(Code) | | Standard base64 decoding mechanisim.
This uses the internal sun base64 decoder.
Parameters: text - to decode using base64. original text before base64 encoding. |
decryptText | public static String decryptText(String text, Key key, String transformation) throws GeneralSecurityException(Code) | | Decrypts simple text using a key and specificed transformation.
This will get an instance of the
Cipher object using the specified transformation. With the given Cipher
the bytes will be transformed into a new string through the Cipher.doFinal() method.
If null or a blank string is given for text, the value is immediately returned as is.
Parameters: text - that needs to be decrypted. Parameters: key - to unlock the text with. Parameters: transformation - the name of the transformation, e.g., DES/CBC/PKCS5Padding. decrypted string of the original text parameter. throws: GeneralSecurityException - if an error occurs during decryption. See Also: Cipher.getInstance(java.lang.String) |
encodeASCII | public static String encodeASCII(String theString)(Code) | | Encodes method into an ASCI compatible form.
This method has the result equivalent the native2ascii tool provided by the JDK.
The value returned here is similar to that found in the Java properties output.
Parameters: theString - to be encoded encoded string. |
encodeBase64 | public static String encodeBase64(String text)(Code) | | Standard base64 encoding mechanisim.
This uses the internal sun base64 encoder.
Parameters: text - to encode using base64. encoded text using base64. |
encodeHTMLEntities | public static String encodeHTMLEntities(String theString)(Code) | | Encodes string safe for HTML.
If encoding into HTML various entities will be encoded for proper HTML formatting.
Parameters: theString - to be encoded encoded string. |
encodeMD5 | public static String encodeMD5(String text)(Code) | | Encode text using an MD5 algorithim.
Parameters: text - to encode. MD5 checksum of the text given. |
encodeMD5Data | public static String encodeMD5Data(byte[] binaryData)(Code) | | Encodes MD5 Hashes into a conformed statisically unique identifier.
Parameters: binaryData - MD5 hash from the message digest. encoded hash value. See Also: java.security.MessageDigest.digest |
encodeXMLEntities | public static String encodeXMLEntities(String str)(Code) | | Encodes XML reserved characters to their respective entities.
This will encode/change the following raw characters:
- " = "
- > = >
- < = <
- & = &
- ' = '
Parameters: str - to be encoded. String with standard encoded entities. |
encryptText | public static String encryptText(String text, Key key, String transformation) throws GeneralSecurityException(Code) | | Encrypts simple text using a key and specified transformation.
This will get an instance of the
Cipher object using the specified transformation. With the given Cipher
the bytes will be transformed into a new string through the Cipher.doFinal() method.
If null or a blank string is given for text, the value is immediately returned as is.
Parameters: text - that needs to be encrypted. Parameters: key - to lock the text with. Parameters: transformation - the name of the transformation, e.g., DES/CBC/PKCS5Padding. encrypted string of the original text parameter. throws: GeneralSecurityException - if an error occurs during encryption. See Also: Cipher.getInstance(java.lang.String) |
explode | public static String explode(Object[] list)(Code) | | Creates a comma seperated list of strings from a collection.
Parameters: list - of strings or objects to created csv string from. a comma seperated list of values from the list. See Also: StringUtilities.explode(Collection) |
explode | public static String explode(Collection list)(Code) | | Creates a comma seperated list of strings from a collection.
For every object in the list, this method relys on the toString() of Object to apped to, so it is allowable to
send a collection containing non-strings.
Parameters: list - of strings or objects to created csv string from. a comma seperated list of values from the list. |
explode | public static String explode(Collection list, String delimiter)(Code) | | Creates a comma seperated list of strings from a collection.
For every object in the list, this method relys on the toString() of Object to apped to, so it is allowable to
send a collection containing non-strings.
Parameters: list - of strings or objects to created csv string from. Parameters: delimiter - delimiter to explode the list by, if null a comma (,) is used. a comma seperated list of values from the list. |
forceTrim | public static String forceTrim(String text)(Code) | | Forces the single character to be stripped from both ends of the string.
This method is mainly useful for stripping off enclosing quotation marks and the like. this method will removes
the first and last character of text, and return the result.
if the text length is less than 2 it will simply return a blank string.
Parameters: text - to strip first and last character from. stripped form of the text parameter. |
formatBreak | public static String formatBreak(int length, String text, String newLine, boolean htmlBreak)(Code) | | Utility method for wrapping text to a specific width.
This method allows you to pragmatically wrap long sections of text to a specified length. This can be help ful if
the string is too long to show on one line to the user.
This also allows for HTML style breaks or '\n' style line breaks.
Parameters: length - maximum line length to wrap lines to. Parameters: text - to to format and wrap. Parameters: newLine - this is text insert with each new line created i.e. '\t'. Parameters: htmlBreak - use HTML style <br/> or '\n' line breaks. formatted and wrapped text. |
getFullHumanReadableTime | public static String getFullHumanReadableTime(long milliseconds)(Code) | | Converts a long number representing milliseconds into human readable format.
This value is mainly for formating milli-seconds into something a bit more readable much like the
getHumanReadableTime() method.
Instead of seeing large incoherent number of milli-seconds you can see something like '1.016m'= 1 minute 1 second
or '1.05h' = 1 hour 3 minutes.
Parameters: milliseconds - to format into a string. human readable format for large milli-seconds intervals. |
getHTMLColor | public static String getHTMLColor(Color c)(Code) | | Creates HTML friendly format for Java Color.
creates a string format of a Color object by taking the RGB color values and converting them to the hexadecimal
equivalent, for example Color.BLACK will be '#000000'
To get the color back from the value returned here, use the decode method on Color.
See Also: Color.decode(java.lang.String) Parameters: c - to encode into HTML friendly format. Hexadecimal representation of the color. |
getHumanReadableSize | public static String getHumanReadableSize(long bytes)(Code) | | Converts a long number representing bytes into human readable format.
This value is mainly for formating values like a file size, memory size and so forth, so instead of seeing a
large incoherent number you can see something like '308.123KB' or '9.68MB'
Parameters: bytes - to format into a string. human readable format for larger byte counts. |
getHumanReadableTime | public static String getHumanReadableTime(long milliseconds)(Code) | | Converts a long number representing milliseconds into human readable format.
This value is mainly for formating milli-seconds into something a bit more readable much like the
getHumanReadableTime() method.
Instead of seeing large incoherent number of milli-seconds you can see something like '1.016m'= 1 minute 1 second
or '1.05h' = 1 hour 3 minutes.
Parameters: milliseconds - to format into a string. human readable format for large milli-seconds intervals. |
getNormalizedClassName | public static String getNormalizedClassName(String fqcn)(Code) | | Gets a normalized class name without a package namespace.
This will strip of the package name of a fully qualified class name that usually comes from
class.getName()
Parameters: fqcn - fully qualified class name to normalize. class name only. |
getSystemProperty | public static String getSystemProperty(String key, String def)(Code) | | Utility method for accessing a system property without a security permission.
This method will simply eat any security exception that could occur and return the default.
Parameters: key - of the system property to lookup. Parameters: def - the default value to use if the property cannot be read. the respective system property or the default if cannot be read. |
getTypeStringForObject | public static String getTypeStringForObject(Object o)(Code) | | Gets an SQL Type field string based on the given object.
Basically this method gets the field name that can be easily reflected back into the java.sql.Types object to get
the integer SQL type that should be used.
See Also: Types See Also: StringUtilities.getTypeforValue(int) Parameters: o - to test. String that is a field name in the Types object. |
getTypeforValue | public static String getTypeforValue(int type)(Code) | | Gets the Field name corresponding to the given type value.
Simple reflection to find the field name that has the same value given. If the value cannot be found the text
'OTHER' will be returned.
This method will also return null if an error occurs during reflection.
Parameters: type - that corresponds to an SQL type. field name based on the value type. See Also: Types |
leftFill | public static String leftFill(String original, int maxLength)(Code) | | Prepends whitespace to a string to a sepecified length.
if leftFill("Bork",8) = '\ \ \ \ Bork'
spaces are escaped for clarity.
Parameters: original - text to prepend data to. Parameters: maxLength - maximum size the length the text grows to. string left filled with white space to maxLength. See Also: StringUtilities.rightFill(String,int) |
parseFontString | public static Font parseFontString(String s)(Code) | | Parses a font string to a Font object.
Complimentary method for re-creating the font objects formatted to a string by the getFontString(Font) of this
object.
for information on the format of the font string see getFontString(Font)
Parameters: s - to parse for the font. new Font object represented by the given string. |
removeSubString | public static String removeSubString(String source, String sub)(Code) | | Removes a substring from a given string.
If source equals something like 'hello world' and the sub equals 'ell' the returned value will be 'ho world'.
Also it should be noted that this method is a case-insensitive operation, for example 'Hello World' with sub
equal to 'LO' would produce 'Hel World'.
Parameters: source - string to remove sub string from. Parameters: sub - to remove from source value. new string with sub removed from the source string. |
rightFill | public static String rightFill(String original, int maxLength)(Code) | | Appends whitespace to a string to a sepecified length.
if leftFill("Bork",8) = 'Bork\ \ \ \ '
spaces are escaped for clarity.
Parameters: original - text to append data to. Parameters: maxLength - maximum size the length the text grows to. string right filled with white space to maxLength. See Also: StringUtilities.leftFill(String,int) |
searchStringSet | public static int searchStringSet(String s, String chars)(Code) | | Searches a string for a set of characters.
This will return the first occurrence of any character that exists in both s and chars. The return values is the
index >=0 and <= s.length()-1;
If either s or chars is null or empty the method will immediately return -1.
Parameters: s - string to search Parameters: chars - set of character to look for. first index of any character in chars within s. |
stripCharacters | public static String stripCharacters(String s, String set)(Code) | | Method for removing a set of characters from a source string.
Strips characters from the given set string from the source string s. for example the String 'XXJavaXxRuntime'
with the set parameter being 'Xa' will results in the following string 'JvxRuntime'
This method is very useful for removing various different characters at once from a string.
Parameters: s - String to strip characters from. Parameters: set - collection of characters to strip. new string that is s that contains no characters from set. |
substituteVariables | public static String substituteVariables(String source, Properties props) throws ParseException(Code) | | Performs variable string substitution with a set of named parameters.
In similar fashion the the JVM and Jakarta projects this will perform string substitution on a string based on a
set of properties using the names to create a dynamically formatted text, that is based on easy to create, as
well as read source string.
Using the source text of 'The quick brown ${fast animal} jumps over the lazy ${lazy animal}' you
would then set properties in the give props of 'fast animal' and 'lazy animal' with the respective values that
you want to be used as a replacement to the ${} variables. such that is 'fast animal' = 'fox' and 'lazy animal' =
'dog' the result would be the famous sentence of 'The quick brown fox jumps over the lazy dog'.
This method does support recursive variables such that a variable can reference another variable until either
there is a stack overflow or there are no variables left to substitute.
Parameters: source - string to be formatted using variable substitution. Parameters: props - set of variable names to variable values to be used for the substitution process. a formatted string with the variables properly substituted. throws: ParseException - if a variable declaration is started but is not closed. |
toNumber | public static Number toNumber(String possibleNumber)(Code) | | Somewhat agressive number conversion method.
This will attempt to parse normal number, local currency, and or a percentage.
Parameters: possibleNumber - to change to a number. null if the string cannot be interpreted as a number. |
trimToSize | public static String trimToSize(String text, int maxLength)(Code) | | Forces a string to be trimmed to a specified length.
Makes sure that the text is <= maxLength. If text is null or blank the original text will be returned.
A trim() will occur before a substring is created, the substring is create iff the trimed version greater than
the maxLength.
Parameters: text - to strip. Parameters: maxLength - maximum size to trim to. the text parameter that does not exceed maxLength. |
|
|