Class StringUtils
- java.lang.Object
-
- yakworks.util.StringUtils
-
public abstract class StringUtils extends java.lang.ObjectMiscellaneousStringutility methods.Mainly for internal use within the framework; consider Apache's Commons Lang for a more comprehensive suite of
Stringutilities.This class delivers some simple functionality that should really be provided by the core Java
StringandStringBuilderclasses. It also provides easy-to-use methods to convert between delimited strings, such as CSV strings, and collections and arrays.- Since:
- 16 April 2001
-
-
Constructor Summary
Constructors Constructor Description StringUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static java.lang.String[]addStringToArray(java.lang.String[] array, java.lang.String str)Append the givenStringto the givenStringarray, returning a new array consisting of the input array contents plus the givenString.static java.lang.StringapplyRelativePath(java.lang.String path, java.lang.String relativePath)Apply the given relative path to the given Java resource path, assuming standard Java folder separation (i.e.static java.lang.StringarrayToCommaDelimitedString(java.lang.Object[] arr)Convert aStringarray into a comma delimitedString(i.e., CSV).static java.lang.StringarrayToDelimitedString(java.lang.Object[] arr, java.lang.String delim)Convert aStringarray into a delimitedString(e.g.static java.lang.Stringcapitalize(java.lang.String str)Capitalize aString, changing the first letter to upper case as perCharacter.toUpperCase(char).static java.lang.StringcleanPath(java.lang.String path)Normalize the path by suppressing sequences like "path/.." and inner simple dots.static java.lang.StringcollectionToCommaDelimitedString(java.util.Collection<?> coll)Convert aCollectioninto a delimitedString(e.g., CSV).static java.lang.StringcollectionToDelimitedString(java.util.Collection<?> coll, java.lang.String delim)Convert aCollectioninto a delimitedString(e.g.static java.lang.StringcollectionToDelimitedString(java.util.Collection<?> coll, java.lang.String delim, java.lang.String prefix, java.lang.String suffix)Convert aCollectionto a delimitedString(e.g.static java.util.Set<java.lang.String>commaDelimitedListToSet(java.lang.String str)Convert a comma delimited list (e.g., a row from a CSV file) into a set.static java.lang.String[]commaDelimitedListToStringArray(java.lang.String str)Convert a comma delimited list (e.g., a row from a CSV file) into an array of strings.static java.lang.String[]concatenateStringArrays(java.lang.String[] array1, java.lang.String[] array2)Concatenate the givenStringarrays into one, with overlapping array elements included twice.static booleancontainsWhitespace(java.lang.CharSequence str)Check whether the givenCharSequencecontains any whitespace characters.static booleancontainsWhitespace(java.lang.String str)Check whether the givenStringcontains any whitespace characters.static intcountOccurrencesOf(java.lang.String str, java.lang.String sub)Count the occurrences of the substringsubin stringstr.static java.lang.Stringdelete(java.lang.String inString, java.lang.String pattern)Delete all occurrences of the given substring.static java.lang.StringdeleteAny(java.lang.String inString, java.lang.String charsToDelete)Delete any character in a givenString.static java.lang.String[]delimitedListToStringArray(java.lang.String str, java.lang.String delimiter)Take aStringthat is a delimited list and convert it into aStringarray.static java.lang.String[]delimitedListToStringArray(java.lang.String str, java.lang.String delimiter, java.lang.String charsToDelete)Take aStringthat is a delimited list and convert it into aStringarray.static booleanendsWithIgnoreCase(java.lang.String str, java.lang.String suffix)Test if the givenStringends with the specified suffix, ignoring upper/lower case.static java.lang.StringgetFilename(java.lang.String path)Extract the filename from the given Java resource path, e.g.static java.lang.StringgetFilenameExtension(java.lang.String path)Extract the filename extension from the given Java resource path, e.g.static booleanhasLength(java.lang.CharSequence str)Check that the givenCharSequenceis neithernullnor of length 0.static booleanhasLength(java.lang.String str)Check that the givenStringis neithernullnor of length 0.static booleanhasText(java.lang.CharSequence str)Check whether the givenCharSequencecontains actual text.static booleanhasText(java.lang.String str)Check whether the givenStringcontains actual text.static booleanisEmpty(java.lang.Object str)Deprecated.static booleanmatchesCharacter(java.lang.String str, char singleCharacter)Test if the givenStringmatches the given single character.static java.lang.String[]mergeStringArrays(java.lang.String[] array1, java.lang.String[] array2)Deprecated.as of 4.3.15, in favor of manual merging viaLinkedHashSet(with every entry included at most once, even entries within the first array)static java.util.LocaleparseLocale(java.lang.String localeValue)Parse the givenStringvalue into aLocale, accepting theLocale.toString()format as well as BCP 47 language tags as specified byLocale.forLanguageTag(java.lang.String).static java.util.LocaleparseLocaleString(java.lang.String localeString)Parse the givenStringrepresentation into aLocale.static java.util.TimeZoneparseTimeZoneString(java.lang.String timeZoneString)Parse the giventimeZoneStringvalue into aTimeZone.static booleanpathEquals(java.lang.String path1, java.lang.String path2)Compare two paths after normalization of them.static java.lang.Stringquote(java.lang.String str)Quote the givenStringwith single quotes.static java.lang.ObjectquoteIfString(java.lang.Object obj)Turn the given Object into aStringwith single quotes if it is aString; keeping the Object as-is else.static java.lang.String[]removeDuplicateStrings(java.lang.String[] array)Remove duplicate strings from the given array.static java.lang.Stringreplace(java.lang.String inString, java.lang.String oldPattern, java.lang.String newPattern)Replace all occurrences of a substring within a string with another string.static java.lang.String[]sortStringArray(java.lang.String[] array)Sort the givenStringarray if necessary.static java.lang.String[]split(java.lang.String toSplit, java.lang.String delimiter)Split aStringat the first occurrence of the delimiter.static java.util.PropertiessplitArrayElementsIntoProperties(java.lang.String[] array, java.lang.String delimiter)Take an array of strings and split each element based on the given delimiter.static java.util.PropertiessplitArrayElementsIntoProperties(java.lang.String[] array, java.lang.String delimiter, java.lang.String charsToDelete)Take an array of strings and split each element based on the given delimiter.static booleanstartsWithIgnoreCase(java.lang.String str, java.lang.String prefix)Test if the givenStringstarts with the specified prefix, ignoring upper/lower case.static java.lang.StringstripFilenameExtension(java.lang.String path)Strip the filename extension from the given Java resource path, e.g.static booleansubstringMatch(java.lang.CharSequence str, int index, java.lang.CharSequence substring)Test whether the given string matches the given substring at the given index.static java.lang.String[]tokenizeToStringArray(java.lang.String str, java.lang.String delimiters)Tokenize the givenStringinto aStringarray via aStringTokenizer.static java.lang.String[]tokenizeToStringArray(java.lang.String str, java.lang.String delimiters, boolean trimTokens, boolean ignoreEmptyTokens)Tokenize the givenStringinto aStringarray via aStringTokenizer.static java.lang.StringtoLanguageTag(java.util.Locale locale)Deprecated.as of 5.0.4, in favor ofLocale.toLanguageTag()static java.lang.String[]toStringArray(java.util.Collection<java.lang.String> collection)Copy the givenCollectioninto aStringarray.static java.lang.String[]toStringArray(java.util.Enumeration<java.lang.String> enumeration)Copy the givenEnumerationinto aStringarray.static java.lang.CharSequencetrimAllWhitespace(java.lang.CharSequence text)Trim all whitespace from the givenCharSequence: leading, trailing, and in between characters.static java.lang.StringtrimAllWhitespace(java.lang.String str)Trim all whitespace from the givenString: leading, trailing, and in between characters.static java.lang.String[]trimArrayElements(java.lang.String[] array)Trim the elements of the givenStringarray, callingString.trim()on each non-null element.static java.lang.StringtrimLeadingCharacter(java.lang.String str, char leadingCharacter)Trim all occurrences of the supplied leading character from the givenString.static java.lang.StringtrimLeadingWhitespace(java.lang.String str)Trim leading whitespace from the givenString.static java.lang.StringtrimTrailingCharacter(java.lang.String str, char trailingCharacter)Trim all occurrences of the supplied trailing character from the givenString.static java.lang.StringtrimTrailingWhitespace(java.lang.String str)Trim trailing whitespace from the givenString.static java.lang.StringtrimWhitespace(java.lang.String str)Trim leading and trailing whitespace from the givenString.static java.lang.Stringuncapitalize(java.lang.String str)Uncapitalize aString, changing the first letter to lower case as perCharacter.toLowerCase(char).static java.lang.Stringunqualify(java.lang.String qualifiedName)Unqualify a string qualified by a '.' dot character.static java.lang.Stringunqualify(java.lang.String qualifiedName, char separator)Unqualify a string qualified by a separator character.static java.lang.StringuriDecode(java.lang.String source, java.nio.charset.Charset charset)Decode the given encoded URI component value.
-
-
-
Method Detail
-
isEmpty
@Deprecated public static boolean isEmpty(@Nullable java.lang.Object str)Deprecated.Check whether the given object (possibly aString) is empty. This is effectively a shortcut for!hasLength(String).This method accepts any Object as an argument, comparing it to
nulland the empty String. As a consequence, this method will never returntruefor a non-null non-String object.The Object signature is useful for general attribute handling code that commonly deals with Strings but generally has to iterate over Objects since attributes may e.g. be primitive value objects as well.
Note: If the object is typed to
Stringupfront, preferhasLength(String)orhasText(String)instead.- Parameters:
str- the candidate object (possibly aString)- Since:
- 3.2.1
-
hasLength
public static boolean hasLength(@Nullable java.lang.CharSequence str)Check that the givenCharSequenceis neithernullnor of length 0.Note: this method returns
truefor aCharSequencethat purely consists of whitespace.StringUtils.hasLength(null) = false StringUtils.hasLength("") = false StringUtils.hasLength(" ") = true StringUtils.hasLength("Hello") = true- Parameters:
str- theCharSequenceto check (may benull)- Returns:
trueif theCharSequenceis notnulland has length- See Also:
hasLength(String),hasText(CharSequence)
-
hasLength
public static boolean hasLength(@Nullable java.lang.String str)Check that the givenStringis neithernullnor of length 0.Note: this method returns
truefor aStringthat purely consists of whitespace.- Parameters:
str- theStringto check (may benull)- Returns:
trueif theStringis notnulland has length- See Also:
hasLength(CharSequence),hasText(String)
-
hasText
public static boolean hasText(@Nullable java.lang.CharSequence str)Check whether the givenCharSequencecontains actual text.More specifically, this method returns
trueif theCharSequenceis notnull, its length is greater than 0, and it contains at least one non-whitespace character.StringUtils.hasText(null) = false StringUtils.hasText("") = false StringUtils.hasText(" ") = false StringUtils.hasText("12345") = true StringUtils.hasText(" 12345 ") = true- Parameters:
str- theCharSequenceto check (may benull)- Returns:
trueif theCharSequenceis notnull, its length is greater than 0, and it does not contain whitespace only- See Also:
hasText(String),hasLength(CharSequence),Character.isWhitespace(char)
-
hasText
public static boolean hasText(@Nullable java.lang.String str)Check whether the givenStringcontains actual text.More specifically, this method returns
trueif theStringis notnull, its length is greater than 0, and it contains at least one non-whitespace character.- Parameters:
str- theStringto check (may benull)- Returns:
trueif theStringis notnull, its length is greater than 0, and it does not contain whitespace only- See Also:
hasText(CharSequence),hasLength(String),Character.isWhitespace(char)
-
containsWhitespace
public static boolean containsWhitespace(@Nullable java.lang.CharSequence str)Check whether the givenCharSequencecontains any whitespace characters.- Parameters:
str- theCharSequenceto check (may benull)- Returns:
trueif theCharSequenceis not empty and contains at least 1 whitespace character- See Also:
Character.isWhitespace(char)
-
containsWhitespace
public static boolean containsWhitespace(@Nullable java.lang.String str)Check whether the givenStringcontains any whitespace characters.- Parameters:
str- theStringto check (may benull)- Returns:
trueif theStringis not empty and contains at least 1 whitespace character- See Also:
containsWhitespace(CharSequence)
-
trimWhitespace
public static java.lang.String trimWhitespace(java.lang.String str)
Trim leading and trailing whitespace from the givenString.- Parameters:
str- theStringto check- Returns:
- the trimmed
String - See Also:
Character.isWhitespace(char)
-
trimAllWhitespace
public static java.lang.CharSequence trimAllWhitespace(java.lang.CharSequence text)
Trim all whitespace from the givenCharSequence: leading, trailing, and in between characters.- Parameters:
text- theCharSequenceto check- Returns:
- the trimmed
CharSequence - Since:
- 5.3.22
- See Also:
trimAllWhitespace(String),Character.isWhitespace(char)
-
trimAllWhitespace
public static java.lang.String trimAllWhitespace(java.lang.String str)
Trim all whitespace from the givenString: leading, trailing, and in between characters.- Parameters:
str- theStringto check- Returns:
- the trimmed
String - See Also:
trimAllWhitespace(CharSequence),Character.isWhitespace(char)
-
trimLeadingWhitespace
public static java.lang.String trimLeadingWhitespace(java.lang.String str)
Trim leading whitespace from the givenString.- Parameters:
str- theStringto check- Returns:
- the trimmed
String - See Also:
Character.isWhitespace(char)
-
trimTrailingWhitespace
public static java.lang.String trimTrailingWhitespace(java.lang.String str)
Trim trailing whitespace from the givenString.- Parameters:
str- theStringto check- Returns:
- the trimmed
String - See Also:
Character.isWhitespace(char)
-
trimLeadingCharacter
public static java.lang.String trimLeadingCharacter(java.lang.String str, char leadingCharacter)Trim all occurrences of the supplied leading character from the givenString.- Parameters:
str- theStringto checkleadingCharacter- the leading character to be trimmed- Returns:
- the trimmed
String
-
trimTrailingCharacter
public static java.lang.String trimTrailingCharacter(java.lang.String str, char trailingCharacter)Trim all occurrences of the supplied trailing character from the givenString.- Parameters:
str- theStringto checktrailingCharacter- the trailing character to be trimmed- Returns:
- the trimmed
String
-
matchesCharacter
public static boolean matchesCharacter(@Nullable java.lang.String str, char singleCharacter)Test if the givenStringmatches the given single character.- Parameters:
str- theStringto checksingleCharacter- the character to compare to- Since:
- 5.2.9
-
startsWithIgnoreCase
public static boolean startsWithIgnoreCase(@Nullable java.lang.String str, @Nullable java.lang.String prefix)Test if the givenStringstarts with the specified prefix, ignoring upper/lower case.- Parameters:
str- theStringto checkprefix- the prefix to look for- See Also:
String.startsWith(java.lang.String, int)
-
endsWithIgnoreCase
public static boolean endsWithIgnoreCase(@Nullable java.lang.String str, @Nullable java.lang.String suffix)Test if the givenStringends with the specified suffix, ignoring upper/lower case.- Parameters:
str- theStringto checksuffix- the suffix to look for- See Also:
String.endsWith(java.lang.String)
-
substringMatch
public static boolean substringMatch(java.lang.CharSequence str, int index, java.lang.CharSequence substring)Test whether the given string matches the given substring at the given index.- Parameters:
str- the original string (or StringBuilder)index- the index in the original string to start matching againstsubstring- the substring to match at the given index
-
countOccurrencesOf
public static int countOccurrencesOf(java.lang.String str, java.lang.String sub)Count the occurrences of the substringsubin stringstr.- Parameters:
str- string to search insub- string to search for
-
replace
public static java.lang.String replace(java.lang.String inString, java.lang.String oldPattern, @Nullable java.lang.String newPattern)Replace all occurrences of a substring within a string with another string.- Parameters:
inString-Stringto examineoldPattern-Stringto replacenewPattern-Stringto insert- Returns:
- a
Stringwith the replacements
-
delete
public static java.lang.String delete(java.lang.String inString, java.lang.String pattern)Delete all occurrences of the given substring.- Parameters:
inString- the originalStringpattern- the pattern to delete all occurrences of- Returns:
- the resulting
String
-
deleteAny
public static java.lang.String deleteAny(java.lang.String inString, @Nullable java.lang.String charsToDelete)Delete any character in a givenString.- Parameters:
inString- the originalStringcharsToDelete- a set of characters to delete. E.g. "az\n" will delete 'a's, 'z's and new lines.- Returns:
- the resulting
String
-
quote
@Nullable public static java.lang.String quote(@Nullable java.lang.String str)Quote the givenStringwith single quotes.- Parameters:
str- the inputString(e.g. "myString")- Returns:
- the quoted
String(e.g. "'myString'"), ornullif the input wasnull
-
quoteIfString
@Nullable public static java.lang.Object quoteIfString(@Nullable java.lang.Object obj)Turn the given Object into aStringwith single quotes if it is aString; keeping the Object as-is else.- Parameters:
obj- the input Object (e.g. "myString")- Returns:
- the quoted
String(e.g. "'myString'"), or the input object as-is if not aString
-
unqualify
public static java.lang.String unqualify(java.lang.String qualifiedName)
Unqualify a string qualified by a '.' dot character. For example, "this.name.is.qualified", returns "qualified".- Parameters:
qualifiedName- the qualified name
-
unqualify
public static java.lang.String unqualify(java.lang.String qualifiedName, char separator)Unqualify a string qualified by a separator character. For example, "this:name:is:qualified" returns "qualified" if using a ':' separator.- Parameters:
qualifiedName- the qualified nameseparator- the separator
-
capitalize
public static java.lang.String capitalize(java.lang.String str)
Capitalize aString, changing the first letter to upper case as perCharacter.toUpperCase(char). No other letters are changed.- Parameters:
str- theStringto capitalize- Returns:
- the capitalized
String
-
uncapitalize
public static java.lang.String uncapitalize(java.lang.String str)
Uncapitalize aString, changing the first letter to lower case as perCharacter.toLowerCase(char). No other letters are changed.- Parameters:
str- theStringto uncapitalize- Returns:
- the uncapitalized
String
-
getFilename
@Nullable public static java.lang.String getFilename(@Nullable java.lang.String path)Extract the filename from the given Java resource path, e.g."mypath/myfile.txt" → "myfile.txt".- Parameters:
path- the file path (may benull)- Returns:
- the extracted filename, or
nullif none
-
getFilenameExtension
@Nullable public static java.lang.String getFilenameExtension(@Nullable java.lang.String path)Extract the filename extension from the given Java resource path, e.g. "mypath/myfile.txt" → "txt".- Parameters:
path- the file path (may benull)- Returns:
- the extracted filename extension, or
nullif none
-
stripFilenameExtension
public static java.lang.String stripFilenameExtension(java.lang.String path)
Strip the filename extension from the given Java resource path, e.g. "mypath/myfile.txt" → "mypath/myfile".- Parameters:
path- the file path- Returns:
- the path with stripped filename extension
-
applyRelativePath
public static java.lang.String applyRelativePath(java.lang.String path, java.lang.String relativePath)Apply the given relative path to the given Java resource path, assuming standard Java folder separation (i.e. "/" separators).- Parameters:
path- the path to start from (usually a full file path)relativePath- the relative path to apply (relative to the full file path above)- Returns:
- the full file path that results from applying the relative path
-
cleanPath
public static java.lang.String cleanPath(java.lang.String path)
Normalize the path by suppressing sequences like "path/.." and inner simple dots.The result is convenient for path comparison. For other uses, notice that Windows separators ("\") are replaced by simple slashes.
NOTE that
cleanPathshould not be depended upon in a security context. Other mechanisms should be used to prevent path-traversal issues.- Parameters:
path- the original path- Returns:
- the normalized path
-
pathEquals
public static boolean pathEquals(java.lang.String path1, java.lang.String path2)Compare two paths after normalization of them.- Parameters:
path1- first path for comparisonpath2- second path for comparison- Returns:
- whether the two paths are equivalent after normalization
-
uriDecode
public static java.lang.String uriDecode(java.lang.String source, java.nio.charset.Charset charset)Decode the given encoded URI component value. Based on the following rules:- Alphanumeric characters
"a"through"z","A"through"Z", and"0"through"9"stay the same. - Special characters
"-","_",".", and"*"stay the same. - A sequence "
%<i>xy</i>" is interpreted as a hexadecimal representation of the character.
- Parameters:
source- the encoded Stringcharset- the character set- Returns:
- the decoded value
- Throws:
java.lang.IllegalArgumentException- when the given source contains invalid encoded sequences- Since:
- 5.0
- See Also:
URLDecoder.decode(String, String)
- Alphanumeric characters
-
parseLocale
@Nullable public static java.util.Locale parseLocale(java.lang.String localeValue)
Parse the givenStringvalue into aLocale, accepting theLocale.toString()format as well as BCP 47 language tags as specified byLocale.forLanguageTag(java.lang.String).- Parameters:
localeValue- the locale value: following eitherLocale'stoString()format ("en", "en_UK", etc), also accepting spaces as separators (as an alternative to underscores), or BCP 47 (e.g. "en-UK")- Returns:
- a corresponding
Localeinstance, ornullif none - Throws:
java.lang.IllegalArgumentException- in case of an invalid locale specification- Since:
- 5.0.4
- See Also:
parseLocaleString(java.lang.String),Locale.forLanguageTag(java.lang.String)
-
parseLocaleString
@Nullable public static java.util.Locale parseLocaleString(java.lang.String localeString)
Parse the givenStringrepresentation into aLocale.For many parsing scenarios, this is an inverse operation of
Locale's toString, in a lenient sense. This method does not aim for strictLocaledesign compliance; it is rather specifically tailored for typical Spring parsing needs.Note: This delegate does not accept the BCP 47 language tag format. Please use
parseLocale(java.lang.String)for lenient parsing of both formats.- Parameters:
localeString- the localeString: followingLocale'stoString()format ("en", "en_UK", etc), also accepting spaces as separators (as an alternative to underscores)- Returns:
- a corresponding
Localeinstance, ornullif none - Throws:
java.lang.IllegalArgumentException- in case of an invalid locale specification
-
toLanguageTag
@Deprecated public static java.lang.String toLanguageTag(java.util.Locale locale)
Deprecated.as of 5.0.4, in favor ofLocale.toLanguageTag()Determine the RFC 3066 compliant language tag, as used for the HTTP "Accept-Language" header.- Parameters:
locale- the Locale to transform to a language tag- Returns:
- the RFC 3066 compliant language tag as
String
-
parseTimeZoneString
public static java.util.TimeZone parseTimeZoneString(java.lang.String timeZoneString)
Parse the giventimeZoneStringvalue into aTimeZone.- Parameters:
timeZoneString- the time zoneString, followingTimeZone.getTimeZone(String)but throwingIllegalArgumentExceptionin case of an invalid time zone specification- Returns:
- a corresponding
TimeZoneinstance - Throws:
java.lang.IllegalArgumentException- in case of an invalid time zone specification
-
toStringArray
public static java.lang.String[] toStringArray(@Nullable java.util.Collection<java.lang.String> collection)Copy the givenCollectioninto aStringarray.The
Collectionmust containStringelements only.- Parameters:
collection- theCollectionto copy (potentiallynullor empty)- Returns:
- the resulting
Stringarray
-
toStringArray
public static java.lang.String[] toStringArray(@Nullable java.util.Enumeration<java.lang.String> enumeration)Copy the givenEnumerationinto aStringarray.The
Enumerationmust containStringelements only.- Parameters:
enumeration- theEnumerationto copy (potentiallynullor empty)- Returns:
- the resulting
Stringarray
-
addStringToArray
public static java.lang.String[] addStringToArray(@Nullable java.lang.String[] array, java.lang.String str)Append the givenStringto the givenStringarray, returning a new array consisting of the input array contents plus the givenString.- Parameters:
array- the array to append to (can benull)str- theStringto append- Returns:
- the new array (never
null)
-
concatenateStringArrays
@Nullable public static java.lang.String[] concatenateStringArrays(@Nullable java.lang.String[] array1, @Nullable java.lang.String[] array2)Concatenate the givenStringarrays into one, with overlapping array elements included twice.The order of elements in the original arrays is preserved.
- Parameters:
array1- the first array (can benull)array2- the second array (can benull)- Returns:
- the new array (
nullif both given arrays werenull)
-
mergeStringArrays
@Deprecated @Nullable public static java.lang.String[] mergeStringArrays(@Nullable java.lang.String[] array1, @Nullable java.lang.String[] array2)Deprecated.as of 4.3.15, in favor of manual merging viaLinkedHashSet(with every entry included at most once, even entries within the first array)Merge the givenStringarrays into one, with overlapping array elements only included once.The order of elements in the original arrays is preserved (with the exception of overlapping elements, which are only included on their first occurrence).
- Parameters:
array1- the first array (can benull)array2- the second array (can benull)- Returns:
- the new array (
nullif both given arrays werenull)
-
sortStringArray
public static java.lang.String[] sortStringArray(java.lang.String[] array)
Sort the givenStringarray if necessary.- Parameters:
array- the original array (potentially empty)- Returns:
- the array in sorted form (never
null)
-
trimArrayElements
public static java.lang.String[] trimArrayElements(java.lang.String[] array)
Trim the elements of the givenStringarray, callingString.trim()on each non-null element.- Parameters:
array- the originalStringarray (potentially empty)- Returns:
- the resulting array (of the same size) with trimmed elements
-
removeDuplicateStrings
public static java.lang.String[] removeDuplicateStrings(java.lang.String[] array)
Remove duplicate strings from the given array.As of 4.2, it preserves the original order, as it uses a
LinkedHashSet.- Parameters:
array- theStringarray (potentially empty)- Returns:
- an array without duplicates, in natural sort order
-
split
@Nullable public static java.lang.String[] split(@Nullable java.lang.String toSplit, @Nullable java.lang.String delimiter)Split aStringat the first occurrence of the delimiter. Does not include the delimiter in the result.- Parameters:
toSplit- the string to split (potentiallynullor empty)delimiter- to split the string up with (potentiallynullor empty)- Returns:
- a two element array with index 0 being before the delimiter, and
index 1 being after the delimiter (neither element includes the delimiter);
or
nullif the delimiter wasn't found in the given inputString
-
splitArrayElementsIntoProperties
@Nullable public static java.util.Properties splitArrayElementsIntoProperties(java.lang.String[] array, java.lang.String delimiter)Take an array of strings and split each element based on the given delimiter. APropertiesinstance is then generated, with the left of the delimiter providing the key, and the right of the delimiter providing the value.Will trim both the key and value before adding them to the
Properties.- Parameters:
array- the array to processdelimiter- to split each element using (typically the equals symbol)- Returns:
- a
Propertiesinstance representing the array contents, ornullif the array to process wasnullor empty
-
splitArrayElementsIntoProperties
@Nullable public static java.util.Properties splitArrayElementsIntoProperties(java.lang.String[] array, java.lang.String delimiter, @Nullable java.lang.String charsToDelete)Take an array of strings and split each element based on the given delimiter. APropertiesinstance is then generated, with the left of the delimiter providing the key, and the right of the delimiter providing the value.Will trim both the key and value before adding them to the
Propertiesinstance.- Parameters:
array- the array to processdelimiter- to split each element using (typically the equals symbol)charsToDelete- one or more characters to remove from each element prior to attempting the split operation (typically the quotation mark symbol), ornullif no removal should occur- Returns:
- a
Propertiesinstance representing the array contents, ornullif the array to process wasnullor empty
-
tokenizeToStringArray
public static java.lang.String[] tokenizeToStringArray(@Nullable java.lang.String str, java.lang.String delimiters)Tokenize the givenStringinto aStringarray via aStringTokenizer.Trims tokens and omits empty tokens.
The given
delimitersstring can consist of any number of delimiter characters. Each of those characters can be used to separate tokens. A delimiter is always a single character; for multi-character delimiters, consider usingdelimitedListToStringArray(java.lang.String, java.lang.String).- Parameters:
str- theStringto tokenize (potentiallynullor empty)delimiters- the delimiter characters, assembled as aString(each of the characters is individually considered as a delimiter)- Returns:
- an array of the tokens
- See Also:
StringTokenizer,String.trim(),delimitedListToStringArray(java.lang.String, java.lang.String)
-
tokenizeToStringArray
public static java.lang.String[] tokenizeToStringArray(@Nullable java.lang.String str, java.lang.String delimiters, boolean trimTokens, boolean ignoreEmptyTokens)Tokenize the givenStringinto aStringarray via aStringTokenizer.The given
delimitersstring can consist of any number of delimiter characters. Each of those characters can be used to separate tokens. A delimiter is always a single character; for multi-character delimiters, consider usingdelimitedListToStringArray(java.lang.String, java.lang.String).- Parameters:
str- theStringto tokenize (potentiallynullor empty)delimiters- the delimiter characters, assembled as aString(each of the characters is individually considered as a delimiter)trimTokens- trim the tokens viaString.trim()ignoreEmptyTokens- omit empty tokens from the result array (only applies to tokens that are empty after trimming; StringTokenizer will not consider subsequent delimiters as token in the first place).- Returns:
- an array of the tokens
- See Also:
StringTokenizer,String.trim(),delimitedListToStringArray(java.lang.String, java.lang.String)
-
delimitedListToStringArray
public static java.lang.String[] delimitedListToStringArray(@Nullable java.lang.String str, @Nullable java.lang.String delimiter)Take aStringthat is a delimited list and convert it into aStringarray.A single
delimitermay consist of more than one character, but it will still be considered as a single delimiter string, rather than as a bunch of potential delimiter characters, in contrast totokenizeToStringArray(java.lang.String, java.lang.String).- Parameters:
str- the inputString(potentiallynullor empty)delimiter- the delimiter between elements (this is a single delimiter, rather than a bunch individual delimiter characters)- Returns:
- an array of the tokens in the list
- See Also:
tokenizeToStringArray(java.lang.String, java.lang.String)
-
delimitedListToStringArray
public static java.lang.String[] delimitedListToStringArray(@Nullable java.lang.String str, @Nullable java.lang.String delimiter, @Nullable java.lang.String charsToDelete)Take aStringthat is a delimited list and convert it into aStringarray.A single
delimitermay consist of more than one character, but it will still be considered as a single delimiter string, rather than as a bunch of potential delimiter characters, in contrast totokenizeToStringArray(java.lang.String, java.lang.String).- Parameters:
str- the inputString(potentiallynullor empty)delimiter- the delimiter between elements (this is a single delimiter, rather than a bunch individual delimiter characters)charsToDelete- a set of characters to delete; useful for deleting unwanted line breaks: e.g. "\r\n\f" will delete all new lines and line feeds in aString- Returns:
- an array of the tokens in the list
- See Also:
tokenizeToStringArray(java.lang.String, java.lang.String)
-
commaDelimitedListToStringArray
public static java.lang.String[] commaDelimitedListToStringArray(@Nullable java.lang.String str)Convert a comma delimited list (e.g., a row from a CSV file) into an array of strings.- Parameters:
str- the inputString(potentiallynullor empty)- Returns:
- an array of strings, or the empty array in case of empty input
-
commaDelimitedListToSet
public static java.util.Set<java.lang.String> commaDelimitedListToSet(@Nullable java.lang.String str)Convert a comma delimited list (e.g., a row from a CSV file) into a set.Note that this will suppress duplicates, and as of 4.2, the elements in the returned set will preserve the original order in a
LinkedHashSet.- Parameters:
str- the inputString(potentiallynullor empty)- Returns:
- a set of
Stringentries in the list - See Also:
removeDuplicateStrings(String[])
-
collectionToDelimitedString
public static java.lang.String collectionToDelimitedString(@Nullable java.util.Collection<?> coll, java.lang.String delim, java.lang.String prefix, java.lang.String suffix)Convert aCollectionto a delimitedString(e.g. CSV).Useful for
toString()implementations.- Parameters:
coll- theCollectionto convert (potentiallynullor empty)delim- the delimiter to use (typically a ",")prefix- theStringto start each element withsuffix- theStringto end each element with- Returns:
- the delimited
String
-
collectionToDelimitedString
public static java.lang.String collectionToDelimitedString(@Nullable java.util.Collection<?> coll, java.lang.String delim)Convert aCollectioninto a delimitedString(e.g. CSV).Useful for
toString()implementations.- Parameters:
coll- theCollectionto convert (potentiallynullor empty)delim- the delimiter to use (typically a ",")- Returns:
- the delimited
String
-
collectionToCommaDelimitedString
public static java.lang.String collectionToCommaDelimitedString(@Nullable java.util.Collection<?> coll)Convert aCollectioninto a delimitedString(e.g., CSV).Useful for
toString()implementations.- Parameters:
coll- theCollectionto convert (potentiallynullor empty)- Returns:
- the delimited
String
-
arrayToDelimitedString
public static java.lang.String arrayToDelimitedString(@Nullable java.lang.Object[] arr, java.lang.String delim)Convert aStringarray into a delimitedString(e.g. CSV).Useful for
toString()implementations.- Parameters:
arr- the array to display (potentiallynullor empty)delim- the delimiter to use (typically a ",")- Returns:
- the delimited
String
-
arrayToCommaDelimitedString
public static java.lang.String arrayToCommaDelimitedString(@Nullable java.lang.Object[] arr)Convert aStringarray into a comma delimitedString(i.e., CSV).Useful for
toString()implementations.- Parameters:
arr- the array to display (potentiallynullor empty)- Returns:
- the delimited
String
-
-