Class StringUtil

java.lang.Object
de.tomatengames.util.StringUtil

public class StringUtil extends Object
Provides methods to handle Strings.
Since:
1.0
  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    Returns if the string contains the specified substring.
    static int
    count(String str, char ch)
    Counts the amount of characters in the string that are equal to the specified character.
    static int
    count(String str, IntPredicate predicate)
    Counts the amount of characters in the string that match the predicate.
    static int
    countFront(String str, char ch)
    Counts the amount of characters at the beginning of the string that are equal to the specified character.
    static int
    countFront(String str, IntPredicate predicate)
    Counts the amount of characters at the beginning of the string that match the predicate.
    static boolean
    Returns if the string ends with the specified suffix.
    static int
    indexOf(String input, int offset, int length, String pattern, int patternOffset, int patternLength)
    Returns the starting index of the first occurrence of the specified part of the pattern in the specified part of the input string.
    static boolean
    isEqualCT(String expected, String actual)
    Performs a near-constant-time comparison for the specified Strings.
    static <T> String
    join(Iterable<T> parts, Function<T,String> conversion, String delimiter)
    Joins the objects from the given iterable into a single string using a specified delimiter and conversion function.
    static <T> String
    join(Iterable<T> parts, Function<T,String> conversion, String delimiter, String lastDelimiter)
    Joins the objects from the given iterable into a single string using specified delimiters and conversion function.
    static <T> String
    join(Iterator<T> parts, Function<T,String> conversion, String delimiter)
    Joins the elements from the given iterator into a single string using the specified delimiter and conversion function.
    static <T> String
    join(Iterator<T> parts, Function<T,String> conversion, String delimiter, String lastDelimiter)
    Joins the elements from the given iterator into a single string using the specified delimiters and conversion function.
    static <T> String
    join(T[] parts, Function<T,String> conversion, String delimiter)
    Joins an array of objects into a single string using a specified delimiter and conversion function.
    static <T> String
    join(T[] parts, Function<T,String> conversion, String delimiter, String lastDelimiter)
    Joins an array of objects into a single string using specified delimiters and a conversion function.
    static int
    lastIndexOf(String input, int offset, int length, String pattern, int patternOffset, int patternLength)
    Returns the starting index of the last occurrence of the specified part of the pattern in the specified part of the input string.
    static boolean
    Tests if the specified input string matches the specified simple pattern.
    static boolean
    Returns if the string starts with the specified prefix.
    static boolean
    startsWithIgnoreCase(String str, String prefix, int offset)
    Returns if the string starts with the specified prefix at the specified index.
    static String
    trimFront(String str, char ch)
    Removes the first characters from the specified string that are equal to the specified character.
    static String
    trimFront(String str, char ch, int maxRemove)
    Removes the first characters from the specified string that are equal to the specified character.
    static String
    trimFront(String str, IntPredicate predicate)
    Removes the first characters from the specified string that match the predicate.
    static String
    trimFront(String str, IntPredicate predicate, int maxRemove)
    Removes the first characters from the specified string that match the predicate.

    Methods inherited from class Object

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

    • countFront

      public static int countFront(String str, IntPredicate predicate)
      Counts the amount of characters at the beginning of the string that match the predicate.
      Parameters:
      str - The string.
      predicate - The predicate.
      Returns:
      The amount of characters.
    • countFront

      public static int countFront(String str, char ch)
      Counts the amount of characters at the beginning of the string that are equal to the specified character.
      Parameters:
      str - The string.
      ch - The character that should be counted.
      Returns:
      The amount of characters.
    • trimFront

      public static String trimFront(String str, IntPredicate predicate)
      Removes the first characters from the specified string that match the predicate.
      Parameters:
      str - The string.
      predicate - The predicate.
      Returns:
      The trimmed string.
    • trimFront

      public static String trimFront(String str, char ch)
      Removes the first characters from the specified string that are equal to the specified character.
      Parameters:
      str - The string.
      ch - The character that should be removed from the string.
      Returns:
      The trimmed string.
    • trimFront

      public static String trimFront(String str, IntPredicate predicate, int maxRemove)
      Removes the first characters from the specified string that match the predicate. If the string starts with more than maxRemove characters that match the predicate, only the first maxRemove characters are removed.
      Parameters:
      str - The string.
      predicate - The predicate.
      maxRemove - The maximum number of characters that can be removed.
      Returns:
      The trimmed string.
    • trimFront

      public static String trimFront(String str, char ch, int maxRemove)
      Removes the first characters from the specified string that are equal to the specified character. If the string starts with more than maxRemove characters that are equal to the specified character, only the first maxRemove characters are removed.
      Parameters:
      str - The string.
      ch - The character that should be removed from the string.
      maxRemove - The maximum number of characters that can be removed.
      Returns:
      The trimmed string.
    • count

      public static int count(String str, IntPredicate predicate)
      Counts the amount of characters in the string that match the predicate.
      Parameters:
      str - The string.
      predicate - The predicate.
      Returns:
      The amount of characters.
    • count

      public static int count(String str, char ch)
      Counts the amount of characters in the string that are equal to the specified character.
      Parameters:
      str - The string.
      ch - The character that should be counted.
      Returns:
      The amount of characters equal to the specified character.
    • startsWithIgnoreCase

      public static boolean startsWithIgnoreCase(String str, String prefix, int offset)
      Returns if the string starts with the specified prefix at the specified index. The case of both strings is ignored.

      The string null does not start with any prefix. No string starts with the prefix null. In particular, the string null does not start with the prefix null.

      Parameters:
      str - The string. May be null.
      prefix - The prefix. May be null.
      offset - The index where the prefix should start in the specified string.
      Returns:
      If the string starts with the specified prefix at the specified index. If the specified offset is out of range, false is returned.
      Since:
      1.1
    • startsWithIgnoreCase

      public static boolean startsWithIgnoreCase(String str, String prefix)
      Returns if the string starts with the specified prefix. The case of both strings is ignored.

      The string null does not start with any prefix. No string starts with the prefix null. In particular, the string null does not start with the prefix null.

      Parameters:
      str - The string. May be null.
      prefix - The prefix. May be null.
      Returns:
      If the string starts with the specified prefix.
      Since:
      1.1
    • endsWithIgnoreCase

      public static boolean endsWithIgnoreCase(String str, String suffix)
      Returns if the string ends with the specified suffix. The case of both strings is ignored.

      The string null does not start with any suffix. No string ends with the suffix null. In particular, the string null does not end with the suffix null.

      Parameters:
      str - The string. May be null.
      suffix - The suffix. May be null.
      Returns:
      If the string ends with the specified suffix.
      Since:
      1.1
    • containsIgnoreCase

      public static boolean containsIgnoreCase(String str, String substr)
      Returns if the string contains the specified substring. The strings are compared case-insensitive.

      The string null does not contain any substring. No string contains the substring null. In particular, the string null does not contain the substring null.

      Parameters:
      str - The string. May be null.
      substr - The substring. May be null.
      Returns:
      If the string contains the specified substring.
      Since:
      1.1
    • isEqualCT

      public static boolean isEqualCT(String expected, String actual)
      Performs a near-constant-time comparison for the specified Strings.

      The calculation time depends mostly on the length of actual. In some situations it may depend slightly on the length of expected. It does not depend on the contents of both Strings.

      Parameters:
      expected - The expected String. Not null.
      actual - The String that should be tested to match the expected String. Not null.
      Returns:
      If both Strings are equal.
      Since:
      1.4
    • matchesSimplePattern

      public static boolean matchesSimplePattern(String input, String pattern)
      Tests if the specified input string matches the specified simple pattern. The pattern may contain '*' to match any substring at its place.
      Parameters:
      input - the input to test whether it matches the pattern
      pattern - the pattern
      Returns:
      whether the input matches the pattern
      Since:
      1.7
    • join

      public static <T> String join(T[] parts, Function<T,String> conversion, String delimiter)
      Joins an array of objects into a single string using a specified delimiter and conversion function.

      The method converts each element in the array to strings using the given conversion function, then concatenates these strings with the provided delimiter.

      Type Parameters:
      T - The type of elements in the array.
      Parameters:
      parts - The array containing the elements to join.
      conversion - The function that converts each element to a String.
      delimiter - The string used to separate the converted parts.
      Returns:
      A single string obtained by joining all elements after converting and separating them with the specified delimiter. Returns an empty string if the input array is null or has no elements.
      Since:
      1.8
    • join

      public static <T> String join(T[] parts, Function<T,String> conversion, String delimiter, String lastDelimiter)
      Joins an array of objects into a single string using specified delimiters and a conversion function.

      This method converts each element in the array to strings using the given conversion function, then concatenates these strings with the provided delimiter. It allows to specify a different delimiter for the last element.

      Type Parameters:
      T - The type of elements in the part array.
      Parameters:
      parts - The array containing the elements to join.
      conversion - The function that converts each element to a String.
      delimiter - The string used to separate all but the last converted part.
      lastDelimiter - The string used before the last converted part.
      Returns:
      A single string obtained by joining all elements after converting and separating them with the specified delimiters. Returns an empty string if the input array is null or has no elements.
      Since:
      1.8
    • join

      public static <T> String join(Iterator<T> parts, Function<T,String> conversion, String delimiter)
      Joins the elements from the given iterator into a single string using the specified delimiter and conversion function.

      The method converts each element from the iterator to strings using the given conversion function, then concatenates these strings with the provided delimiter.

      Type Parameters:
      T - The type of elements in the part array.
      Parameters:
      parts - The iterator containing the elements to join.
      conversion - The function that converts each element to a String.
      delimiter - The string used to separate the converted parts.
      Returns:
      A single string obtained by joining all elements after converting and separating them with the specified delimiter. Returns an empty string if the iterator is null or has no elements.
      Since:
      1.8
    • join

      public static <T> String join(Iterator<T> parts, Function<T,String> conversion, String delimiter, String lastDelimiter)
      Joins the elements from the given iterator into a single string using the specified delimiters and conversion function.

      This method converts each element from the iterator to strings using the given conversion function, then concatenates these strings with the provided delimiter. It allows to specify a different delimiter for the last element.

      Type Parameters:
      T - The type of elements in the part array.
      Parameters:
      parts - The iterator containing the elements to join.
      conversion - The function that converts each element to a String.
      delimiter - The string used to separate all but the last converted part.
      lastDelimiter - A string used before the last converted part.
      Returns:
      A single string obtained by joining all elements after converting and separating them with the specified delimiters. Returns an empty string if the iterator is null or has no elements.
      Since:
      1.8
    • join

      public static <T> String join(Iterable<T> parts, Function<T,String> conversion, String delimiter)
      Joins the objects from the given iterable into a single string using a specified delimiter and conversion function.

      This method converts each element in the iterable to strings using the given conversion function, then concatenates these strings with the specified delimiter.

      Type Parameters:
      T - The type of elements in the array.
      Parameters:
      parts - The iterable containing the elements to join.
      conversion - The function that converts each element to a String.
      delimiter - The string used to separate consecutive converted elements.
      Returns:
      A single string obtained by joining all elements after converting them and separating with the specified delimiter. Returns an empty string if the iterable is null or has no elements.
      Since:
      1.8
    • join

      public static <T> String join(Iterable<T> parts, Function<T,String> conversion, String delimiter, String lastDelimiter)
      Joins the objects from the given iterable into a single string using specified delimiters and conversion function.

      This method converts each element in the iterable to strings using the given conversion function. It concatenates these strings with the specified delimiter for all elements except before the last, where it uses the specified last delimiter.

      Type Parameters:
      T - The type of elements in the iterable.
      Parameters:
      parts - The iterable containing the elements to join.
      conversion - The function that converts each element to a String.
      delimiter - The string used to separate consecutive converted elements.
      lastDelimiter - The string used before the last converted element.
      Returns:
      A single string obtained by joining all elements after converting them and separating with the specified delimiters. Returns an empty string if the iterable is null or has no elements.
      Since:
      1.8
    • indexOf

      public static int indexOf(String input, int offset, int length, String pattern, int patternOffset, int patternLength)
      Returns the starting index of the first occurrence of the specified part of the pattern in the specified part of the input string. The pattern is limited to the substring starting at patternOffset with the length patternLength.
      Parameters:
      input - The input string. Not null.
      offset - The index where to start searching for the pattern. Not negative.
      length - The length of the input to search for the pattern. Not negative. offset+length must not be greater than the length of the input string.
      pattern - The pattern to search for. Not null.
      patternOffset - The beginning index of the pattern. Not negative.
      patternLength - The length of the part of the pattern to search for. Not negative. patternOffset+patternLength must not be greater than the length of the pattern string.
      Returns:
      The index of the first occurrence of the part of the pattern in the part of the input string, or -1 if no such index is found.
      Since:
      1.8
    • lastIndexOf

      public static int lastIndexOf(String input, int offset, int length, String pattern, int patternOffset, int patternLength)
      Returns the starting index of the last occurrence of the specified part of the pattern in the specified part of the input string. The pattern is limited to the substring starting at patternOffset with the length patternLength.
      Parameters:
      input - The input string. Not null.
      offset - The index where to start searching for the pattern. Not negative.
      length - The length of the input to search for the pattern. Not negative. offset+length must not be greater than the length of the input string.
      pattern - The pattern to search for. Not null.
      patternOffset - The beginning index of the pattern. Not negative.
      patternLength - The length of the part of the pattern to search for. Not negative. patternOffset+patternLength must not be greater than the length of the pattern string.
      Returns:
      The index of the last occurrence of the part of the pattern in the part of the input string, or -1 if no such index is found.
      Since:
      1.8