Class StringUtil
-
Method Summary
Modifier and TypeMethodDescriptionstatic booleancontainsIgnoreCase(String str, String substr) Returns if the string contains the specified substring.static intCounts the amount of characters in the string that are equal to the specified character.static intcount(String str, IntPredicate predicate) Counts the amount of characters in the string that match the predicate.static intcountFront(String str, char ch) Counts the amount of characters at the beginning of the string that are equal to the specified character.static intcountFront(String str, IntPredicate predicate) Counts the amount of characters at the beginning of the string that match the predicate.static booleanendsWithIgnoreCase(String str, String suffix) Returns if the string ends with the specified suffix.static intReturns the starting index of the first occurrence of the specified part of the pattern in the specified part of the input string.static booleanPerforms a near-constant-time comparison for the specified Strings.static <T> StringJoins the objects from the given iterable into a single string using a specified delimiter and conversion function.static <T> StringJoins the objects from the given iterable into a single string using specified delimiters and conversion function.static <T> StringJoins the elements from the given iterator into a single string using the specified delimiter and conversion function.static <T> StringJoins the elements from the given iterator into a single string using the specified delimiters and conversion function.static <T> StringJoins an array of objects into a single string using a specified delimiter and conversion function.static <T> StringJoins an array of objects into a single string using specified delimiters and a conversion function.static intlastIndexOf(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 booleanmatchesSimplePattern(String input, String pattern) Tests if the specified input string matches the specified simple pattern.static booleanstartsWithIgnoreCase(String str, String prefix) Returns if the string starts with the specified prefix.static booleanstartsWithIgnoreCase(String str, String prefix, int offset) Returns if the string starts with the specified prefix at the specified index.static StringRemoves the first characters from the specified string that are equal to the specified character.static StringRemoves the first characters from the specified string that are equal to the specified character.static StringtrimFront(String str, IntPredicate predicate) Removes the first characters from the specified string that match the predicate.static StringtrimFront(String str, IntPredicate predicate, int maxRemove) Removes the first characters from the specified string that match the predicate.
-
Method Details
-
countFront
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
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
Removes the first characters from the specified string that match the predicate.- Parameters:
str- The string.predicate- The predicate.- Returns:
- The trimmed string.
-
trimFront
-
trimFront
Removes the first characters from the specified string that match the predicate. If the string starts with more thanmaxRemovecharacters that match the predicate, only the firstmaxRemovecharacters are removed.- Parameters:
str- The string.predicate- The predicate.maxRemove- The maximum number of characters that can be removed.- Returns:
- The trimmed string.
-
trimFront
Removes the first characters from the specified string that are equal to the specified character. If the string starts with more thanmaxRemovecharacters that are equal to the specified character, only the firstmaxRemovecharacters 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
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
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
Returns if the string starts with the specified prefix at the specified index. The case of both strings is ignored.The string
nulldoes not start with any prefix. No string starts with the prefixnull. In particular, the stringnulldoes not start with the prefixnull.- Parameters:
str- The string. May benull.prefix- The prefix. May benull.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,
falseis returned. - Since:
- 1.1
-
startsWithIgnoreCase
Returns if the string starts with the specified prefix. The case of both strings is ignored.The string
nulldoes not start with any prefix. No string starts with the prefixnull. In particular, the stringnulldoes not start with the prefixnull.- Parameters:
str- The string. May benull.prefix- The prefix. May benull.- Returns:
- If the string starts with the specified prefix.
- Since:
- 1.1
-
endsWithIgnoreCase
Returns if the string ends with the specified suffix. The case of both strings is ignored.The string
nulldoes not start with any suffix. No string ends with the suffixnull. In particular, the stringnulldoes not end with the suffixnull.- Parameters:
str- The string. May benull.suffix- The suffix. May benull.- Returns:
- If the string ends with the specified suffix.
- Since:
- 1.1
-
containsIgnoreCase
Returns if the string contains the specified substring. The strings are compared case-insensitive.The string
nulldoes not contain any substring. No string contains the substringnull. In particular, the stringnulldoes not contain the substringnull.- Parameters:
str- The string. May benull.substr- The substring. May benull.- Returns:
- If the string contains the specified substring.
- Since:
- 1.1
-
isEqualCT
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 ofexpected. It does not depend on the contents of both Strings.- Parameters:
expected- The expected String. Notnull.actual- The String that should be tested to match the expected String. Notnull.- Returns:
- If both Strings are equal.
- Since:
- 1.4
-
matchesSimplePattern
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 patternpattern- the pattern- Returns:
- whether the input matches the pattern
- Since:
- 1.7
-
join
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
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
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 atpatternOffsetwith the lengthpatternLength.- 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+lengthmust 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+patternLengthmust 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
-1if 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 atpatternOffsetwith the lengthpatternLength.- 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+lengthmust 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+patternLengthmust 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
-1if no such index is found. - Since:
- 1.8
-