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 final int
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    Counts the number of consecutive matches at end of a a string.
    static int
    Counts the number of consecutive matches from the beginning of a string.
    static String
    Trims white spaces at the end of a string.
    static String
    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.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Method Details

    • 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, the maxLength 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, use truncateWithHash(String, int, String)
      Parameters:
      text - text to truncate
      maxLength - 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, the maxLength argument must be be minimum 10 + separator length for any truncation to occur.
      Parameters:
      text - text to truncate
      maxLength - maximum length the truncated text must have
      separator - 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, the maxByteLength argument must be be large enough for any truncation to occur.
      Parameters:
      text - text to truncate
      charset - character encoding
      maxByteLength - 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, the maxByteLength argument must be be large enough for any truncation to occur.
      Parameters:
      text - text to truncate
      charset - character encoding
      maxByteLength - maximum byte length the truncated text must have
      separator - 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, the maxByteLength argument must be be large enough for any truncation to occur.
      Parameters:
      bytes - byte array of text to truncate
      charset - character encoding
      maxByteLength - 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, the maxByteLength argument must be be large enough for any truncation to occur.
      Parameters:
      bytes - byte array of text to truncate
      charset - character encoding
      maxByteLength - maximum byte length the truncated text must have
      separator - 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 check
      sub - the substring to count
      Returns:
      number of matches from the start
      Since:
      2.0.0
    • countMatchesStart

      public static int countMatchesStart(String str, String sub)
      Counts the number of consecutive matches from the beginning of a string.
      Parameters:
      str - the string to check
      sub - the substring to count
      Returns:
      number of matches from the end
      Since:
      2.0.0