Package com.norconex.commons.lang.text
Class StringUtil
- java.lang.Object
-
- com.norconex.commons.lang.text.StringUtil
-
public final class StringUtil extends Object
String-related utility methods not found in popular libraries.- Since:
- 1.14.0
- Author:
- Pascal Essiembre
-
-
Field Summary
Fields Modifier and Type Field Description static int
TRUNCATE_HASH_LENGTH
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static int
countMatchesEnd(String str, String sub)
Counts the number of consecutive matches at end of a a string.static int
countMatchesStart(String str, String sub)
Counts the number of consecutive matches from the beginning of a string.static void
ifBlank(String str, Runnable runnable)
Consume the given string only if it is blank.static void
ifNotBlank(String str, Consumer<String> consumer)
Consume the given string only if it is not blank.static String
trimEnd(String str)
Trims white spaces at the end of a string.static String
trimStart(String str)
Trims 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 String
truncateBytesWithHash(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 String
truncateBytesWithHash(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 String
truncateWithHash(String text, int maxLength)
Truncate text larger than the given max length and appends a hash value from the truncated text.static String
truncateWithHash(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 Detail
-
TRUNCATE_HASH_LENGTH
public static final int TRUNCATE_HASH_LENGTH
- See Also:
- Constant Field Values
-
-
Method Detail
-
ifBlank
public static void ifBlank(String str, Runnable runnable)
Consume the given string only if it is blank.- Parameters:
str
- the string to consumerunnable
- code to be executed- Since:
- 3.0.0
-
ifNotBlank
public static void ifNotBlank(String str, Consumer<String> consumer)
Consume the given string only if it is not blank.- Parameters:
str
- the string to consumeconsumer
- string consumer- Since:
- 3.0.0
-
truncateWithHash
public static String truncateWithHash(String text, int maxLength)
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, themaxLength
argument 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
public static String truncateWithHash(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. The hash size has 10 digits. The hash and separator are added to fit within the maximum length supplied. For this reason, themaxLength
argument 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, themaxByteLength
argument 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, themaxByteLength
argument 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, themaxByteLength
argument 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, themaxByteLength
argument 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
public static String trimEnd(String str)
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
public static String trimStart(String str)
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
public static int countMatchesEnd(String str, String sub)
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
-
-