Package hec.util
Class TextUtil
java.lang.Object
hec.util.TextUtil
Miscellaneous text utilities
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringCompresses a string using gzip compressionstatic StringCompresses a string using gzip compression, optionally encoding the resultstatic booleanequals(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 sequencestatic booleanequals(CharSequence txt1, CharSequence txt2, boolean nullEqualsEmpty) Returns whether to sequences of characters are equal on a case-sensitive basisstatic booleanequalsIgnoreCase(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 sequencestatic booleanequalsIgnoreCase(CharSequence txt1, CharSequence txt2, boolean nullEqualsEmpty) Returns whether to sequences of characters are equal on a case-insensitive basisstatic booleanglob_match(char[] str_chars, char[] pat_chars, int s, int p) Tests a string against a glob-style patternstatic booleanglob_match(CharSequence str, CharSequence pat) Tests a string against a glob-style patternstatic Stringglob2Regex(String globStr) Converts a glob pattern into a regular expression.static StringConcatenates individual strings together with a specified delimiter separating the individual partsstatic StringreduceParentheses(String str) Removes unnecessary parentheses from a string.static StringReplaces the specified occurrence of a specified substring in a stringstatic StringReplaces the specified occurrence of a specified regex in a stringstatic StringreplaceAll(String str, String old, String replacement) Replaces all occurrences of a specified substring in a stringstatic StringreplaceAll(String str, String old, String replacement, String opts) Replaces all occurrences of a specified regex in a stringstatic String[]Splits a string around a specified literal delimiter.static String[]Splits a string around a specified literal delimiter.static String[]Splits a string around a specified regex delimiter.static String[]Splits a string around a specified regex delimiter.static StringtoTitleCase(String input) Returns a title-cased version of the input stringstatic Stringuncompress(String str) Decompresses a string that has been compressed with gzip compression (and no encoding applied)static Stringuncompress(String str, String encodingName) Decompresses a string that has been compressed with gzip compression and possibly encodedstatic StringxmlEntityDecode(String str) Replace XML entities with charsstatic StringxmlEntityEncode(String str) Replace chars with XML entities
-
Constructor Details
-
TextUtil
public TextUtil()
-
-
Method Details
-
split
Splits a string around a specified regex delimiter. The delimiter is not included in the parts.- Parameters:
str- The string to splitdelim- The delimiter to useopts- A string specifying the regex options to use. The string can be composed zero or more of the following characters:Character Meaning E java.util.regex.Pattern.CANON_EQ I java.util.regex.Pattern.CASE_INSENSITIVE C java.util.regex.Pattern.COMMENTS D java.util.regex.Pattern.DOTALL L java.util.regex.Pattern.LITERAL U java.util.regex.Pattern.UNICODE_CASE X java.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
Splits a string around a specified literal delimiter. The delimiter is not included in the parts.- Parameters:
str- The string to splitdelim- The delimiter to use- Returns:
- The parts. If the delimiter is not found in the string, an array of length 1 is returned.
-
split
Splits a string around a specified regex delimiter. The delimiter is not included in the parts.- Parameters:
str- The string to splitdelim- The delimiter to useopts- A string specifying the regex options to use. The string can be composed zero or more of the following characters:Character Meaning E java.util.regex.Pattern.CANON_EQ I java.util.regex.Pattern.CASE_INSENSITIVE C java.util.regex.Pattern.COMMENTS D java.util.regex.Pattern.DOTALL L java.util.regex.Pattern.LITERAL U java.util.regex.Pattern.UNICODE_CASE X java.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
Splits a string around a specified literal delimiter. The delimiter is not included in the parts.- Parameters:
str- The string to splitdelim- The delimiter to uselimit- 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
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
Replaces all occurrences of a specified regex in a string- Parameters:
str- The string to perform the replacement onold- The regex to replace all occurrences ofreplacement- The string to replace each occurrence of the regexopts- A string specifying the regex options to use. The string can be composed zero or more of the following characters:Character Meaning E java.util.regex.Pattern.CANON_EQ I java.util.regex.Pattern.CASE_INSENSITIVE C java.util.regex.Pattern.COMMENTS D java.util.regex.Pattern.DOTALL L java.util.regex.Pattern.LITERAL M java.util.regex.Pattern.MULTILINE U java.util.regex.Pattern.UNICODE_CASE X java.util.regex.Pattern.UNIX_LINES - Returns:
- The input string with all occurrences of the regex replaced
-
replaceAll
Replaces all occurrences of a specified substring in a string- Parameters:
str- The string to perform the replacement onold- The substring to replace all occurrences ofreplacement- 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 onold- The regex to replace the specified occurrence ofreplacement- The string to replace each occurrence of the regexopts- A string specifying the regex options to use. The string can be composed zero or more of the following characters:Character Meaning E java.util.regex.Pattern.CANON_EQ I java.util.regex.Pattern.CASE_INSENSITIVE C java.util.regex.Pattern.COMMENTS D java.util.regex.Pattern.DOTALL L java.util.regex.Pattern.LITERAL U java.util.regex.Pattern.UNICODE_CASE X java.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
Replaces the specified occurrence of a specified substring in a string- Parameters:
str- The string to perform the replacement onold- The substring to replace the specified occurrence ofreplacement- The string to replace each occurrence of the substringoccurrence- 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
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 characterstxt2- The second sequence of characters- Returns:
- Whether the character sequences are equivalent
-
equals
Returns whether to sequences of characters are equal on a case-sensitive basis- Parameters:
txt1- The first sequence of characterstxt2- The second sequence of charactersnullEqualsEmpty- Specifies whether to consider null equivalent to an empty sequence- Returns:
- Whether the character sequences are equivalent
-
equalsIgnoreCase
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 characterstxt2- 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 characterstxt2- The second sequence of charactersnullEqualsEmpty- Specifies whether to consider null equivalent to an empty sequence- Returns:
- Whether the character sequences are equivalent
-
toTitleCase
Returns a title-cased version of the input string- Parameters:
input- The input string- Returns:
- A title-cased version of the input string
-
compress
Compresses a string using gzip compression- Parameters:
str- The string to compress- Returns:
- The compressed string
- Throws:
IOException
-
compress
Compresses a string using gzip compression, optionally encoding the result- Parameters:
str- The string to compressencodingName- 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
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
Decompresses a string that has been compressed with gzip compression and possibly encoded- Parameters:
str- The string to decompressencodingName- 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
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
Replace chars with XML entities- Parameters:
str- The source string- Returns:
- The string with the replacements made
-
xmlEntityDecode
Replace XML entities with chars- Parameters:
str- The source string- Returns:
- The string with the replacements made
-
glob_match
Tests a string against a glob-style pattern- Parameters:
str- the string to testpat- 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 arraypat_chars- the glob-style pattern to test against as a character arrays- the starting offset in the string character arrayp- the starting offset in the pattern character array- Returns:
- true if the pattern matches the string
-
glob2Regex
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
- Parameters:
globStr- The glob pattern to convert- Returns:
- The equivalent regular expression
-