Package com.gengoai.string
Class Strings
- java.lang.Object
-
- com.gengoai.string.Strings
-
public final class Strings extends Object
Convenience methods for manipulating strings.
- Author:
- David B. Bracewell
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static String
abbreviate(CharSequence input, int length)
Abbreviates a string to a desired length and adds "..." at the end.static String
appendIfNotPresent(@NonNull String string, @NonNull String suffix)
Append if not present string.static String
center(String s, int length)
Centers an input string inside a string of given lengthstatic int
count(String str, String target)
Counts the number target strings in the given stringstatic String
emptyToNull(String input)
Converts empty/bank strings to nullstatic String
escape(String input, char escapeCharacter, @NonNull String reservedCharacters)
Escapes a string by placing an escape character in front of reserved characters.static String
escape(String input, @NonNull String escapeMarker, @NonNull IntPredicate escapeChecker, @NonNull IntFunction<String> conversionFunction)
Escapes and converts characters matched using the givenescapeChecker
predicate by prepending the transformed value, determines using the givenconversionFunction
, with theescapeMarker
.static String
escapeUnicode(String string)
Escapes the unicode in the given string using the Java specificationstatic Span
expand(String txt, int start, int end)
Expand int pair.static Iterator<IntPair>
findIterator(@NonNull String input, @NonNull String target)
static String
firstMatch(@NonNull Pattern pattern, @NonNull String input)
static String
firstMatch(@NonNull Pattern pattern, @NonNull String input, int group)
static String
firstMatch(@NonNull Pattern pattern, @NonNull String input, @NonNull String group)
static boolean
hasDigit(CharSequence string)
Determines if a string has at least one digitstatic boolean
hasLetter(CharSequence string)
Determines if a string has at least one letterstatic boolean
hasPunctuation(CharSequence string)
Determines if a given string has one or more punctuation characters.static boolean
isAlphaNumeric(CharSequence string)
Determines if a string is only made up of letters or digitsstatic boolean
isDigit(CharSequence string)
Determines if a string is only made up of numbers.static boolean
isLetter(CharSequence string)
Determines if a string is only made up of letters.static boolean
isLowerCase(CharSequence input)
Determines if an entire string is lower case or notstatic boolean
isNonAlphaNumeric(CharSequence string)
Determines if a string is only made up of non letters and digitsstatic boolean
isNotNullOrBlank(CharSequence input)
Determines if a string is not null or blank (trimmed string is empty).static boolean
isNullOrBlank(CharSequence input)
Determines if a string is null or blank (trimmed string is empty).static boolean
isPunctuation(CharSequence string)
Determines if a given string is only made up of punctuation characters.static boolean
isTitleCase(@NonNull CharSequence input)
Determines if an entire string is title case or notstatic boolean
isUpperCase(CharSequence input)
Determines if an entire string is upper case or notstatic String
join(@NonNull Iterable<?> iterable, @NonNull CharSequence delimiter)
Joins the items in the given iterable into a string separated using the given delimiter.static String
join(@NonNull Iterable<?> iterable, @NonNull CharSequence delimiter, @NonNull CharSequence prefix, @NonNull CharSequence suffix)
Joins the items in the given iterable into a string separated using the given delimiter, with the given prefix at the beginning, and the given suffix at the end of the string.static <T> String
join(@NonNull T[] values, @NonNull CharSequence delimiter)
Joins the items in the given array into a string separated using the given delimiterstatic <T> String
join(@NonNull T[] values, @NonNull CharSequence delimiter, @NonNull CharSequence prefix, @NonNull CharSequence suffix)
Joins the items in the given array into a string separated using the given delimiter, with the given prefix at the beginning, and the given suffix at the end of the string.static void
main(String[] args)
static Iterator<IntPair>
matchIterator(@NonNull String input, @NonNull Pattern pattern)
static String
nullToEmpty(String input)
Converts null values into an empty stringstatic String
padEnd(CharSequence sequence, int desiredLength, char paddingCharacter)
Pads the end of the string to make the string into the desired length using the given padding character to make up the additional length.static String
padStart(CharSequence sequence, int desiredLength, char paddingCharacter)
Pads the beginning of the string to make the string into the desired length using the given padding character to make up the additional length.static String
prependIfNotPresent(@NonNull String string, @NonNull String prefix)
Prepend if not present string.static String
randomHexString(int length)
Generates a random string of given length made up of valid hexadecimal characters.static String
randomString(int length, int min, int max)
Generates a random string of a given lengthstatic String
randomString(int length, int min, int max, CharMatcher validChar)
Generates a random string of a given lengthstatic String
randomString(int length, CharMatcher validChar)
Generates a random string of a given lengthstatic String
removeDiacritics(CharSequence input)
Normalizes a string by removing the diacritics.static String
removeRepeatedChars(CharSequence sequence)
Replaces repeated characters with a single instance.static String
repeat(char c, int count)
Repeats the given character count timesstatic String
repeat(String s, int count)
Repeats the given string count timesstatic boolean
safeEquals(String s1, String s2, boolean caseSensitive)
Safe equals of two strings taking null into consideration.static List<String>
split(CharSequence input, char separator)
Properly splits a delimited separated string usingCSV
assuming default values for the CSV object except for the delimiter.static String
toCanonicalForm(CharSequence input)
Normalize to canonical form.static String
toTitleCase(CharSequence input)
Converts an input string to title casestatic String
unescape(String input, char escapeCharacter)
Unescapes a string which is escaped with the given escaped character.
-
-
-
Field Detail
-
BLANK
public static final String BLANK
Single blank string- See Also:
- Constant Field Values
-
EMPTY
public static final String EMPTY
Empty String- See Also:
- Constant Field Values
-
-
Method Detail
-
abbreviate
public static String abbreviate(CharSequence input, int length)
Abbreviates a string to a desired length and adds "..." at the end.
- Parameters:
input
- The input stringlength
- The length of the abbreviation- Returns:
- The abbreviated string
-
appendIfNotPresent
public static String appendIfNotPresent(@NonNull @NonNull String string, @NonNull @NonNull String suffix)
Append if not present string.- Parameters:
string
- the stringsuffix
- the suffix- Returns:
- the string
-
center
public static String center(String s, int length)
Centers an input string inside a string of given length- Parameters:
s
- the string to centerlength
- the length of the new string- Returns:
- the centered string
-
count
public static int count(String str, String target)
Counts the number target strings in the given string- Parameters:
str
- the str to calculate the count ontarget
- the target which we want to count- Returns:
- the number of times target occurs in str
-
emptyToNull
public static String emptyToNull(String input)
Converts empty/bank strings to null- Parameters:
input
- the input string- Returns:
- the output string (null if input null or blank, input otherwise)
-
escape
public static String escape(String input, char escapeCharacter, @NonNull @NonNull String reservedCharacters)
Escapes a string by placing an escape character in front of reserved characters.- Parameters:
input
- the inputescapeCharacter
- the escape characterreservedCharacters
- the characters needing to be escaped- Returns:
- the string
-
escape
public static String escape(String input, @NonNull @NonNull String escapeMarker, @NonNull @NonNull IntPredicate escapeChecker, @NonNull @NonNull IntFunction<String> conversionFunction)
Escapes and converts characters matched using the givenescapeChecker
predicate by prepending the transformed value, determines using the givenconversionFunction
, with theescapeMarker
.escape("MY @ time is great.", "\\x", c -> !Character.isLetter(c) && !Character.isWhitespace(c), c -> Integer::toHexString);
MY \x40 time is great\x2e
- Parameters:
input
- the input string to escapeescapeMarker
- the escape string to useescapeChecker
- the predicate to check if a character needs to be escapedconversionFunction
- the function to convert the character needing escaped to a string.- Returns:
- the escaped string
-
escapeUnicode
public static String escapeUnicode(String string)
Escapes the unicode in the given string using the Java specification- Parameters:
string
- The string to escape- Returns:
- The escaped string
-
expand
public static Span expand(String txt, int start, int end)
Expand int pair.- Parameters:
txt
- the txtstart
- the startend
- the end- Returns:
- the int pair
-
findIterator
public static Iterator<IntPair> findIterator(@NonNull @NonNull String input, @NonNull @NonNull String target)
-
firstMatch
public static String firstMatch(@NonNull @NonNull Pattern pattern, @NonNull @NonNull String input, int group)
-
firstMatch
public static String firstMatch(@NonNull @NonNull Pattern pattern, @NonNull @NonNull String input, @NonNull @NonNull String group)
-
firstMatch
public static String firstMatch(@NonNull @NonNull Pattern pattern, @NonNull @NonNull String input)
-
hasDigit
public static boolean hasDigit(CharSequence string)
Determines if a string has at least one digit- Parameters:
string
- the string to check- Returns:
- True if the string has at least one digit
-
hasLetter
public static boolean hasLetter(CharSequence string)
Determines if a string has at least one letter- Parameters:
string
- the string to check- Returns:
- True if the string has at least one letter
-
hasPunctuation
public static boolean hasPunctuation(CharSequence string)
Determines if a given string has one or more punctuation characters.- Parameters:
string
- the string to check- Returns:
- True if the string has one or more punctuation characters
-
isAlphaNumeric
public static boolean isAlphaNumeric(CharSequence string)
Determines if a string is only made up of letters or digits- Parameters:
string
- the string to check- Returns:
- True if the string is only made up of letter or digits
-
isDigit
public static boolean isDigit(CharSequence string)
Determines if a string is only made up of numbers.- Parameters:
string
- the string to check- Returns:
- True if the string is only made up of numbers.
-
isLetter
public static boolean isLetter(CharSequence string)
Determines if a string is only made up of letters.- Parameters:
string
- the string to check- Returns:
- True if the string is only made up of letters.
-
isLowerCase
public static boolean isLowerCase(CharSequence input)
Determines if an entire string is lower case or not- Parameters:
input
- The input string- Returns:
- True if the string is lower case, False if not
-
isNonAlphaNumeric
public static boolean isNonAlphaNumeric(CharSequence string)
Determines if a string is only made up of non letters and digits- Parameters:
string
- the string to check- Returns:
- True if the string is only made up of non letters and digits
-
isNotNullOrBlank
public static boolean isNotNullOrBlank(CharSequence input)
Determines if a string is not null or blank (trimmed string is empty).- Parameters:
input
- The input string- Returns:
- True when the input string is not null and the trimmed version of the string is not empty.
-
isNullOrBlank
public static boolean isNullOrBlank(CharSequence input)
Determines if a string is null or blank (trimmed string is empty).- Parameters:
input
- The input string- Returns:
- True when the input string is null or the trimmed version of the string is empty.
-
isPunctuation
public static boolean isPunctuation(CharSequence string)
Determines if a given string is only made up of punctuation characters.- Parameters:
string
- the string to check- Returns:
- True if the string is all punctuation
-
isTitleCase
public static boolean isTitleCase(@NonNull @NonNull CharSequence input)
Determines if an entire string is title case or not- Parameters:
input
- The input string- Returns:
- True if the string is title case, False if not
-
isUpperCase
public static boolean isUpperCase(CharSequence input)
Determines if an entire string is upper case or not- Parameters:
input
- The input string- Returns:
- True if the string is upper case, False if not
-
join
public static String join(@NonNull @NonNull Iterable<?> iterable, @NonNull @NonNull CharSequence delimiter, @NonNull @NonNull CharSequence prefix, @NonNull @NonNull CharSequence suffix)
Joins the items in the given iterable into a string separated using the given delimiter, with the given prefix at the beginning, and the given suffix at the end of the string.- Parameters:
iterable
- the iterabledelimiter
- the delimiterprefix
- the prefixsuffix
- the suffix- Returns:
- the string
-
join
public static String join(@NonNull @NonNull Iterable<?> iterable, @NonNull @NonNull CharSequence delimiter)
Joins the items in the given iterable into a string separated using the given delimiter.- Parameters:
iterable
- the iterabledelimiter
- the delimiter- Returns:
- the string
-
join
public static <T> String join(@NonNull @NonNull T[] values, @NonNull @NonNull CharSequence delimiter)
Joins the items in the given array into a string separated using the given delimiter- Type Parameters:
T
- the type parameter- Parameters:
values
- the valuesdelimiter
- the delimiter- Returns:
- the string
-
join
public static <T> String join(@NonNull @NonNull T[] values, @NonNull @NonNull CharSequence delimiter, @NonNull @NonNull CharSequence prefix, @NonNull @NonNull CharSequence suffix)
Joins the items in the given array into a string separated using the given delimiter, with the given prefix at the beginning, and the given suffix at the end of the string.- Type Parameters:
T
- the type parameter- Parameters:
values
- the valuesdelimiter
- the delimiterprefix
- the prefixsuffix
- the suffix- Returns:
- the string
-
main
public static void main(String[] args)
-
matchIterator
public static Iterator<IntPair> matchIterator(@NonNull @NonNull String input, @NonNull @NonNull Pattern pattern)
-
nullToEmpty
public static String nullToEmpty(String input)
Converts null values into an empty string- Parameters:
input
- the input string- Returns:
- the output string (empty if input null, input otherwise)
-
padEnd
public static String padEnd(CharSequence sequence, int desiredLength, char paddingCharacter)
Pads the end of the string to make the string into the desired length using the given padding character to make up the additional length.- Parameters:
sequence
- the sequencedesiredLength
- the desired lengthpaddingCharacter
- the padding character- Returns:
- the string
-
padStart
public static String padStart(CharSequence sequence, int desiredLength, char paddingCharacter)
Pads the beginning of the string to make the string into the desired length using the given padding character to make up the additional length.- Parameters:
sequence
- the sequencedesiredLength
- the desired lengthpaddingCharacter
- the padding character- Returns:
- the string
-
prependIfNotPresent
public static String prependIfNotPresent(@NonNull @NonNull String string, @NonNull @NonNull String prefix)
Prepend if not present string.- Parameters:
string
- the stringprefix
- the prefix- Returns:
- the string
-
randomHexString
public static String randomHexString(int length)
Generates a random string of given length made up of valid hexadecimal characters.- Parameters:
length
- the length of the string- Returns:
- the random string
-
randomString
public static String randomString(int length, int min, int max)
Generates a random string of a given length- Parameters:
length
- The length of the stringmin
- The min character in the stringmax
- The max character in the string- Returns:
- A string of random characters
-
randomString
public static String randomString(int length, CharMatcher validChar)
Generates a random string of a given length- Parameters:
length
- The length of the stringvalidChar
- CharPredicate that must match for a character to be returned in the string- Returns:
- A string of random characters
-
randomString
public static String randomString(int length, int min, int max, CharMatcher validChar)
Generates a random string of a given length- Parameters:
length
- The length of the stringmin
- The min character in the stringmax
- The max character in the stringvalidChar
- CharPredicate that must match for a character to be returned in the string- Returns:
- A string of random characters
-
removeDiacritics
public static String removeDiacritics(CharSequence input)
Normalizes a string by removing the diacritics.- Parameters:
input
- the input string- Returns:
- Resulting string without diacritic marks
-
removeRepeatedChars
public static String removeRepeatedChars(CharSequence sequence)
Replaces repeated characters with a single instance. e.g.
Gooooood
would becomeGod
.- Parameters:
sequence
- The character sequence- Returns:
- The compacted string
- Throws:
NullPointerException
- when the sequence is null
-
repeat
public static String repeat(String s, int count)
Repeats the given string count times- Parameters:
s
- the string to repeatcount
- the count- Returns:
- the string
-
repeat
public static String repeat(char c, int count)
Repeats the given character count times- Parameters:
c
- the character to repeatcount
- the count- Returns:
- the string
-
safeEquals
public static boolean safeEquals(String s1, String s2, boolean caseSensitive)
Safe equals of two strings taking null into consideration.- Parameters:
s1
- the first strings2
- the second stringcaseSensitive
- True if equals is case sensitive, False if case insensitive.- Returns:
- the boolean
-
split
public static List<String> split(CharSequence input, char separator)
Properly splits a delimited separated string usingCSV
assuming default values for the CSV object except for the delimiter.- Parameters:
input
- The input stringseparator
- The separator- Returns:
- A list of all the cells in the input
-
toCanonicalForm
public static String toCanonicalForm(CharSequence input)
Normalize to canonical form.- Parameters:
input
- the input string- Returns:
- the normalized string
-
toTitleCase
public static String toTitleCase(CharSequence input)
Converts an input string to title case- Parameters:
input
- The input string- Returns:
- The title cased version of the input
-
-