Class StringUtil

java.lang.Object
org.cdlib.mrt.utility.StringUtil

public class StringUtil extends Object
This class is a collection of String Utility functions not found in the String class. They are all static and so can be called with instanciating this class.
Author:
Michael Thwaites
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    Test if passed value is a "true" value Used to validate that an argument means "true" or "yes"
    static String
    byteArrToString(byte[] byteArr)
    Converts an array of bytes to a string guaranteeing utf-8 mapping if possible
    Note works only with utf-8 and a byte-char set (e.g.
    static final String
    compress multiple occurances of blank to one occurance
    static final String
    compress multiple occurances of cval to one occurance
    static int
    count(String source, String match)
    Count the number of times match occurs in source
    static String
    replace all occurances of single quote (') with two single quotes.
    static String
    getUTF8(byte[] bytes)
     
    static final boolean
    Return true if only space characters are found or empty
    static final boolean
    isEmpty(byte[] b)
    test if the byte [] passed is null or empty
    static final boolean
    test if the string passed is null or empty
    static final boolean
    test if the StringBuffer passed is null or empty
    static final boolean
    isNotEmpty(byte[] b)
    test if the byte [] passed is not null and not empty
    static final boolean
    test if the string passed is not null and not empty
    static final boolean
    test if the StringBuffer passed is not null and not empty
    static final boolean
    isNumeric (String) tests if all the characters of a string are digits
    static String
    leftPad(String in, int size, char padc)
     
    static String
    lowerCaseFirst (String) returns the passed string with first char lowercased , e.g.
    static boolean
    matchRegex(String inString, String regex)
    MatchRegex (String) returns boolean value for whether string matches regex Added by SR For info on Java regex, which is quite similar to Perl regex, see http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html Examples: -- inString contains "a" or "b": regex = "a|b" -- inString contains "a" zero or more times: regex = "a*" -- inString contains "a" one or more times: regex = "a+"
    static String
    A common normalization for parms: if null or has length zero then set null if all space characters set null trim space characters
    static String
    removeControl - replace ASCII control characters with blank
    static final String
    squeeze the blanks from a string
    static final String
    squeeze(String s, String toRemove)
    squeeze a set of characters from a string
    static String
     
    static String
    stackTrace returns the stack trace for the exception passed.
    static byte[]
    convert ioStream to String
    static String
    streamToString(InputStream ioStream, String encoding)
    convert ioStream to String
    stringToStream(String inString, String encoding)
    convert String to InputStream
    static String
    strip(String inString, String stripString)
     
    static String
    stripNonAlphabetic remove all non-alphabetics
    static String
    toHex(byte[] inBytes)
    toHex (byte []) returns the passed byte array in hexadecimal
    static String
    toHex(byte[] inBytes, int maxbytes)
    toHex (byte []) returns the passed byte array in hexadecimal
    static String
    toHex(String inString)
    toHex (String) returns the passed string in hexadecimal
    static String
    upperCaseFirst (String) returns the passed string with first char uppercased , e.g.
    static String
    xchange(String in, String from, String[] to)
    xchange replaces all occurances of a String of characters for the corresponding string from an array of String.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • StringUtil

      public StringUtil()
  • Method Details

    • isNumeric

      public static final boolean isNumeric(String s)
      isNumeric (String) tests if all the characters of a string are digits
      Parameters:
      s - - the string to test
    • isAllBlank

      public static final boolean isAllBlank(String s)
      Return true if only space characters are found or empty
      Parameters:
      s -
      Returns:
      true=all space; false=at least one non-space
    • isEmpty

      public static final boolean isEmpty(String s)
      test if the string passed is null or empty
      Parameters:
      s - - the string test
      Returns:
      true or false
    • isEmpty

      public static final boolean isEmpty(StringBuffer sb)
      test if the StringBuffer passed is null or empty
      Parameters:
      sb - - the StringBuffer to test
      Returns:
      true or false
    • isEmpty

      public static final boolean isEmpty(byte[] b)
      test if the byte [] passed is null or empty
      Parameters:
      b - - the byte [] to test
      Returns:
      true or false
    • isNotEmpty

      public static final boolean isNotEmpty(String s)
      test if the string passed is not null and not empty
      Parameters:
      s - - the string to test
      Returns:
      true or false
    • isNotEmpty

      public static final boolean isNotEmpty(StringBuffer sb)
      test if the StringBuffer passed is not null and not empty
      Parameters:
      sb - - the StringBuffer to test
      Returns:
      true or false
    • isNotEmpty

      public static final boolean isNotEmpty(byte[] b)
      test if the byte [] passed is not null and not empty
      Parameters:
      b - - the byte [] to test
      Returns:
      true or false
    • squeeze

      public static final String squeeze(String s)
      squeeze the blanks from a string
      Parameters:
      s - - the string to squeeze
      Returns:
      a string with the blanks removed
    • squeeze

      public static final String squeeze(String s, String toRemove)
      squeeze a set of characters from a string
      Parameters:
      s - - the string to squeeze
      toRemove - - the characters to remove
      Returns:
      a string with the characters removed
    • compress

      public static final String compress(String s, String cval)
      compress multiple occurances of cval to one occurance
      Parameters:
      s - the string to compress
      cval - compress string
      Returns:
      compressed string
    • compress

      public static final String compress(String s)
      compress multiple occurances of blank to one occurance
      Parameters:
      s - the string to compress
      Returns:
      compressed string
    • escapeQuotes

      public static String escapeQuotes(String in)
      replace all occurances of single quote (') with two single quotes.
      Parameters:
      in - - the string to process
      Returns:
      the processed string
    • toHex

      public static String toHex(String inString)
      toHex (String) returns the passed string in hexadecimal
      Parameters:
      inString - - the string to convert
      Returns:
      the string in hex
    • removeControl

      public static String removeControl(String inString)
      removeControl - replace ASCII control characters with blank
      Parameters:
      inString - - the string to convert
      Returns:
      string without controls
    • stripNonAlphabetic

      public static String stripNonAlphabetic(String inString)
      stripNonAlphabetic remove all non-alphabetics
      Parameters:
      inString - - the string to strip
      Returns:
      string without alphabetics
    • strip

      public static String strip(String inString, String stripString)
    • toHex

      public static String toHex(byte[] inBytes)
      toHex (byte []) returns the passed byte array in hexadecimal
      Parameters:
      inBytes - - the byte array to convert
      Returns:
      the byte array in hex
    • toHex

      public static String toHex(byte[] inBytes, int maxbytes)
      toHex (byte []) returns the passed byte array in hexadecimal
      Parameters:
      inBytes - - the byte array to convert
      Returns:
      the byte array in hex
    • stackTrace

      public static String stackTrace(Throwable e)
      stackTrace returns the stack trace for the exception passed. This is the same stack trace that would be printed with the Exception.printStackTrace() call.
      Parameters:
      e - - The throwable to give the stack trace for.
    • byteArrToString

      public static String byteArrToString(byte[] byteArr)
      Converts an array of bytes to a string guaranteeing utf-8 mapping if possible
      Note works only with utf-8 and a byte-char set (e.g. any iso-8859-n)
      Parameters:
      byteArr - array of bytes to convert to string
      Returns:
      mapped String
    • streamToByteArray

      public static byte[] streamToByteArray(InputStream ioStream)
      convert ioStream to String
      Parameters:
      ioStream - stream to be converted
      encoding - String encoding from byte buffer
      Returns:
      converted String
    • streamToString

      public static String streamToString(InputStream ioStream, String encoding)
      convert ioStream to String
      Parameters:
      ioStream - stream to be converted
      encoding - String encoding from byte buffer
      Returns:
      converted String
    • stringToStream

      public static InputStream stringToStream(String inString, String encoding)
      convert String to InputStream
      Parameters:
      inString - string to be converted
      encoding - String encoding from byte buffer
      Returns:
      converted String
    • lowerCaseFirst

      public static String lowerCaseFirst(String inString)
      lowerCaseFirst (String) returns the passed string with first char lowercased , e.g. "CapString" becomes "capString". Added by SR
      Parameters:
      inString - - the string to convert
      Returns:
      the string with lowercased first char
    • upperCaseFirst

      public static String upperCaseFirst(String inString)
      upperCaseFirst (String) returns the passed string with first char uppercased , e.g. "capString" becomes "CapString". Added by SR
      Parameters:
      inString - - the string to convert
      Returns:
      the string with uppercased first char
    • matchRegex

      public static boolean matchRegex(String inString, String regex)
      MatchRegex (String) returns boolean value for whether string matches regex Added by SR For info on Java regex, which is quite similar to Perl regex, see http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html Examples: -- inString contains "a" or "b": regex = "a|b" -- inString contains "a" zero or more times: regex = "a*" -- inString contains "a" one or more times: regex = "a+"
      Parameters:
      inString - - the string to test
      regex - - the regex to match
      Returns:
      boolean matches or not
    • squeezeControl

      public static String squeezeControl(String in)
    • count

      public static int count(String source, String match)
      Count the number of times match occurs in source
      Parameters:
      source - string to be tested
      match - string to count in source
      Returns:
      number of times match occurs in source
    • argIsTrue

      public static boolean argIsTrue(String test) throws TException
      Test if passed value is a "true" value Used to validate that an argument means "true" or "yes"
      Parameters:
      test - String to be tested
      Returns:
      true="true" or "yes" value, false="false" or "no"
      Throws:
      TException
    • getUTF8

      public static String getUTF8(byte[] bytes)
    • xchange

      public static String xchange(String in, String from, String[] to)
      xchange replaces all occurances of a String of characters for the corresponding string from an array of String.
      Parameters:
      in - - the string to make changes in
      from - - the Sting of characters to look for
      to - - the array of Strings to substitute
      Returns:
      the string with substitutions made.
    • normParm

      public static String normParm(String in)
      A common normalization for parms: if null or has length zero then set null if all space characters set null trim space characters
      Parameters:
      in - string to convert
      Returns:
      string with leading and trailing non-space
    • leftPad

      public static String leftPad(String in, int size, char padc)