Package com.github.tadukoo.util
Class StringUtil
java.lang.Object
com.github.tadukoo.util.StringUtil
Util functions for dealing with Strings, including building and parsing them.
- Since:
- Pre-Alpha
- Version:
- Alpha v.0.3.1
- Author:
- Logan Ferree (Tadukoo)
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic String
Builds a string from the given collection of Strings where each is separated by a comma.static String
buildString
(Collection<String> items) Builds a string from the given collection of Strings with no separator between them.static String
buildStringWithNewLines
(Collection<String> lines) Builds a string from the given collection of Strings where each is on a newline.static String
buildStringWithSeparator
(Collection<String> items, String separator) Builds a string from the given collection of Strings with the given separator placed between them.convertCollectionToStrings
(Collection<T> items) Converts an entire Collection of items to strings, and returns them as a List.static String
convertToString
(Object obj) Converts the given Object to a String, including proper null handling.static boolean
Checks if the two strings are equal using String.equals(), but properly handles null (if they're both null, returns true, if one is null and the other isn't, return false instead of throwing an NPE).static boolean
Checks if the given string is equal to any of the given expected strings using String.equals(), but properly handles null (if the string is null and null is in the expected strings, returns true, if the string is null and the expected strings don't have null, return false instead of throwing an NPE).static boolean
equalsAnyIgnoreCase
(String actual, String... expected) Checks if the given string is equal to any of the given expected strings using String.equalsIgnoreCase(), but properly handles null (if the string is null and null is in the expected strings, returns true, if the string is null and the expected strings don't have null, return false instead of throwing an NPE).static boolean
equalsIgnoreCase
(String actual, String expected) Checks if the two strings are equal using String.equalsIgnoreCase(), but properly handles null (if they're both null, returns true, if one is null and the other isn't, return false instead of throwing an NPE).static boolean
Checks if the given string is blank (either null or the empty string).static boolean
isCamelCase
(String text) Checks if the given text is camelCase or not.static boolean
isNotBlank
(String text) Checks if the given string is NOT blank (blank = either null or the empty string).static boolean
isPascalCase
(String text) Checks if the given text is PascalCase or not.static boolean
isSnakeCase
(String text) Checks if the given text is snake_case or not.static boolean
Checks if the two strings are NOT equal using String.equals(), but properly handles null (if they're both null, returns false, if one is null and the other isn't, return true instead of throwing an NPE).static boolean
notEqualsAny
(String actual, String... expected) Checks if the given string is NOT equal to any of the given expected strings using String.equals(), but properly handles null (if the string is null and null is in the expected strings, returns false, if the string is null and the expected strings don't have null, return true instead of throwing an NPE).static boolean
notEqualsAnyIgnoreCase
(String actual, String... expected) Checks if the given string is NOT equal to any of the given expected strings using String.equalsIgnoreCase(), but properly handles null (if the string is null and null is in the expected strings, returns false, if the string is null and the expected strings don't have null, return true instead of throwing an NPE).static boolean
notEqualsIgnoreCase
(String actual, String expected) Checks if the two strings are NOT equal using String.equalsIgnoreCase(), but properly handles null (if they're both null, returns false, if one is null and the other isn't, return true instead of throwing an NPE).Parses a comma-separated list into a List of Strings.parseListFromStringWithSeparator
(String text, String separator, boolean trim) Parses the given text into a List using the given separator to split and optionally trimming to remove whitespace in the resulting strings.static String
toCamelCase
(String text) Converts the given text to camelCase.static String
toPascalCase
(String text) Converts the given text to PascalCase.static String
toSnakeCase
(String text) Converts the given text to snake_case.
-
Constructor Details
-
StringUtil
private StringUtil()Not allowed to create a StringUtil
-
-
Method Details
-
isBlank
Checks if the given string is blank (either null or the empty string).- Parameters:
text
- The string to check- Returns:
- true if the string is null or the empty string
-
isNotBlank
Checks if the given string is NOT blank (blank = either null or the empty string).- Parameters:
text
- The string to check- Returns:
- true if the string is not null and not the empty string
-
equals
Checks if the two strings are equal using String.equals(), but properly handles null (if they're both null, returns true, if one is null and the other isn't, return false instead of throwing an NPE).- Parameters:
actual
- The actual string being checkedexpected
- The expected string we want- Returns:
- true if actual.equals(expected) or they're both null
-
equalsAny
Checks if the given string is equal to any of the given expected strings using String.equals(), but properly handles null (if the string is null and null is in the expected strings, returns true, if the string is null and the expected strings don't have null, return false instead of throwing an NPE).- Parameters:
actual
- The actual string being checkedexpected
- The expected strings we want- Returns:
- true if actual.equals({any of the expected}) or they're both null
-
equalsIgnoreCase
Checks if the two strings are equal using String.equalsIgnoreCase(), but properly handles null (if they're both null, returns true, if one is null and the other isn't, return false instead of throwing an NPE).- Parameters:
actual
- The actual string being checkedexpected
- The expected string we want- Returns:
- true if actual.equalsIgnoreCase(expected) or they're both null
-
equalsAnyIgnoreCase
Checks if the given string is equal to any of the given expected strings using String.equalsIgnoreCase(), but properly handles null (if the string is null and null is in the expected strings, returns true, if the string is null and the expected strings don't have null, return false instead of throwing an NPE).- Parameters:
actual
- The actual string being checkedexpected
- The expected strings we want- Returns:
- true if actual.equalsIgnoreCase({any of the expected}) or they're both null
-
notEquals
Checks if the two strings are NOT equal using String.equals(), but properly handles null (if they're both null, returns false, if one is null and the other isn't, return true instead of throwing an NPE).- Parameters:
actual
- The actual string being checkedexpected
- The expected string we want- Returns:
- false if actual.equals(expected) or they're both null
-
notEqualsAny
Checks if the given string is NOT equal to any of the given expected strings using String.equals(), but properly handles null (if the string is null and null is in the expected strings, returns false, if the string is null and the expected strings don't have null, return true instead of throwing an NPE).- Parameters:
actual
- The actual string being checkedexpected
- The expected strings we want- Returns:
- false if actual.equals({any of the expected}) or they're both null
-
notEqualsIgnoreCase
Checks if the two strings are NOT equal using String.equalsIgnoreCase(), but properly handles null (if they're both null, returns false, if one is null and the other isn't, return true instead of throwing an NPE).- Parameters:
actual
- The actual string being checkedexpected
- The expected string we want- Returns:
- false if actual.equalsIgnoreCase(expected) or they're both null
-
notEqualsAnyIgnoreCase
Checks if the given string is NOT equal to any of the given expected strings using String.equalsIgnoreCase(), but properly handles null (if the string is null and null is in the expected strings, returns false, if the string is null and the expected strings don't have null, return true instead of throwing an NPE).- Parameters:
actual
- The actual string being checkedexpected
- The expected strings we want- Returns:
- false if actual.equalsIgnoreCase({any of the expected}) or they're both null
-
convertToString
Converts the given Object to a String, including proper null handling.- Parameters:
obj
- The Object to convert to a String- Returns:
- null if obj is null, or a String representing the Object
-
convertCollectionToStrings
Converts an entire Collection of items to strings, and returns them as a List.- Type Parameters:
T
- The type of the items in the collection- Parameters:
items
- The Collection of items to convert to strings- Returns:
- A List of the given items converted to Strings
-
buildStringWithSeparator
Builds a string from the given collection of Strings with the given separator placed between them.
e.g. buildStringWithSeparator(Arrays.asList("foo", "bar", "baz"), " space ") would produce:
"foo space bar space baz"- Parameters:
items
- The collection of strings to put togetherseparator
- The character to put in-between the strings- Returns:
- A String of the given collection with the separator between each of them
-
buildString
Builds a string from the given collection of Strings with no separator between them.- Parameters:
items
- The strings to combine into a single String- Returns:
- A String of all the given items
-
buildStringWithNewLines
Builds a string from the given collection of Strings where each is on a newline. UsesbuildStringWithSeparator(java.util.Collection<java.lang.String>, java.lang.String)
to accomplish this.
e.g. buildStringWithNewLines(Arrays.asList("foo", "bar", "baz") would produce:
"foo
bar
baz"- Parameters:
lines
- The lines to make into a single string- Returns:
- A String of the given lines each on a newline
-
buildCommaSeparatedString
Builds a string from the given collection of Strings where each is separated by a comma. UsesbuildStringWithSeparator(java.util.Collection<java.lang.String>, java.lang.String)
to accomplish this.
e.g. buildCommaSeparatedString(Arrays.asList("foo", "bar", "baz") would produce:
"foo,bar,baz"- Parameters:
items
- The items to make into a single string- Returns:
- A String of the given items as a comma-separated string
-
parseListFromStringWithSeparator
public static List<String> parseListFromStringWithSeparator(String text, String separator, boolean trim) Parses the given text into a List using the given separator to split and optionally trimming to remove whitespace in the resulting strings.- Parameters:
text
- The text to parse into a List of Stringsseparator
- The separator String to use in separating the given texttrim
- Whether to trim any whitespace off the resulting strings- Returns:
- The resulting List of Strings
-
parseCommaSeparatedListFromString
Parses a comma-separated list into a List of Strings.- Parameters:
text
- The text to convert into a List of Strings- Returns:
- The List of Strings produced by parsing the text
-
isPascalCase
Checks if the given text is PascalCase or not. PascalCase has no underscores or spaces between words and every word is capitalized, including the first word.- Parameters:
text
- The text to be checked- Returns:
- true if the text is PascalCase, false otherwise
-
toPascalCase
Converts the given text to PascalCase. It removes any spaces and underscores and capitalizes all words in the text.- Parameters:
text
- The text to be converted- Returns:
- The PascalCase version of the given text
-
isCamelCase
Checks if the given text is camelCase or not. camelCase has no underscores or spaces between words and every word is capitalized, excluding the first word.- Parameters:
text
- The text to be checked- Returns:
- true if the text is camelCase, false otherwise
-
toCamelCase
Converts the given text to camelCase. It removes any spaces and underscores and capitalizes all words in the text, except for the first word, which is made lowercase.- Parameters:
text
- The text to be converted- Returns:
- The camelCase version of the given text
-
isSnakeCase
Checks if the given text is snake_case or not. snake_case has an underscore between words, and there are no spaces.- Parameters:
text
- The text to be checked- Returns:
- true is the text is snake_case, false otherwise
-
toSnakeCase
Converts the given text to snake_case. Any spaces are replaced with underscores, underscores are placed between words, and everything is made lowercase.- Parameters:
text
- The text to be converted- Returns:
- The snake_case version of the given text
-