Class StringMethods

java.lang.Object
org.klojang.util.StringMethods

public final class StringMethods extends Object
Methods for working with strings. Quite a few methods in this class are geared towards printing. They take an argument of type Object, rather than String. If the argument is null, they will return an empty string, else they will call toString() on the argument and then manipulate the resulting String. They are null-safe and they will never return null themselves. The parameter name for the Object argument will be "input". For ease of reading the input parameter will still be referred to as a String.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    The empty string.
  • Method Summary

    Modifier and Type
    Method
    Description
    Appends the specified value to the specified StringBuilder and returns the StringBuilder.
    append(StringBuilder sb, Object val0, Object val1)
    Appends the specified values to the specified StringBuilder and returns the StringBuilder.
    append(StringBuilder sb, Object val0, Object val1, Object val2)
    Appends the specified values to the specified StringBuilder and returns the StringBuilder.
    append(StringBuilder sb, Object val0, Object val1, Object val2, Object val3)
    Appends the specified values to the specified StringBuilder and returns the StringBuilder.
    append(StringBuilder sb, Object val0, Object val1, Object val2, Object val3, Object val4)
    Appends the specified values to the specified StringBuilder and returns the StringBuilder.
    append(StringBuilder sb, Object val0, Object val1, Object val2, Object val3, Object val4, Object val5)
    Appends the specified values to the specified StringBuilder and returns the StringBuilder.
    append(StringBuilder sb, Object val0, Object val1, Object val2, Object val3, Object val4, Object val5, Object val6)
    Appends the specified values to the specified StringBuilder and returns the StringBuilder.
    append(StringBuilder sb, Object val0, Object val1, Object val2, Object val3, Object val4, Object val5, Object val6, Object val7)
    Appends the specified values to the specified StringBuilder and returns the StringBuilder.
    append(StringBuilder sb, Object val0, Object val1, Object val2, Object val3, Object val4, Object val5, Object val6, Object val7, Object val8)
    Appends the specified values to the specified StringBuilder and returns the StringBuilder.
    append(StringBuilder sb, Object val0, Object val1, Object val2, Object val3, Object val4, Object val5, Object val6, Object val7, Object val8, Object val9, Object... moreData)
    Appends the specified values to the specified StringBuilder and returns the StringBuilder.
    static String
    concat(Object... data)
    Concatenates the specified data.
    static int
    count(Object input, String substr)
    Counts the number of occurrences of substr within input.
    static int
    count(Object input, String substr, boolean ignoreCase)
    Counts the number of occurrences of substr within input.
    static int
    count(Object input, String substr, boolean ignoreCase, int limit)
    Counts the number of occurrences of substr within input.
    static int
    countDiscrete(Object input, String substr)
    Counts the number of non-overlapping occurrences of substr within input.
    static int
    countDiscrete(Object input, String substr, boolean ignoreCase)
    Counts the number of non-overlapping occurrences of substr within input.
    static int
    countDiscrete(Object input, String substr, boolean ignoreCase, int limit)
    Counts the number of non-overlapping occurrences of substr within input.
    static String
    ellipsis(Object input, int maxWidth)
    Returns input.toString() if its length does not exceed maxWidth, else truncates the string and appends "...", such that the new string's length does not exceed maxWidth.
    endsWith(Object input, boolean ignoreCase, String... suffixes)
    Determines whether input ends with any of the specified suffixes.
    endsWith(Object input, boolean ignoreCase, Collection<String> suffixes)
    Determines whether input ends with any of the specified suffixes.
    static String
    ensurePrefix(Object input, String prefix)
    Prefixes to specified prefix to input if it did not already start with that prefix.
    static String
    ensureSuffix(Object input, String suffix)
    Appends to specified suffix to input if it did not already have that suffix.
    static String
    Ensures that the first character of the specified string is not an uppercase character.
    static String
    Ensures that the first character of the specified string is not a lowercase character.
    static int[]
    getLineAndColumn(String str, int index)
    Returns the line number and column number of the character at the specified index, given the system-defined line separator.
    static int[]
    getLineAndColumn(String str, int index, String lineSep)
    Returns the line number and column number of the character at the specified index, given the specified line separator.
    static String
    ifBlank(Object input, String dfault)
    Returns the 1st argument if it is not a whitespace-only string, else the 2nd argument.
    static int
    indexOf(Object input, String substr, int occurrence)
    Returns the index of the nth occurrence of the specified substring within input.
    static boolean
    isBlank(Object input)
    Whether the specified string is null or blank.
    static String
    lchop(Object input, boolean ignoreCase, String... prefixes)
    Removes all occurrences of the specified prefixes from the start of a string.
    static String
    lchop(Object input, String... prefixes)
    Removes all occurrences of the specified prefixes from the start of a string.
    static String
    lpad(Object input, int width)
    Left-pads a string to the specified width using the space character (' ').
    static String
    lpad(Object input, int width, char padChar)
    Left-pads a string to the specified width using the specified padding character.
    static String
    lpad(Object input, int width, char padChar, String delimiter)
    Left-pads a string to the specified width using the specified padding character and then appends the specified terminator.
    static String
    ltrim(Object input, String chars)
    Left-trims all characters contained in chars from the specified string.
    static String
    pad(Object input, int width)
    Centers (left- and right-pads) a string within the specified width using the space character.
    static String
    pad(Object input, int width, char padChar)
    Centers (left- and right-pads) a string within the specified width using the specified padding character.
    static String
    pad(Object input, int width, char padChar, String delimiter)
    Centers (left- and right-pads) a string within the specified width using the specified padding character and then appends the specified delimiter.
    static String
    rchop(Object input, boolean ignoreCase, String... suffixes)
    Removes all occurrences of the specified suffixes from the end of a string.
    static String
    rchop(Object input, String... suffixes)
    Removes all occurrences of the specified suffixes from the end of a string.
    static String
    rpad(Object input, int width)
    Right-pads a string to the specified width using the space character (' ').
    static String
    rpad(Object input, int width, char padChar)
    Right-pads a string to the specified width using the specified padding character.
    static String
    rpad(Object input, int width, char padChar, String suffix)
    Right-pads a string to the specified width using the specified padding character and appends the specified suffix.
    static String
    rtrim(Object input, String chars)
    Right-trims all characters contained in chars from the specified string.
    startsWith(Object input, boolean ignoreCase, String... prefixes)
    Determines whether input starts with any of the specified prefixes.
    startsWith(Object input, boolean ignoreCase, Collection<String> prefixes)
    Determines whether input starts with any of the specified prefixes.
    static String
    substr(String str, int from)
    Substring method that facilitates substring retrieval relative to the end of a string.
    static String
    substr(String str, int from, int length)
    Substring method that facilitates substring retrieval relative to the end of a string as well as substring retrieval in the opposite direction.
    static String
    substrAfter(Object input, String substr, int occurrence)
    Returns the substring after the nth occurrence of the specified substring, or the entire string if there is no nth occurrence of the substring.
    static String
    substrFrom(Object input, String substr, int occurrence)
    Returns the substring from (and including) the nth occurrence of the specified substring, or the entire string if there is no nth occurrence of the substring.
    static String
    substringBefore(Object input, String substr, int occurrence)
    Returns the substring up to, but not including the nth occurrence of the specified substring, or the entire string if there is no nth occurrence of the substring.
    static String
    substringOnTo(Object input, String substr, int occurrence)
    Returns the substring up to, and including the nth occurrence of the specified substring, or the entire string if there is no nth occurrence of the substring.
    static String
    Returns a short string representation of an object.
    static String
    toShortString(Object obj, int maxWidth)
    Returns a short string representation of an object.
    static String
    toShortString(Object obj, int maxWidth, int maxElements, int maxEntries)
    Returns a short string representation of an object.
    static String
    trim(Object input, String chars)
    Left and right-trims the specified string.

    Methods inherited from class java.lang.Object

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

  • Method Details

    • append

      public static StringBuilder append(StringBuilder sb, Object val)
      Appends the specified value to the specified StringBuilder and returns the StringBuilder.
      Parameters:
      sb - the StringBuilder to append the value to
      val - The value to append
      Returns:
      the StringBuilder
    • append

      public static StringBuilder append(StringBuilder sb, Object val0, Object val1)
      Appends the specified values to the specified StringBuilder and returns the StringBuilder.
      Parameters:
      sb - the StringBuilder to append the values to
      val0 - a value
      val1 - another value
      Returns:
      the StringBuilder
    • append

      public static StringBuilder append(StringBuilder sb, Object val0, Object val1, Object val2)
      Appends the specified values to the specified StringBuilder and returns the StringBuilder.
      Parameters:
      sb - the StringBuilder to append the values to
      val0 - a value
      val1 - another value
      val2 - another value
      Returns:
      the StringBuilder
    • append

      public static StringBuilder append(StringBuilder sb, Object val0, Object val1, Object val2, Object val3)
      Appends the specified values to the specified StringBuilder and returns the StringBuilder.
      Parameters:
      sb - the StringBuilder to append the values to
      val0 - a value
      val1 - another value
      val2 - another value
      val3 - another value
      Returns:
      the StringBuilder
    • append

      public static StringBuilder append(StringBuilder sb, Object val0, Object val1, Object val2, Object val3, Object val4)
      Appends the specified values to the specified StringBuilder and returns the StringBuilder.
      Parameters:
      sb - the StringBuilder to append the values to
      val0 - a value
      val1 - another value
      val2 - another value
      val3 - another value
      val4 - another value
      Returns:
      the StringBuilder
    • append

      public static StringBuilder append(StringBuilder sb, Object val0, Object val1, Object val2, Object val3, Object val4, Object val5)
      Appends the specified values to the specified StringBuilder and returns the StringBuilder.
      Parameters:
      sb - the StringBuilder to append the values to
      val0 - a value
      val1 - another value
      val2 - another value
      val3 - another value
      val4 - another value
      val5 - another value
      Returns:
      the StringBuilder
    • append

      public static StringBuilder append(StringBuilder sb, Object val0, Object val1, Object val2, Object val3, Object val4, Object val5, Object val6)
      Appends the specified values to the specified StringBuilder and returns the StringBuilder.
      Parameters:
      sb - the StringBuilder to append the values to
      val0 - a value
      val1 - another value
      val2 - another value
      val3 - another value
      val4 - another value
      val5 - another value
      val6 - another value
      Returns:
      the StringBuilder
    • append

      public static StringBuilder append(StringBuilder sb, Object val0, Object val1, Object val2, Object val3, Object val4, Object val5, Object val6, Object val7)
      Appends the specified values to the specified StringBuilder and returns the StringBuilder.
      Parameters:
      sb - the StringBuilder to append the values to
      val0 - a value
      val1 - another value
      val2 - another value
      val3 - another value
      val4 - another value
      val5 - another value
      val6 - another value
      val7 - another value
      Returns:
      the StringBuilder
    • append

      public static StringBuilder append(StringBuilder sb, Object val0, Object val1, Object val2, Object val3, Object val4, Object val5, Object val6, Object val7, Object val8)
      Appends the specified values to the specified StringBuilder and returns the StringBuilder.
      Parameters:
      sb - the StringBuilder to append the values to
      val0 - a value
      val1 - another value
      val2 - another value
      val3 - another value
      val4 - another value
      val5 - another value
      val6 - another value
      val7 - another value
      val8 - another value
      Returns:
      the StringBuilder
    • append

      public static StringBuilder append(StringBuilder sb, Object val0, Object val1, Object val2, Object val3, Object val4, Object val5, Object val6, Object val7, Object val8, Object val9, Object... moreData)
      Appends the specified values to the specified StringBuilder and returns the StringBuilder.
      Parameters:
      sb - the StringBuilder to append the values to
      val0 - a value
      val1 - another value
      val2 - another value
      val3 - another value
      val4 - another value
      val5 - another value
      val6 - another value
      val7 - another value
      val8 - another value
      val9 - another value
      moreData - more values
      Returns:
      the StringBuilder
    • concat

      public static String concat(Object... data)
      Concatenates the specified data.
      Parameters:
      data - the data to append (must not be null)
      Returns:
      the concatenation of the data
    • count

      public static int count(Object input, String substr)
      Counts the number of occurrences of substr within input. Returns 0 (zero) if input is null.
      Parameters:
      input - the string to search
      substr - the substring to search for (must not be null or empty)
      Returns:
      the number of occurrences of substr within input
    • count

      public static int count(Object input, String substr, boolean ignoreCase)
      Counts the number of occurrences of substr within input. Returns 0 (zero) if input is null.
      Parameters:
      input - the string to search
      substr - the substring to search for (must not be null or empty)
      ignoreCase - whether to ignore case while comparing substrings
      Returns:
      the number of occurrences of substr within input
    • count

      public static int count(Object input, String substr, boolean ignoreCase, int limit)
      Counts the number of occurrences of substr within input. Returns 0 (zero) if input is null.
      Parameters:
      input - the string to search
      substr - the substring to search for (must not be null or empty)
      ignoreCase - whether to ignore case while comparing substrings
      limit - the maximum number of occurrences the count. You may specify 0 (zero) for "no maximum".
      Returns:
      the number of occurrences of substr within input (will not exceed limit)
    • countDiscrete

      public static int countDiscrete(Object input, String substr)
      Counts the number of non-overlapping occurrences of substr within input. The string to search for must not be null or empty and is not treated as a regular expression. Returns 0 (zero) if input is null.
      Parameters:
      input - the string to search
      substr - the substring to search for
      Returns:
      the number of non-overlapping occurrences of substr within input
    • countDiscrete

      public static int countDiscrete(Object input, String substr, boolean ignoreCase)
      Counts the number of non-overlapping occurrences of substr within input. The string to search for must not be null or empty and is not treated as a regular expression. Returns 0 (zero) if input is null.
      Parameters:
      input - the string to search
      substr - the substring to search for
      ignoreCase - whether to ignore case while comparing substrings
      Returns:
      the number of non-overlapping occurrences of substr within input
    • countDiscrete

      public static int countDiscrete(Object input, String substr, boolean ignoreCase, int limit)
      Counts the number of non-overlapping occurrences of substr within input. Returns 0 (zero) if input is null.
      Parameters:
      input - the string to search
      substr - the substring to search for
      ignoreCase - whether to ignore case while comparing substrings
      limit - the maximum number of occurrences the count. You may specify 0 (zero) for "no maximum".
      Returns:
      the number of non-overlapping occurrences of substr within input (will not exceed limit)
    • ellipsis

      public static String ellipsis(Object input, int maxWidth)
      Returns input.toString() if its length does not exceed maxWidth, else truncates the string and appends "...", such that the new string's length does not exceed maxWidth. The lower bound for maxWidth will tacitly be clamped to 4, so that at least one letter of the string is displayed.

      Examples:

       String hello = "Hello World, how are you?";
       assertEquals("Hello W...", ellipsis(hello, 10));
       assertEquals("H...", ellipsis(hello, 4));
       assertEquals(hello, ellipsis(hello, 100));
       
      Parameters:
      input - the string to abbreviate, if necessary
      maxWidth - the maximum width of the string (must be greater than 3)
      Returns:
      the string itself or an abbreviated version, suffixed with "..."
    • toShortString

      public static String toShortString(Object obj)
      Returns a short string representation of an object. Equivalent to toShortString(obj, 50).
      Parameters:
      obj - the object to stringify
      Returns:
      a string consisting of no more than 50 characters
    • toShortString

      public static String toShortString(Object obj, int maxWidth)
      Returns a short string representation of an object. Roughly equivalent to toShortString(obj, maxWidth, maxWidth/8, maxWidth/16).
      Parameters:
      obj - the object to stringify
      maxWidth - the maximum width of the returned string
      Returns:
      a string whose length will not exceed maxWidth
    • toShortString

      public static String toShortString(Object obj, int maxWidth, int maxElements, int maxEntries)
      Returns a short string representation of an object. Broadly speaking, this method behaves as follows:
      • if obj is null, it is stringified to "null".
      • if obj is a Class object, it is stringified using ClassMethods.simpleClassName.
      • if obj is a Collection, it is stringified as though by calling toString() on the collection after all but the first maxElements elements have been removed from it. (This prevents collections from blowing up into huge strings, only for them to be truncated to maxWidth again. So there is both a performance aspect and a security aspect to making the maximum number of elements user-definable.)
      • if obj is an array, it is stringified as though by calling Arrays.deepToString on it after all but the first maxElements elements have been removed from it.
      • if obj is a Map, it is stringified as though by calling toString() on the collection after all but the first maxEntries entries have been removed from it.
      • Otherwise obj is stringified simply by calling toString on it.

      The resulting string is then truncated to maxWidth as though by calling ellipsis on it.

      You should not rely on the exact appearance of the returned string. Future implementations may produce slightly different strings for the same object. (For example, they might give special treatment to certain other types of objects.) The only stated aim of this method is to provide a length-constrained string representation of an object. The exact contents of the returned string is unspecified.

      Parameters:
      obj - the object to stringify
      maxWidth - the maximum width of the returned string
      maxElements - the maximum number of elements to process if the argument is an array or Collection.
      maxEntries - the maximum number of entries to process if the argument is a Map.
      Returns:
      a string whose length will not exceed maxWidth
    • startsWith

      public static Optional<String> startsWith(Object input, boolean ignoreCase, Collection<String> prefixes)
      Determines whether input starts with any of the specified prefixes. Returns an Optional containing the first prefix found to be equal to the end of the string, or an empty Optional if the string does not end with any of the specified prefixes.
      Parameters:
      input - the string to test
      ignoreCase - whether to ignore case
      prefixes - the prefixes to test
      Returns:
      Returns an Optional containing the first prefix found to be equal to the end of the string, or an empty Optional if the string does not end with any of the specified prefixes.
    • startsWith

      public static Optional<String> startsWith(Object input, boolean ignoreCase, String... prefixes)
      Determines whether input starts with any of the specified prefixes. Returns an Optional containing the first prefix found to be equal to the end of the string, or an empty Optional if the string does not end with any of the specified prefixes.
      Parameters:
      input - the string to test
      ignoreCase - whether to ignore case
      prefixes - the prefixes to test
      Returns:
      Returns an Optional containing the first prefix found to be equal to the end of the string, or an empty Optional if the string does not end with any of the specified prefixes.
    • endsWith

      public static Optional<String> endsWith(Object input, boolean ignoreCase, Collection<String> suffixes)
      Determines whether input ends with any of the specified suffixes. Returns an Optional containing the first suffix found to be equal to the end of the string, or an empty Optional if the string does not end with any of the specified suffixes.
      Parameters:
      input - the string to test
      ignoreCase - whether to ignore case
      suffixes - the suffixes to test
      Returns:
      Returns an Optional containing the first suffix found to be equal to the end of the string, or an empty Optional if the string does not end with any of the specified suffixes.
    • endsWith

      public static Optional<String> endsWith(Object input, boolean ignoreCase, String... suffixes)
      Determines whether input ends with any of the specified suffixes. Returns an Optional containing the first suffix found to be equal to the end of the string, or an empty Optional if the string does not end with any of the specified suffixes.
      Parameters:
      input - the string to test
      ignoreCase - whether to ignore case
      suffixes - the suffixes to test
      Returns:
      Returns an Optional containing the first suffix found to be equal to the end of the string, or an empty Optional if the string does not end with any of the specified suffixes.
    • ensurePrefix

      public static String ensurePrefix(Object input, String prefix)
      Prefixes to specified prefix to input if it did not already start with that prefix. Returns prefix if input is null,
      Parameters:
      input - the String to which to append the prefix
      prefix - the prefix (must not be null)
      Returns:
      a string that is guaranteed to start with prefix
    • ensureSuffix

      public static String ensureSuffix(Object input, String suffix)
      Appends to specified suffix to input if it did not already have that suffix. If input is null, suffix is returned.
      Parameters:
      input - the String to which to append the suffix
      suffix - the suffix (must not be null)
      Returns:
      a string that is guaranteed to end with suffix
    • isBlank

      public static boolean isBlank(Object input)
      Whether the specified string is null or blank.
      Parameters:
      input - the string
      Returns:
      whether it is null or blank
    • ifBlank

      public static String ifBlank(Object input, String dfault)
      Returns the 1st argument if it is not a whitespace-only string, else the 2nd argument.
      Parameters:
      input - the string to return if not null
      dfault - the replacement string
      See Also:
    • lchop

      public static String lchop(Object input, String... prefixes)
      Removes all occurrences of the specified prefixes from the start of a string. The returned string will no longer start with any of the specified prefixes.
      Parameters:
      input - the string to remove the prefixes from
      prefixes - the prefixes to remove
    • lchop

      public static String lchop(Object input, boolean ignoreCase, String... prefixes)
      Removes all occurrences of the specified prefixes from the start of a string. The returned string will no longer start with any of the specified prefixes.
      Parameters:
      input - the string to remove the prefixes from
      ignoreCase - whether to ignore case
      prefixes - the prefixes to remove
    • rchop

      public static String rchop(Object input, String... suffixes)
      Removes all occurrences of the specified suffixes from the end of a string. The returned string will no longer end with any of the specified suffixes.
      Parameters:
      input - the string to manipulate
      suffixes - the suffixes to chop off the right of the string
      Returns:
      a String that does not end with any of the specified suffixes
    • rchop

      public static String rchop(Object input, boolean ignoreCase, String... suffixes)
      Removes all occurrences of the specified suffixes from the end of a string. The returned string will no longer end with any of the specified suffixes.
      Parameters:
      input - the string to manipulate
      ignoreCase - whether to ignore case while chopping off suffixes
      suffixes - a String that does not end with any of the specified suffixes
    • firstToUpper

      public static String firstToUpper(Object input)
      Ensures that the first character of the specified string is not a lowercase character.
      Parameters:
      input - the string
      Returns:
      the same string except that the first character is not a lowercase character
    • firstToLower

      public static String firstToLower(Object input)
      Ensures that the first character of the specified string is not an uppercase character.
      Parameters:
      input - the string
      Returns:
      the same string except that the first character is not an uppercase character
    • lpad

      public static String lpad(Object input, int width)
      Left-pads a string to the specified width using the space character (' ').
      Parameters:
      input - an object whose toString() method produces the string to be padded. Null is treated as the empty string.
      width - the total length of the padded string. If the string itself is wider than the specified width, the string is returned without padding.
      Returns:
      the left-padded string
    • lpad

      public static String lpad(Object input, int width, char padChar)
      Left-pads a string to the specified width using the specified padding character.
      Parameters:
      input - an object whose toString() method produces the string to be padded. Null is treated as the empty string.
      width - the total length of the padded string. If the string itself is wider than the specified width, the string is returned without padding.
      padChar - the character used to left-pad the string
      Returns:
      the left-padded string
    • lpad

      public static String lpad(Object input, int width, char padChar, String delimiter)
      Left-pads a string to the specified width using the specified padding character and then appends the specified terminator.
      Parameters:
      input - an object whose toString() method produces the string to be padded. Null is treated as the empty string.
      width - the total length of the padded string. If the string itself is wider than the specified width, the string is returned without padding.
      padChar - the character used to left-pad the string
      delimiter - a delimiter to append to the padded string. Specify null or an empty string to indicate that no delimiter should be appended.
      Returns:
      the left-padded string
      Throws:
      IllegalArgumentException - If terminator is null
    • pad

      public static String pad(Object input, int width)
      Centers (left- and right-pads) a string within the specified width using the space character.
      Parameters:
      input - an object whose toString() method produces the string to be padded. Null is treated as the empty string.
      width - the total length of the padded string. If the string itself is wider than the specified width, the string is printed without padding.
      Returns:
      the left- and right-padded string plus the terminator
    • pad

      public static String pad(Object input, int width, char padChar)
      Centers (left- and right-pads) a string within the specified width using the specified padding character.
      Parameters:
      input - an object whose toString() method produces the string to be padded. Null is treated as the empty string.
      width - the total length of the padded string. If the string itself is wider than the specified width, the string is printed without padding.
      padChar - the character used to left- and right-pad the string.
      Returns:
      the left- and right-padded string plus the terminator
    • pad

      public static String pad(Object input, int width, char padChar, String delimiter)
      Centers (left- and right-pads) a string within the specified width using the specified padding character and then appends the specified delimiter.
      Parameters:
      input - an object whose toString() method produces the string to be padded. Null is treated as the empty string.
      width - the total length of the padded string. If the string itself is wider than the specified width, the string is printed without padding.
      padChar - the character used to left- and right-pad the string.
      delimiter - a delimiter to append to the padded string. Specify null or an empty string to indicate that no delimiter should be appended.
      Returns:
      the left- and right-padded string plus the terminator
    • rpad

      public static String rpad(Object input, int width)
      Right-pads a string to the specified width using the space character (' ').
      Parameters:
      input - an object whose toString() method produces the string to be padded. Null is treated as the empty string.
      width - the total length of the padded string. If the string itself is wider than the specified width, the string is returned without padding.
      Returns:
      the right-padded string
    • rpad

      public static String rpad(Object input, int width, char padChar)
      Right-pads a string to the specified width using the specified padding character.
      Parameters:
      input - an object whose toString() method produces the string to be padded. Null is treated as the empty string.
      width - the total length of the padded string. If the string itself is wider than the specified width, the string is returned without padding.
      padChar - the character used to left-pad the string.
      Returns:
      the right-padded string
    • rpad

      public static String rpad(Object input, int width, char padChar, String suffix)
      Right-pads a string to the specified width using the specified padding character and appends the specified suffix.
      Parameters:
      input - an object whose toString() method produces the string to be padded. Null is treated as the empty string.
      width - the total length of the padded string. If the string itself is wider than the specified width, the string is printed without padding.
      padChar - the character used to right-pad the string.
      suffix - A suffix to append to the padded string.
      Returns:
      the right-padded string
    • ltrim

      public static String ltrim(Object input, String chars)
      Left-trims all characters contained in chars from the specified string. The resulting string will not start with any of the characters contained in chars.
      Parameters:
      input - the String to trim
      chars - the character to trim off the String
      Returns:
      the left-trimmed String or the input string if it did not start with any of the specified characters
    • rtrim

      public static String rtrim(Object input, String chars)
      Right-trims all characters contained in chars from the specified string. The resulting string will not end with any of the characters contained in chars.
      Parameters:
      input - the String to trim
      chars - the character to trim off the String (must not be null or empty)
      Returns:
      the right-trimmed String or the input string if it did not end with any of the specified characters
    • trim

      public static String trim(Object input, String chars)
      Left and right-trims the specified string. The resulting string will neither start nor end with any of the specified characters.
      Parameters:
      input - the String to trim
      chars - the character to trim off the String (must not be null or empty)
      Returns:
      the trimmed String.
    • substr

      public static String substr(String str, int from)
      Substring method that facilitates substring retrieval relative to the end of a string. If from is negative, it is taken relative to the end of the string (-1 being equivalent to str.length()-1).
      Parameters:
      str - the String to extract a substring from
      from - the start index within string (may be negative)
      Returns:
      the substring
    • substr

      public static String substr(String str, int from, int length)
      Substring method that facilitates substring retrieval relative to the end of a string as well as substring retrieval in the opposite direction. If from is negative, it is taken relative to the end of the string (-1 being equivalent to str.length()-1). If length is negative, the substring is taken in the opposite direction. The character at from will then be the last character of the substring.
      Parameters:
      str - the String to extract a substring from. Must not be null.
      from - the start index within string (may be negative)
      length - the desired length of the substring
      Returns:
      the substring
      See Also:
    • indexOf

      public static int indexOf(Object input, String substr, int occurrence)
      Returns the index of the nth occurrence of the specified substring within input. To find the first occurrence of the specified substring, specify 1 for occurrence; to find the second occurrence, specify 2, etc. If input is null, or if there is no nth occurrence of the specified substring, the return value will be -1. You can specify a negative occurrence to search backwards from the end of the string. Specify -1 for the last occurrence of the specified substring; -2 for the last-but-one occurrence, etc.
      Parameters:
      input - the string to search
      substr - the substring to search for (must not be null or empty)
      occurrence - the occurrence number of the substring (1 means: get index of 1st occurrence; -1 means: get index of last occurrence)
      Returns:
      the index of the nth occurrence of the specified substring
    • substringBefore

      public static String substringBefore(Object input, String substr, int occurrence)
      Returns the substring up to, but not including the nth occurrence of the specified substring, or the entire string if there is no nth occurrence of the substring. Returns an empty string if input is null. Specify 1 to find the first occurrence of the substring; 2 to find the second occurrence, etc. Specify -1 to find the first occurrence; -2 to find the last-but-one occurrence, etc.
      Parameters:
      input - the string to search
      substr - the substring to search for
      occurrence - the occurrence number of the substring. Specify 1 for first occurrence; 2 for second occurrence, etc. Specify -1 for last occurrence, -2 for last-but-one occurrence, etc.
      Returns:
      a substring up to (not including) the nth occurrence of the specified substring
    • substringOnTo

      public static String substringOnTo(Object input, String substr, int occurrence)
      Returns the substring up to, and including the nth occurrence of the specified substring, or the entire string if there is no nth occurrence of the substring. Returns an empty string if input is null. Specify 1 to find the first occurrence of the substring; 2 to find the second occurrence, etc. Specify -1 to find the first occurrence; -2 to find the last-but-one occurrence, etc.
      Parameters:
      input - the string to search
      substr - the substring to search for
      occurrence - the occurrence number of the substring. Specify 1 for first occurrence; 2 for second occurrence, etc. Specify -1 for last occurrence, -2 for last-but-one occurrence, etc.
      Returns:
      a substring up to, and including the nth occurrence of the specified substring
    • substrFrom

      public static String substrFrom(Object input, String substr, int occurrence)
      Returns the substring from (and including) the nth occurrence of the specified substring, or the entire string if there is no nth occurrence of the substring. Returns an empty string if input is null. Specify 1 to find the first occurrence of the substring; 2 to find the second occurrence, etc. Specify -1 to find the first occurrence; -2 to find the last-but-one occurrence, etc.
      Parameters:
      input - the string to search
      substr - the substring to search for
      occurrence - the occurrence number of the substring. Specify 1 for first occurrence; 2 for second occurrence, etc. Specify -1 for last occurrence, -2 for last-but-one occurrence, etc.
      Returns:
      a substring from (inclusive) the nth occurrence of the specified substring
    • substrAfter

      public static String substrAfter(Object input, String substr, int occurrence)
      Returns the substring after the nth occurrence of the specified substring, or the entire string if there is no nth occurrence of the substring. Returns an empty string if input is null. Specify 1 to find the first occurrence of the substring; 2 to find the second occurrence, etc. Specify -1 to find the first occurrence; -2 to find the last-but-one occurrence, etc.
      Parameters:
      input - the string to search
      substr - the substring to search for
      occurrence - the occurrence number of the substring. Specify 1 for first occurrence; 2 for second occurrence, etc. Specify -1 for last occurrence, -2 for last-but-one occurrence, etc.
      Returns:
      a substring after (not including) the nth occurrence of the specified substring
    • getLineAndColumn

      public static int[] getLineAndColumn(String str, int index)
      Returns the line number and column number of the character at the specified index, given the system-defined line separator.
      Parameters:
      str - the string to search
      index - the string index to determine the line and column number of
      Returns:
      a two-element array containing the line number and column number of the character at the specified index
    • getLineAndColumn

      public static int[] getLineAndColumn(String str, int index, String lineSep)
      Returns the line number and column number of the character at the specified index, given the specified line separator.
      Parameters:
      str - the string to search
      index - the string index to determine the line and column number of
      lineSep - the line separator
      Returns:
      a two-element array containing the line number and column number of the character at the specified index