Package com.norconex.commons.lang.text
Class StringUtil
java.lang.Object
com.norconex.commons.lang.text.StringUtil
String-related utility methods not found in popular libraries.
- Since:
- 1.14.0
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic intcountMatchesEnd(String str, String sub) Counts the number of consecutive matches at end of a a string.static intcountMatchesStart(String str, String sub) Counts the number of consecutive matches from the beginning of a string.static voidConsume the given string only if it is blank.static voidifNotBlank(String str, Consumer<String> consumer) Consume the given string only if it is not blank.static StringConverts an English plural word to singular one using very simple heuristics.static StringsingularOrElse(String plural, String defaultValue) Converts an English plural word to singular one using very simple heuristics.static StringTrims white spaces at the end of a string.static StringTrims white spaces at the beginning of a string.static byte[]truncateBytesWithHash(byte[] bytes, Charset charset, int maxByteLength) Truncates character byte array text larger than the given max byte length and appends a hash value from the truncated text.static byte[]truncateBytesWithHash(byte[] bytes, Charset charset, int maxByteLength, String separator) Truncates character byte array text larger than the given max byte length and appends a hash value from the truncated text, with an optional separator in-between.static StringtruncateBytesWithHash(String text, Charset charset, int maxByteLength) Truncates text with size in bytes larger than the given max byte length and appends a hash value from the truncated text.static StringtruncateBytesWithHash(String text, Charset charset, int maxByteLength, String separator) Truncates text with size in bytes larger than the given max byte length and appends a hash value from the truncated text, with an optional separator in-between.static StringtruncateWithHash(String text, int maxLength) Truncate text larger than the given max length and appends a hash value from the truncated text.static StringtruncateWithHash(String text, int maxLength, String separator) Truncate text larger than the given max length and appends a hash value from the truncated text, with an optional separator in-between.
-
Field Details
-
TRUNCATE_HASH_LENGTH
public static final int TRUNCATE_HASH_LENGTH- See Also:
-
-
Method Details
-
ifBlank
Consume the given string only if it is blank.- Parameters:
str- the string to consumerunnable- code to be executed- Since:
- 3.0.0
-
ifNotBlank
Consume the given string only if it is not blank.- Parameters:
str- the string to consumeconsumer- string consumer- Since:
- 3.0.0
-
truncateWithHash
Truncate text larger than the given max length and appends a hash value from the truncated text. The hash size has 10 digits. The hash is added to fit within the maximum length supplied. For this reason, themaxLengthargument must be be minimum 10 for any truncation to occur. The hash is added without a separator. To insert a separator between the truncated text and the hash code, usetruncateWithHash(String, int, String)- Parameters:
text- text to truncatemaxLength- maximum length the truncated text must have- Returns:
- truncated text, or original text if no truncation required
-
truncateWithHash
Truncate text larger than the given max length and appends a hash value from the truncated text, with an optional separator in-between. The hash size has 10 digits. The hash and separator are added to fit within the maximum length supplied. For this reason, themaxLengthargument must be be minimum 10 + separator length for any truncation to occur.- Parameters:
text- text to truncatemaxLength- maximum length the truncated text must haveseparator- string separating truncated text from hash code- Returns:
- truncated text, or original text if no truncation required
-
truncateBytesWithHash
public static String truncateBytesWithHash(String text, Charset charset, int maxByteLength) throws CharacterCodingException Truncates text with size in bytes larger than the given max byte length and appends a hash value from the truncated text. The hash size is equal to the byte length of 10 digits using the given charset. The hash and separator are added to fit within the maximum byte length supplied. For this reason, themaxByteLengthargument must be be large enough for any truncation to occur.- Parameters:
text- text to truncatecharset- character encodingmaxByteLength- maximum byte length the truncated text must have- Returns:
- truncated character byte array, or original text if no truncation required
- Throws:
CharacterCodingException- character coding problem
-
truncateBytesWithHash
public static String truncateBytesWithHash(String text, Charset charset, int maxByteLength, String separator) throws CharacterCodingException Truncates text with size in bytes larger than the given max byte length and appends a hash value from the truncated text, with an optional separator in-between. The hash size is equal to the byte length of 10 digits using the given charset. The hash and separator are added to fit within the maximum byte length supplied. For this reason, themaxByteLengthargument must be be large enough for any truncation to occur.- Parameters:
text- text to truncatecharset- character encodingmaxByteLength- maximum byte length the truncated text must haveseparator- string separating truncated text from hash code- Returns:
- truncated character byte array, or original text if no truncation required
- Throws:
CharacterCodingException- character coding problem
-
truncateBytesWithHash
public static byte[] truncateBytesWithHash(byte[] bytes, Charset charset, int maxByteLength) throws CharacterCodingException Truncates character byte array text larger than the given max byte length and appends a hash value from the truncated text. The hash size is equal to the byte length of 10 digits using the given charset. The hash and separator are added to fit within the maximum byte length supplied. For this reason, themaxByteLengthargument must be be large enough for any truncation to occur.- Parameters:
bytes- byte array of text to truncatecharset- character encodingmaxByteLength- maximum byte length the truncated text must have- Returns:
- truncated character byte array, or original text if no truncation required
- Throws:
CharacterCodingException- character coding problem
-
truncateBytesWithHash
public static byte[] truncateBytesWithHash(byte[] bytes, Charset charset, int maxByteLength, String separator) throws CharacterCodingException Truncates character byte array text larger than the given max byte length and appends a hash value from the truncated text, with an optional separator in-between. The hash size is equal to the byte length of 10 digits using the given charset. The hash and separator are added to fit within the maximum byte length supplied. For this reason, themaxByteLengthargument must be be large enough for any truncation to occur.- Parameters:
bytes- byte array of text to truncatecharset- character encodingmaxByteLength- maximum byte length the truncated text must haveseparator- string separating truncated text from hash code- Returns:
- truncated character byte array, or original text if no truncation required
- Throws:
CharacterCodingException- character coding problem
-
trimEnd
Trims white spaces at the end of a string.- Parameters:
str- the string to trim its end- Returns:
- trimmed string
- Since:
- 2.0.0
-
trimStart
Trims white spaces at the beginning of a string.- Parameters:
str- the string to trim its beginning- Returns:
- trimmed string
- Since:
- 2.0.0
-
countMatchesEnd
Counts the number of consecutive matches at end of a a string.- Parameters:
str- the string to checksub- the substring to count- Returns:
- number of matches from the start
- Since:
- 2.0.0
-
countMatchesStart
Counts the number of consecutive matches from the beginning of a string.- Parameters:
str- the string to checksub- the substring to count- Returns:
- number of matches from the end
- Since:
- 2.0.0
-
singular
Converts an English plural word to singular one using very simple heuristics. Returnsnullif the plural word isnull. Else, returns the word unchanged if it cannot find a singular variant. You are encouraged to used a more sophisticated approach if accuracy is important.- Parameters:
plural- plural word- Returns:
- singular word
- Since:
- 3.0.0
-
singularOrElse
Converts an English plural word to singular one using very simple heuristics. Returnsnullif the plural word isnull. Else, returns the default value if it cannot find a singular variant. You are encouraged to used a more sophisticated approach if accuracy is important.- Parameters:
plural- plural worddefaultValue- default value if singular can't be resolved- Returns:
- singular word or
null - Since:
- 3.0.0
-