Package hec.util
Class TextUtil
java.lang.Object
hec.util.TextUtil
Miscellaneous text utilities
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic String
Compresses a string using gzip compressionstatic String
Compresses a string using gzip compression, optionally encoding the resultstatic 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 sequencestatic boolean
equals
(CharSequence txt1, CharSequence txt2, boolean nullEqualsEmpty) Returns whether to sequences of characters are equal on a case-sensitive basisstatic 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 sequencestatic boolean
equalsIgnoreCase
(CharSequence txt1, CharSequence txt2, boolean nullEqualsEmpty) Returns whether to sequences of characters are equal on a case-insensitive basisstatic boolean
glob_match
(char[] str_chars, char[] pat_chars, int s, int p) Tests a string against a glob-style patternstatic boolean
glob_match
(CharSequence str, CharSequence pat) Tests a string against a glob-style patternstatic String
glob2Regex
(String globStr) Converts a glob pattern into a regular expression.static String
Concatenates individual strings together with a specified delimiter separating the individual partsstatic String
reduceParentheses
(String str) Removes unnecessary parentheses from a string.static String
Replaces the specified occurrence of a specified substring in a stringstatic String
Replaces the specified occurrence of a specified regex in a stringstatic String
replaceAll
(String str, String old, String replacement) Replaces all occurrences of a specified substring in a stringstatic String
replaceAll
(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 String
toTitleCase
(String input) Returns a title-cased version of the input stringstatic String
uncompress
(String str) Decompresses a string that has been compressed with gzip compression (and no encoding applied)static String
uncompress
(String str, String encodingName) Decompresses a string that has been compressed with gzip compression and possibly encodedstatic String
xmlEntityDecode
(String str) Replace XML entities with charsstatic String
xmlEntityEncode
(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
-