Package liquibase.util
Class StringUtil
- java.lang.Object
-
- liquibase.util.StringUtil
-
public class StringUtil extends Object
Various utility methods for working with strings.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classStringUtil.DefaultFormatterstatic interfaceStringUtil.StringUtilFormatter<Type>static classStringUtil.ToStringFormatter
-
Constructor Summary
Constructors Constructor Description StringUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static StringconcatConsistentCase(String baseString, String addition)Concatenates the addition string to the baseString string, adjusting the case of "addition" to match the base string.static booleancontains(String value, String containsValue)Returns true if the input string contains the specified valuestatic booleanendsWith(String value, String endsWith)Checks whether the givenvalueends with the specifiedendsWithstring.static booleanequalsIgnoreCaseAndEmpty(String s1, String s2)Returns if two strings are equal, ignoring: case (uppercase/lowercase) difference between null, and empty string, and a string that only has spacesstatic booleanequalsWordNull(String value)Check whether the value is 'null' (case insensitive)static StringescapeHtml(String str)static byte[]getBytesWithEncoding(String string)static StringgetLastBlockComment(String sqlString)Retrieves the last block comment in a SQL string, if any.static StringgetLastLineComment(String sqlString)Returns the last line comment from a given SQL string, if there is one.static booleanhasLowerCase(String string)static booleanhasUpperCase(String string)static Stringindent(String string)static Stringindent(String string, int padding)static booleanisAscii(char ch)Returns true if ch is a "7-bit-clean" ASCII character (ordinal number < 128).static booleanisAscii(String string)protected static booleanisDelimiter(String piece, String previousPiece, String endDelimiter)Returns true if the input is a delimiter in one of the popular RDBMSs.static booleanisEmpty(CharSequence cs)static booleanisEmpty(String value)Returns true if the input string is the empty string (null-safe).static booleanisMinimumVersion(String minimumVersion, int candidateMajor, int candidateMinor, int candidatePatch)Compares a minimum version number given in string form (only the first three parts are considered) with a candidate version given as the three ints major, minor and patch.static booleanisNotEmpty(String value)Returns true if the input string is NOT the empty string.static booleanisNumeric(CharSequence cs)Checks whether the char sequence is numeric by checking that all chars in the sequence are numbers, so (-1, 1.0 and 1F) will return falsestatic booleanisWhitespace(CharSequence string)Returns true if the given string only consists of whitespace characters (null-safe)static Stringjoin(int[] array, String delimiter)static Stringjoin(Integer[] array, String delimiter)static Stringjoin(Object[] array, String delimiter, StringUtil.StringUtilFormatter formatter)static Stringjoin(String[] array, String delimiter)static Stringjoin(Collection<String> collection, String delimiter)static Stringjoin(Collection<String> collection, String delimiter, boolean sorted)static Stringjoin(Collection collection, String delimiter, StringUtil.StringUtilFormatter formatter)static Stringjoin(Collection collection, String delimiter, StringUtil.StringUtilFormatter formatter, boolean sorted)static Stringjoin(Map map, String delimiter)static Stringjoin(Map map, String delimiter, StringUtil.StringUtilFormatter formatter)static Stringjoin(ExtensibleObject extensibleObject, String delimiter)static Stringjoin(ExtensibleObject extensibleObject, String delimiter, StringUtil.StringUtilFormatter formatter)static StringleftPad(String value, int length)Adds spaces to the left of the input value until the string has reached the given length.static StringlimitSize(String string, int maxLength)static StringlowerCaseFirst(String string)static Stringpad(String value, int length)Adds spaces to the right of the input value until the string has reached the given length.static String[]processMultiLineSQL(String multiLineSQL, boolean stripComments, boolean splitStatements, String endDelimiter)Removes any comments from multiple line SQL usingstripComments(String, ChangeSet)and then extracts each individual statement usingsplitSQL(String, String, ChangeSet).static String[]processMultiLineSQL(String multiLineSQL, boolean stripComments, boolean splitStatements, String endDelimiter, ChangeSet changeSet)Removes any comments from multiple line SQL usingstripComments(String, ChangeSet)and then extracts each individual statement usingsplitSQL(String, String, ChangeSet).static String[]processMutliLineSQL(String multiLineSQL, boolean stripComments, boolean splitStatements, String endDelimiter)Deprecated.The new method isprocessMultiLineSQL(String, boolean, boolean, String, ChangeSet)(String)}static String[]processMutliLineSQL(String multiLineSQL, boolean stripComments, boolean splitStatements, String endDelimiter, ChangeSet changeSet)Deprecated.The new method isprocessMultiLineSQL(String, boolean, boolean, String, ChangeSet)(String)}static StringrandomIdentifer(int len)Produce a random identifer of the given length, consisting only of uppercase letters.static Stringrepeat(String string, int times)static List<String>splitAndTrim(String s, String regex)static String[]splitCamelCase(String str)Splits a camel-case string into words based on the came casing.static String[]splitSQL(String multiLineSQL, String endDelimiter)Splits a candidate multi-line SQL statement along ;'s and "go"'s.static String[]splitSQL(String multiLineSQL, String endDelimiter, ChangeSet changeSet)Splits a candidate multi-line SQL statement along ;'s and "go"'s.static List<String>splitToChunks(String input, int chunkSize)Split the input string into chunks no larger than the supplied chunkSize.static StringstandardizeLineEndings(String string)static booleanstartsWith(String value, String startsWith)Checks whether the givenvaluestarts with the specifiedstartsWithstring.static StringstripComments(String multiLineSQL)Searches through a String which contains SQL code and strips out any comments that are between \/**\/ or anything that matches SP--SP\n (to support the ANSI standard commenting of -- at the end of a line). static StringstripComments(String multiLineSQL, ChangeSet changeSet)Searches through a String which contains SQL code and strips out any comments that are between \/**\/ or anything that matches SP--SP\n (to support the ANSI standard commenting of -- at the end of a line). static StringstripEnclosingQuotes(String string)static StringstripSqlCommentsAndWhitespacesFromTheEnd(String sqlString)Strips the comments and white spaces from the end of given SQL string.static StringtoCamelCase(String string)Converts a kabob-case or underscore_case string to a camel-case onestatic StringtoKabobCase(String string)Converts a camelCase string to a kabob-case onestatic StringtoLowerWithoutWhitespaces(String value)static StringtrimRight(String string)Trimswhitespacecharacters from the end of specifiedstringstatic StringtrimToEmpty(String string)Returns the trimmed (left and right) version of the input string.static StringtrimToNull(String string)Returns the trimmed (left and right) form of the input string.static StringupperCaseFirst(String string)static Stringwrap(String inputStr, int wrapPoint, int extraLinePadding)Deprecated.Liquibase does not wrap any console output, and instead lets the terminal handle its own wrapping.
-
-
-
Method Detail
-
trimToEmpty
public static String trimToEmpty(String string)
Returns the trimmed (left and right) version of the input string. If null is passed, an empty string is returned.- Parameters:
string- the input string to trim- Returns:
- the trimmed string, or an empty string if the input was null.
-
trimToNull
public static String trimToNull(String string)
Returns the trimmed (left and right) form of the input string. If the string is empty after trimming (or null was passed in the first place), null is returned, i.e. the input string is reduced to nothing.- Parameters:
string- the string to trim- Returns:
- the trimmed string or null
-
processMultiLineSQL
public static String[] processMultiLineSQL(String multiLineSQL, boolean stripComments, boolean splitStatements, String endDelimiter)
Removes any comments from multiple line SQL usingstripComments(String, ChangeSet)and then extracts each individual statement usingsplitSQL(String, String, ChangeSet).- Parameters:
multiLineSQL- A String containing all the SQL statementsstripComments- If true then comments will be stripped, if false then they will be left in the code
-
processMultiLineSQL
public static String[] processMultiLineSQL(String multiLineSQL, boolean stripComments, boolean splitStatements, String endDelimiter, ChangeSet changeSet)
Removes any comments from multiple line SQL usingstripComments(String, ChangeSet)and then extracts each individual statement usingsplitSQL(String, String, ChangeSet).- Parameters:
multiLineSQL- A String containing all the SQL statementsstripComments- If true then comments will be stripped, if false then they will be left in the codechangeSet- the changeset associated with the sql being parsed
-
processMutliLineSQL
public static String[] processMutliLineSQL(String multiLineSQL, boolean stripComments, boolean splitStatements, String endDelimiter)
Deprecated.The new method isprocessMultiLineSQL(String, boolean, boolean, String, ChangeSet)(String)}Removes any comments from multiple line SQL usingstripComments(String, ChangeSet)and then extracts each individual statement usingsplitSQL(String, String, ChangeSet).- Parameters:
multiLineSQL- A String containing all the SQL statementsstripComments- If true then comments will be stripped, if false then they will be left in the code
-
processMutliLineSQL
public static String[] processMutliLineSQL(String multiLineSQL, boolean stripComments, boolean splitStatements, String endDelimiter, ChangeSet changeSet)
Deprecated.The new method isprocessMultiLineSQL(String, boolean, boolean, String, ChangeSet)(String)}Removes any comments from multiple line SQL usingstripComments(String, ChangeSet)and then extracts each individual statement usingsplitSQL(String, String, ChangeSet).- Parameters:
multiLineSQL- A String containing all the SQL statementsstripComments- If true then comments will be stripped, if false then they will be left in the codechangeSet- the changeset associated with the sql being parsed
-
isDelimiter
protected static boolean isDelimiter(String piece, String previousPiece, String endDelimiter)
Returns true if the input is a delimiter in one of the popular RDBMSs. Recognized delimiters are: semicolon (;), a slash (as the only content) or the word GO (as the only content).- Parameters:
piece- the input line to testpreviousPiece- the characters in the input stream that came before pieceendDelimiter- ??? (need to see this in a debugger to find out)
-
wrap
@Deprecated public static String wrap(String inputStr, int wrapPoint, int extraLinePadding)
Deprecated.Liquibase does not wrap any console output, and instead lets the terminal handle its own wrapping. If you wish to use this method, consider whether its usage is truly necessary.Add new lines to the input string to cause output to wrap. Optional line padding can be passed in for the additional lines that are created- Parameters:
inputStr- The string to split and wrapwrapPoint- The point at which to split the linesextraLinePadding- Any additional spaces to add- Returns:
- String Output string with new lines
-
splitSQL
public static String[] splitSQL(String multiLineSQL, String endDelimiter)
Splits a candidate multi-line SQL statement along ;'s and "go"'s.
-
splitSQL
public static String[] splitSQL(String multiLineSQL, String endDelimiter, ChangeSet changeSet)
Splits a candidate multi-line SQL statement along ;'s and "go"'s.- Parameters:
changeSet- the changeset associated with the sql being parsed
-
stripComments
public static String stripComments(String multiLineSQL)
Searches through a String which contains SQL code and strips out any comments that are between \/**\/ or anything that matches SP--SP\n (to support the ANSI standard commenting of -- at the end of a line). - Returns:
- The String without the comments in
-
stripComments
public static String stripComments(String multiLineSQL, ChangeSet changeSet)
Searches through a String which contains SQL code and strips out any comments that are between \/**\/ or anything that matches SP--SP\n (to support the ANSI standard commenting of -- at the end of a line). - Parameters:
changeSet- the changeset associated with the sql being parsed- Returns:
- The String without the comments in
-
join
public static String join(Object[] array, String delimiter, StringUtil.StringUtilFormatter formatter)
-
join
public static String join(Collection<String> collection, String delimiter)
-
join
public static String join(Collection collection, String delimiter, StringUtil.StringUtilFormatter formatter)
-
join
public static String join(Collection collection, String delimiter, StringUtil.StringUtilFormatter formatter, boolean sorted)
-
join
public static String join(Collection<String> collection, String delimiter, boolean sorted)
-
join
public static String join(Map map, String delimiter, StringUtil.StringUtilFormatter formatter)
-
join
public static String join(ExtensibleObject extensibleObject, String delimiter)
-
join
public static String join(ExtensibleObject extensibleObject, String delimiter, StringUtil.StringUtilFormatter formatter)
-
hasUpperCase
public static boolean hasUpperCase(String string)
-
hasLowerCase
public static boolean hasLowerCase(String string)
-
isAscii
public static boolean isAscii(String string)
-
isAscii
public static boolean isAscii(char ch)
Returns true if ch is a "7-bit-clean" ASCII character (ordinal number < 128).- Parameters:
ch- the character to test- Returns:
- true if 7 bit-clean, false otherwise.
-
pad
public static String pad(String value, int length)
Adds spaces to the right of the input value until the string has reached the given length. Nothing is done if the string already has the given length or if the string is even longer.- Parameters:
value- The string to pad (if necessary)length- the desired length- Returns:
- the input string, padded if necessary.
-
leftPad
public static String leftPad(String value, int length)
Adds spaces to the left of the input value until the string has reached the given length. Nothing is done if the string already has the given length or if the string is even longer.- Parameters:
value- The string to pad (if necessary)length- the desired length- Returns:
- the input string, padded if necessary.
-
contains
public static boolean contains(String value, String containsValue)
Returns true if the input string contains the specified value- Parameters:
value- String to be checkedcontainsValue- String to look for- Returns:
- true if String contains the value
-
isEmpty
public static boolean isEmpty(String value)
Returns true if the input string is the empty string (null-safe).- Parameters:
value- String to be checked- Returns:
- true if String is null or empty
-
isNotEmpty
public static boolean isNotEmpty(String value)
Returns true if the input string is NOT the empty string. If the string is null, false is returned.- Parameters:
value- String to be checked- Returns:
- true if string is not null and not empty (length > 0)
-
startsWith
public static boolean startsWith(String value, String startsWith)
Checks whether the givenvaluestarts with the specifiedstartsWithstring.- Parameters:
value- the string to checkstartsWith- the prefix to check for- Returns:
trueifvaluestarts withstartsWith,falseotherwise. Returnsfalseif either argument isnull.
-
endsWith
public static boolean endsWith(String value, String endsWith)
Checks whether the givenvalueends with the specifiedendsWithstring.- Parameters:
value- the string to checkendsWith- the prefix to check for- Returns:
trueifvalueends withendsWith,falseotherwise. Returnsfalseif either argument isnull.
-
isWhitespace
public static boolean isWhitespace(CharSequence string)
Returns true if the given string only consists of whitespace characters (null-safe)- Parameters:
string- the string to test- Returns:
- true if the string is null or only consists of whitespaces.
-
isMinimumVersion
public static boolean isMinimumVersion(String minimumVersion, int candidateMajor, int candidateMinor, int candidatePatch)
Compares a minimum version number given in string form (only the first three parts are considered) with a candidate version given as the three ints major, minor and patch.- Parameters:
minimumVersion- The minimum version that is required, given as a string with up to 3 parts, e.g. "7.4" or "9.6.3"candidateMajor- the version number to be tested, major partcandidateMinor- the version number to be tested, minor partcandidatePatch- the version number to be tested, patch part- Returns:
- true if candidateMajor.candidateMinor.candidatePatch >= minimumVersion or false if not
-
randomIdentifer
public static String randomIdentifer(int len)
Produce a random identifer of the given length, consisting only of uppercase letters.- Parameters:
len- desired length of the string- Returns:
- an identifier of the desired length
-
toKabobCase
public static String toKabobCase(String string)
Converts a camelCase string to a kabob-case one
-
toCamelCase
public static String toCamelCase(String string)
Converts a kabob-case or underscore_case string to a camel-case one
-
equalsIgnoreCaseAndEmpty
public static boolean equalsIgnoreCaseAndEmpty(String s1, String s2)
Returns if two strings are equal, ignoring:- case (uppercase/lowercase)
- difference between null, and empty string, and a string that only has spaces
- Parameters:
s1- the first String to compare (or null)s2- the second String to compare (or null)- Returns:
- true if the Strings are equal by the above criteria, false in all other cases
-
trimRight
public static String trimRight(String string)
Trimswhitespacecharacters from the end of specifiedstring- Parameters:
string- String to trim- Returns:
- new String without the whitespace at the end
-
getLastBlockComment
public static String getLastBlockComment(String sqlString)
Retrieves the last block comment in a SQL string, if any.- Parameters:
sqlString- the SQL string to search for the last block comment- Returns:
- the last block comment in the SQL string, or
nullif none was found
-
getLastLineComment
public static String getLastLineComment(String sqlString)
Returns the last line comment from a given SQL string, if there is one.- Parameters:
sqlString- the SQL string to search- Returns:
- the last line comment from the SQL string, or
nullif there is no line comment
-
stripSqlCommentsAndWhitespacesFromTheEnd
public static String stripSqlCommentsAndWhitespacesFromTheEnd(String sqlString)
Strips the comments and white spaces from the end of given SQL string.- Parameters:
sqlString- the SQL string to strip- Returns:
- the stripped SQL string
-
concatConsistentCase
public static String concatConsistentCase(String baseString, String addition)
Concatenates the addition string to the baseString string, adjusting the case of "addition" to match the base string. If the string is all caps, append addition in all caps. If all lower case, append in all lower case. If baseString is mixed case, make no changes to addition.
-
equalsWordNull
public static boolean equalsWordNull(String value)
Check whether the value is 'null' (case insensitive)
-
splitCamelCase
public static String[] splitCamelCase(String str)
Splits a camel-case string into words based on the came casing.
This code originated from the StringUtils class of commons-lang
- Parameters:
str- the String to split, may benull- Returns:
- an array of parsed Strings,
nullif null String input
-
getBytesWithEncoding
public static byte[] getBytesWithEncoding(String string)
-
toLowerWithoutWhitespaces
public static String toLowerWithoutWhitespaces(String value)
- Parameters:
value- string to process- Returns:
- string without any whitespaces formatted to lowercase.
-
isNumeric
public static boolean isNumeric(CharSequence cs)
Checks whether the char sequence is numeric by checking that all chars in the sequence are numbers, so (-1, 1.0 and 1F) will return false
This code originated from the StringUtils class of commons-lang
- Parameters:
cs- the arg to check if it is numeric- Returns:
- true if convertible to numeric and false otherwise
-
isEmpty
public static boolean isEmpty(CharSequence cs)
-
-