Class StringUtils


  • public class StringUtils
    extends java.lang.Object
    Provides simple static methods for string manipulation
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String concatString​(java.lang.String[] p, java.lang.String token)
      Concatenates a String array into a single String, using the token parameter as a separator.
      static java.lang.String expandEnvironmentVariables​(java.lang.String arg)
      Expands any environment variables in the specified text.
      static java.lang.String findInbetween​(java.lang.String text, java.lang.String startPattern, java.lang.String endPattern, boolean ignoreCase)
      Searches for two String objects in text and returns the text between them, or an empty string if the patterns could not be matched.
      static byte[] hexToBinary​(java.lang.String hex)
      Returns a byte array containing the values of a sequence of hexadecimal numbers in the hex String parameter.
      static java.lang.String hexToString​(byte[] hex)
      Returns a hexadecimal string representation of the byte values in the hex parameter.
      static java.lang.String simplifyWhitespace​(java.lang.String text)
      Replaces one or more consecutive white space characters by a single space character, and trims white space from each end of a String.
      static java.lang.String[] splitString​(java.lang.String p, java.lang.String token)
      Splits a String based upon any character in the specified token.
      static java.util.ArrayList<java.lang.String> splitStringExcel​(java.lang.String line, java.lang.String delim)
      Split a String using Microsoft Excel-style quoting.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • splitString

        public static java.lang.String[] splitString​(java.lang.String p,
                                                     java.lang.String token)
        Splits a String based upon any character in the specified token.

        The string is split on any individual character in the token parameter, not the string as a whole. e.g.

        
         String[] tokens = StringUtils.splitString("Split me up! Word1 Word2 Word3.", " o");
         // tokens now contains { "Split", "me", "up!", "W", "rd1", "W", "rd2", "W", "rd3." } 
        
         for (String token: tokens)
         {
             writeMessage("Token: " + token);
         }
         
        Parameters:
        p - the String to split
        token - the characters to use as delimiters
        Returns:
        an array of String objects
      • splitStringExcel

        public static java.util.ArrayList<java.lang.String> splitStringExcel​(java.lang.String line,
                                                                             java.lang.String delim)
        Split a String using Microsoft Excel-style quoting.
        Parameters:
        line - the String to split
        delim - the token to use as delimiters
        Returns:
        a list of String objects
      • concatString

        public static java.lang.String concatString​(java.lang.String[] p,
                                                    java.lang.String token)
        Concatenates a String array into a single String, using the token parameter as a separator.

        e.g.

        
         String value = StringUtils.concatString(new String[] { "item1", "item2", "item3" }, ", ");
         writeMessage(value);
         
        Parameters:
        p - the array of String objects to concatenate
        token - the delimiter to place between the String objects
        Returns:
        the concatenated String
      • expandEnvironmentVariables

        public static java.lang.String expandEnvironmentVariables​(java.lang.String arg)
        Expands any environment variables in the specified text.

        Any environment variables in the form %VARIABLE% will be replaced with the value of the environment variable on the injector machine at runtime.

        Parameters:
        arg - the text to parse
        Returns:
        the text, with any environment variables expanded
      • findInbetween

        public static java.lang.String findInbetween​(java.lang.String text,
                                                     java.lang.String startPattern,
                                                     java.lang.String endPattern,
                                                     boolean ignoreCase)
        Searches for two String objects in text and returns the text between them, or an empty string if the patterns could not be matched.

        e.g.

        
         // If the web page contained the string ...name="John Smith">, this call will return
         // the text "John Smith" (excluding the quotes)
         String page = response.getContent(); // get the contents of the response
         String myValue = StringUtils.findInbetween(page, "name=\"", "\">", true); // find the text between value=" and ">
         
        Parameters:
        text - the String to search in
        startPattern - a String which occurs immediately before the text to be extracted
        endPattern - a String which occurs immediately after the text to be extracted
        ignoreCase - true if the search should be case-independent
        Returns:
        the String found between the start and end patterns, or an empty String if there is no match
      • simplifyWhitespace

        public static java.lang.String simplifyWhitespace​(java.lang.String text)
        Replaces one or more consecutive white space characters by a single space character, and trims white space from each end of a String.

        White space comprises of the characters ' \t\n\r' - space, tab, line feed and newline.

        e.g.

        
         String s = "  This is  a\t\rstring\n\nwith white space  ";
         s = StringUtils.simplifyWhitespace(s);
         // s now contains "This is a string with white space"
         
        Parameters:
        text - the String to be modified
        Returns:
        the modified String
      • hexToBinary

        public static byte[] hexToBinary​(java.lang.String hex)
        Returns a byte array containing the values of a sequence of hexadecimal numbers in the hex String parameter.

        This method is used to convert a String containing the hexadecimal representation of a sequence of bytes into a byte array containing the binary values.

        The input string must contain pairs of hexadecimal characters; white space or any non hexadecimal character between each pair is ignored.

        If the second character of an expected pair of hexadecimal characters is not a valid hex character, the conversion is terminated.

        Valid input format examples:

        • "8014833031F300AB15974D4252204157"
        • "80 14 83 f3 31 F3 00 ab 15 97 4d 42 ef 20 41 57"
        • "x80 x14 xAA x30"
        • "80 1483xAAx31 \\x33 0080bcef4d4252204157"

        e.g.

        
         String EBCDIC = "C2C9C8C4F0F1F8F04040F0F240404040404040404040E2D";
         byte[] hex = StringUtils.hexToBinary("EBCDIC");
         writeMessage(StringUtils.hexToString(hex)); //  display contents
         
        Parameters:
        hex - the input String, composed of pairs of hexadecimal characters where each pair represents a single byte
        Returns:
        the binary values of the hexadecimal bytes in the input string
        See Also:
        hexToString(byte[])
      • hexToString

        public static java.lang.String hexToString​(byte[] hex)
        Returns a hexadecimal string representation of the byte values in the hex parameter.

        The returned string is in the format typically used to display binary data such as in an ethernet trace or the log file output from a TCP Virtual User.

        Typical output format example:

        • "80 14 83 f3 31 F3 00 ab 15 97 4d 42 ef 20 41 57"
        Parameters:
        hex - the input byte array
        Returns:
        a hexadecimal string representation of the byte values
        See Also:
        hexToBinary(String)