Package hec.util

Class TextUtil

java.lang.Object
hec.util.TextUtil

public class TextUtil extends Object
Miscellaneous text utilities
  • Constructor Details

    • TextUtil

      public TextUtil()
  • Method Details

    • split

      public static String[] split(String str, String delim, String opts)
      Splits a string around a specified regex delimiter. The delimiter is not included in the parts.
      Parameters:
      str - The string to split
      delim - The delimiter to use
      opts - A string specifying the regex options to use. The string can be composed zero or more of the following characters:
      CharacterMeaning
      Ejava.util.regex.Pattern.CANON_EQ
      Ijava.util.regex.Pattern.CASE_INSENSITIVE
      Cjava.util.regex.Pattern.COMMENTS
      Djava.util.regex.Pattern.DOTALL
      Ljava.util.regex.Pattern.LITERAL
      Ujava.util.regex.Pattern.UNICODE_CASE
      Xjava.util.regex.Pattern.UNIX_LINES
      Returns:
      The parts. If the delimiter is not found in the string, an array of length 1 is returned.
    • split

      public static String[] split(String str, String delim)
      Splits a string around a specified literal delimiter. The delimiter is not included in the parts.
      Parameters:
      str - The string to split
      delim - The delimiter to use
      Returns:
      The parts. If the delimiter is not found in the string, an array of length 1 is returned.
    • split

      public static String[] split(String str, String delim, String opts, int limit)
      Splits a string around a specified regex delimiter. The delimiter is not included in the parts.
      Parameters:
      str - The string to split
      delim - The delimiter to use
      opts - A string specifying the regex options to use. The string can be composed zero or more of the following characters:
      CharacterMeaning
      Ejava.util.regex.Pattern.CANON_EQ
      Ijava.util.regex.Pattern.CASE_INSENSITIVE
      Cjava.util.regex.Pattern.COMMENTS
      Djava.util.regex.Pattern.DOTALL
      Ljava.util.regex.Pattern.LITERAL
      Ujava.util.regex.Pattern.UNICODE_CASE
      Xjava.util.regex.Pattern.UNIX_LINES
      limit - The maximum number of parts to split into (not the maximum number of splits)
      Returns:
      The parts. If the delimiter is not found in the string, an array of length 1 is returned.
    • split

      public static String[] split(String str, String delim, int limit)
      Splits a string around a specified literal delimiter. The delimiter is not included in the parts.
      Parameters:
      str - The string to split
      delim - The delimiter to use
      limit - The maximum number of parts to split into (not the maximum number of splits). A non-positive number imposes no limit to the number of splits
      Returns:
      The parts. If the delimiter is not found in the string, an array of length 1 is returned.
    • join

      public static String join(String delim, String... parts)
      Concatenates individual strings together with a specified delimiter separating the individual parts
      Parameters:
      delim - The delimiter to insert between the individual strings.
      parts - The individual strings to concatenate together
      Returns:
      The concatenated string
    • replaceAll

      public static String replaceAll(String str, String old, String replacement, String opts)
      Replaces all occurrences of a specified regex in a string
      Parameters:
      str - The string to perform the replacement on
      old - The regex to replace all occurrences of
      replacement - The string to replace each occurrence of the regex
      opts - A string specifying the regex options to use. The string can be composed zero or more of the following characters:
      CharacterMeaning
      Ejava.util.regex.Pattern.CANON_EQ
      Ijava.util.regex.Pattern.CASE_INSENSITIVE
      Cjava.util.regex.Pattern.COMMENTS
      Djava.util.regex.Pattern.DOTALL
      Ljava.util.regex.Pattern.LITERAL
      Mjava.util.regex.Pattern.MULTILINE
      Ujava.util.regex.Pattern.UNICODE_CASE
      Xjava.util.regex.Pattern.UNIX_LINES
      Returns:
      The input string with all occurrences of the regex replaced
    • replaceAll

      public static String replaceAll(String str, String old, String replacement)
      Replaces all occurrences of a specified substring in a string
      Parameters:
      str - The string to perform the replacement on
      old - The substring to replace all occurrences of
      replacement - The string to replace each occurrence of the substring
      Returns:
      The input string with all occurrences of the substring replaced
    • replace

      public static String replace(String str, String old, String replacement, String opts, int occurrence)
      Replaces the specified occurrence of a specified regex in a string
      Parameters:
      str - The string to perform the replacement on
      old - The regex to replace the specified occurrence of
      replacement - The string to replace each occurrence of the regex
      opts - A string specifying the regex options to use. The string can be composed zero or more of the following characters:
      CharacterMeaning
      Ejava.util.regex.Pattern.CANON_EQ
      Ijava.util.regex.Pattern.CASE_INSENSITIVE
      Cjava.util.regex.Pattern.COMMENTS
      Djava.util.regex.Pattern.DOTALL
      Ljava.util.regex.Pattern.LITERAL
      Ujava.util.regex.Pattern.UNICODE_CASE
      Xjava.util.regex.Pattern.UNIX_LINES
      occurrence - The occurrence of the regex to replace. If < 0, the occurrence is counted from the end (-1 = last occurrence)
      Returns:
      The input string with the specified occurrence of the regex replaced
    • replace

      public static String replace(String str, String old, String replacement, int occurrence)
      Replaces the specified occurrence of a specified substring in a string
      Parameters:
      str - The string to perform the replacement on
      old - The substring to replace the specified occurrence of
      replacement - The string to replace each occurrence of the substring
      occurrence - The occurrence of the substring to replace. If < 0, the occurrence is counted from the end (-1 = last occurrence)
      Returns:
      The input string with the specified occurrence of the substring replaced
    • equals

      public static boolean equals(CharSequence txt1, CharSequence txt2)
      Returns whether to sequences of characters are equal on a case-sensitive basis, where null is considered equivalent to an empty sequence
      Parameters:
      txt1 - The first sequence of characters
      txt2 - The second sequence of characters
      Returns:
      Whether the character sequences are equivalent
    • equals

      public static boolean equals(CharSequence txt1, CharSequence txt2, boolean nullEqualsEmpty)
      Returns whether to sequences of characters are equal on a case-sensitive basis
      Parameters:
      txt1 - The first sequence of characters
      txt2 - The second sequence of characters
      nullEqualsEmpty - Specifies whether to consider null equivalent to an empty sequence
      Returns:
      Whether the character sequences are equivalent
    • equalsIgnoreCase

      public static boolean equalsIgnoreCase(CharSequence txt1, CharSequence txt2)
      Returns whether to sequences of characters are equal on a case-insensitive basis, where null is considered equivalent to an empty sequence
      Parameters:
      txt1 - The first sequence of characters
      txt2 - The second sequence of characters
      Returns:
      Whether the character sequences are equivalent
    • equalsIgnoreCase

      public static boolean equalsIgnoreCase(CharSequence txt1, CharSequence txt2, boolean nullEqualsEmpty)
      Returns whether to sequences of characters are equal on a case-insensitive basis
      Parameters:
      txt1 - The first sequence of characters
      txt2 - The second sequence of characters
      nullEqualsEmpty - Specifies whether to consider null equivalent to an empty sequence
      Returns:
      Whether the character sequences are equivalent
    • toTitleCase

      public static String toTitleCase(String input)
      Returns a title-cased version of the input string
      Parameters:
      input - The input string
      Returns:
      A title-cased version of the input string
    • compress

      public static String compress(String str) throws IOException
      Compresses a string using gzip compression
      Parameters:
      str - The string to compress
      Returns:
      The compressed string
      Throws:
      IOException
    • compress

      public static String compress(String str, String encodingName) throws IOException
      Compresses a string using gzip compression, optionally encoding the result
      Parameters:
      str - The string to compress
      encodingName - The encoding to apply to the compressed string. Currently only "base64" is supported. Null specifies no encoding
      Returns:
      The compressed, possibly encoded, string
      Throws:
      IOException
    • uncompress

      public static String uncompress(String str) throws IOException
      Decompresses a string that has been compressed with gzip compression (and no encoding applied)
      Parameters:
      str - The string to decompress
      Returns:
      The decompressed string
      Throws:
      IOException
    • uncompress

      public static String uncompress(String str, String encodingName) throws IOException
      Decompresses a string that has been compressed with gzip compression and possibly encoded
      Parameters:
      str - The string to decompress
      encodingName - The name of the encoding that has been applied to the compressed string. Currently only "base64" is supported. Null specifies no encoding has been applied
      Returns:
      The decompressed string
      Throws:
      IOException
    • reduceParentheses

      public static String reduceParentheses(String str)
      Removes unnecessary parentheses from a string. Enclosing parentheses and more that one level of parentheses without other intervening characters are considered unnecessary.
      Parameters:
      str - The string to have the unnecessary parentheses removed from
      Returns:
      The string with the unnecessary parentheses removed.
    • xmlEntityEncode

      public static String xmlEntityEncode(String str)
      Replace chars with XML entities
      Parameters:
      str - The source string
      Returns:
      The string with the replacements made
    • xmlEntityDecode

      public static String xmlEntityDecode(String str)
      Replace XML entities with chars
      Parameters:
      str - The source string
      Returns:
      The string with the replacements made
    • glob_match

      public static boolean glob_match(CharSequence str, CharSequence pat)
      Tests a string against a glob-style pattern
      Parameters:
      str - the string to test
      pat - the glob-style pattern to test against
      Returns:
      true if the pattern matches the string
    • glob_match

      public static boolean glob_match(char[] str_chars, char[] pat_chars, int s, int p)
      Tests a string against a glob-style pattern
      Parameters:
      str_chars - the string to test as a character array
      pat_chars - the glob-style pattern to test against as a character array
      s - the starting offset in the string character array
      p - the starting offset in the pattern character array
      Returns:
      true if the pattern matches the string
    • glob2Regex

      public static String glob2Regex(String globStr)
      Converts a glob pattern into a regular expression. Taken from here. Handles standard glob patterns:
      • * matches zero or more characters
      • ? matches exactly one character
      • [AaBbCc] matches A, B, or C in upper or lower case
      • [0-9] matches any single digit
      Does not handle extended glob patterns
      Parameters:
      globStr - The glob pattern to convert
      Returns:
      The equivalent regular expression