com.pelzer.util
Class StringMan

java.lang.Object
  extended by com.pelzer.util.StringMan

public class StringMan
extends Object

Utility class for string manipulation.


Method Summary
static boolean areStringsEqual(String s1, String s2)
          null-safe string equality test.
static String chop(String source, int length)
          Chops a string off after a certain number of characters.
static String compressHexString(String inString)
          Very simplistic compression method that encodes repeating characters into a smaller character, so '0000000000' becomes '^0j' where the '^' escapes the next two characters, '0' is the repeating character, and j encodes 10 repetitions.
static String concat(String[] array, String separator, int fromIndex, int toIndex)
          Concats elements in array between fromIndex, inclusive, to toIndex, inclusive, inserting separator between them.
static byte[] decodeStringToBytes(String hex)
          Takes a string encoded in hex (00-FF), two chars per byte, and returns an array of bytes.
static String decompressHexString(String in)
          Decompresses a string encoded by compressHexString(java.lang.String)
static String detokenize(String[] values, String delimiter)
          Converts a string array into a single string with values delimited by the specified delimiter.
static String encodeBytesToString(byte[] bytes)
          Takes a byte array and returns a hex string (00-FF) encoded two chars per byte.
static String escapeForXML(String inString)
          Makes a string xml-safe, converting &<>"' to their &token; equivalents, and converting all other non-safe chars to their � version.
static String getFirstLine(String s)
           
static String getNullAsEmptyString(String input)
          Returns an empty string if input is null, otherwise returns the original input string untouched.
static String getNullAsValue(String input, String value)
          Returns the specified string value if input is null, otherwise returns the original input string untouched.
static String getStackTrace(Throwable ex)
           
static String getUS7ASCIIEquiv(String convertString)
          Converts a string with possible high-ascii values into their ascii7 equiv., for instance 'é' maps to 'e'.
static boolean isEmpty(String s)
           
static boolean isStringTrue(String in)
          Returns true if the string is 'TRUE' or 'YES' or '1', case insensitive.
static String padLeftJustified(String string, int length)
          "foo" becomes "foo "
static String padLeftJustified(String string, int length, String padString)
           
static String padRightJustified(String string, int length)
          "foo" becomes " foo"
static String padRightJustified(String string, int length, String padString)
           
static int[] parseInts(String[] array)
          Takes an array of Strings with integer values and returns an array of ints, or throws a NumberFormatException if there's a problem.
static long[] parseLongs(String[] array)
          Takes an array of Strings with long values and returns an array of longs, or throws a NumberFormatException if there's a problem.
static String replace(String haystack, String needle, String... replacements)
          Same behavior as replace(String, String, String) but cycles through the replacements, so replace("xxx","x","1","2","3") would return "123".
static String replace(String haystack, String needle, String replacement)
          Replaces all occurences of a String with another string, using the fastest method we could devise.
static String replaceFirst(String source, String find, String replace)
          Replaces first occurence of a String with another string
static String stripNonAlphaNumericCharacters(String input)
          Strips all non A-Z/0-9 characters from the input string.
static String stripNonEmailCharacters(String input)
          Strips everything except A-Z a-z 0-9 @ .
static String stripWhitespace(String input)
          Strips all characters c satisfying Character.isWhitespace(c).
static String toInitialCaps(String inString)
          Takes the inString and capitalizes every leter that occurs after a non-alpha character.
"I like pizza" becomes "I Like Pizza"
"DON'T EAT THAT" becomes "Don'T Eat That"
static String[] tokenize(String s)
          Splits string into an array using StringTokenizer's default delimeter set which is * " \t\n\r\f": the space character, the tab character, the newline character, the carriage-return character, and the form-feed character.
static String[] tokenize(String s, String delimiter)
          Splits string into an array using the given delimiter
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getNullAsEmptyString

public static String getNullAsEmptyString(String input)
Returns an empty string if input is null, otherwise returns the original input string untouched.

Parameters:
input - String to be evaluated

getNullAsValue

public static String getNullAsValue(String input,
                                    String value)
Returns the specified string value if input is null, otherwise returns the original input string untouched.

Parameters:
input - String to be evaluated
value - String to substitute if input is null

replaceFirst

public static String replaceFirst(String source,
                                  String find,
                                  String replace)
Replaces first occurence of a String with another string


isEmpty

public static boolean isEmpty(String s)
Returns:
true if s is null or all whitespace.

getFirstLine

public static String getFirstLine(String s)
Returns:
a substring up to the first occurence of '\n'. If '\n' is not found, returns the original string. If s is null, throws a NullPointerException

getStackTrace

public static String getStackTrace(Throwable ex)
Returns:
what you'd see in the console by doing ex.printStackTrace()

isStringTrue

public static boolean isStringTrue(String in)
Returns true if the string is 'TRUE' or 'YES' or '1', case insensitive. False for null, empty, etc.


chop

public static String chop(String source,
                          int length)
Chops a string off after a certain number of characters. If the string is shorter than the length, then the string is returned untouched. nulls are returned as null.

Parameters:
source -
length -

replace

public static String replace(String haystack,
                             String needle,
                             String replacement)
Replaces all occurences of a String with another string, using the fastest method we could devise. Runs about twice as fast as the java.util.regex replace method.


replace

public static String replace(String haystack,
                             String needle,
                             String... replacements)
Same behavior as replace(String, String, String) but cycles through the replacements, so replace("xxx","x","1","2","3") would return "123". If you don't give enough replacements, needles will be left in the resulting string. If you don't have enough instances of needle to fit all replacements, no foul, you just won't see those replacements.


escapeForXML

public static String escapeForXML(String inString)
Makes a string xml-safe, converting &<>"' to their &token; equivalents, and converting all other non-safe chars to their � version.


toInitialCaps

public static String toInitialCaps(String inString)
Takes the inString and capitalizes every leter that occurs after a non-alpha character.
"I like pizza" becomes "I Like Pizza"
"DON'T EAT THAT" becomes "Don'T Eat That"


padLeftJustified

public static String padLeftJustified(String string,
                                      int length)
"foo" becomes "foo "


padLeftJustified

public static String padLeftJustified(String string,
                                      int length,
                                      String padString)
See Also:
padLeftJustified(String, int)

padRightJustified

public static String padRightJustified(String string,
                                       int length)
"foo" becomes " foo"


padRightJustified

public static String padRightJustified(String string,
                                       int length,
                                       String padString)

decodeStringToBytes

public static byte[] decodeStringToBytes(String hex)
Takes a string encoded in hex (00-FF), two chars per byte, and returns an array of bytes. array.length will be hex.length/2 after hex has been run through decompressHexString(java.lang.String)


stripWhitespace

public static String stripWhitespace(String input)
Strips all characters c satisfying Character.isWhitespace(c).


stripNonAlphaNumericCharacters

public static String stripNonAlphaNumericCharacters(String input)
Strips all non A-Z/0-9 characters from the input string. Spaces are left intact.


stripNonEmailCharacters

public static String stripNonEmailCharacters(String input)
Strips everything except A-Z a-z 0-9 @ . characters from the input string. Spaces are left intact.


encodeBytesToString

public static String encodeBytesToString(byte[] bytes)
Takes a byte array and returns a hex string (00-FF) encoded two chars per byte.


compressHexString

public static String compressHexString(String inString)
Very simplistic compression method that encodes repeating characters into a smaller character, so '0000000000' becomes '^0j' where the '^' escapes the next two characters, '0' is the repeating character, and j encodes 10 repetitions.


decompressHexString

public static String decompressHexString(String in)
Decompresses a string encoded by compressHexString(java.lang.String)


getUS7ASCIIEquiv

public static String getUS7ASCIIEquiv(String convertString)
Converts a string with possible high-ascii values into their ascii7 equiv., for instance 'é' maps to 'e'. All low ascii characters are left untouched. Any character above 255 is converted to a space.


concat

public static String concat(String[] array,
                            String separator,
                            int fromIndex,
                            int toIndex)
Concats elements in array between fromIndex, inclusive, to toIndex, inclusive, inserting separator between them.


tokenize

public static String[] tokenize(String s)
Splits string into an array using StringTokenizer's default delimeter set which is * " \t\n\r\f": the space character, the tab character, the newline character, the carriage-return character, and the form-feed character.

See Also:
StringTokenizer

tokenize

public static String[] tokenize(String s,
                                String delimiter)
Splits string into an array using the given delimiter

See Also:
StringTokenizer

detokenize

public static String detokenize(String[] values,
                                String delimiter)
Converts a string array into a single string with values delimited by the specified delimiter.


areStringsEqual

public static boolean areStringsEqual(String s1,
                                      String s2)
null-safe string equality test. If both s1 and s2 are null, returns true.


parseLongs

public static long[] parseLongs(String[] array)
Takes an array of Strings with long values and returns an array of longs, or throws a NumberFormatException if there's a problem.


parseInts

public static int[] parseInts(String[] array)
Takes an array of Strings with integer values and returns an array of ints, or throws a NumberFormatException if there's a problem.



Copyright © 2012. All Rights Reserved.