Package net.sf.basedb.core
Class StringUtil
java.lang.Object
net.sf.basedb.core.StringUtil
Utility methods that will make it easier to implement
data validation for string attributes.
- Version:
- 2.0
- Author:
- Nicklas
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic final String
Return the first non-null value (or null if all values are null).static final String
decapitalize
(String value) Decapitalize the string by making sure the first character is lower-case.static final String
getCommonPrefix
(Collection<String> strings) Find the longest common prefix for a collection of strings.static boolean
isEqualOrNull
(String s1, String s2) Check if two strings are equal or both are null.static boolean
isEqualOrNull
(String s1, String s2, boolean caseInsensitive) Check if two strings are equal or both are null.static final String
join
(Collection<?> values, String deliminator, boolean skipNull) Join a collection of values to a string, using the specified delimiator.static String
setNotNullString
(String value, String name, int maxLength) Trim and check the length of a string.static String
setNullableString
(String value, String name, int maxLength) Trim and check the length of a string.static final String[]
Tokenize a string by splitting it at positions matching the regular expression, optionally ignoring matches that are inside quotes.static final String[]
Tokenize a string by splitting it at positions matching the regular expression.static final String
trimString
(String s, int length) Trim a string to a maximum length.static final String
trimStringMiddle
(String s, int length) Trim a string to a maximum length.
-
Constructor Details
-
StringUtil
public StringUtil()
-
-
Method Details
-
setNullableString
public static String setNullableString(String value, String name, int maxLength) throws StringTooLongException Trim and check the length of a string. Null values are allowed.- Parameters:
value
- The string to checkname
- The name of the attribute to use if an exception is thrownmaxLength
- The maximum length of the string- Returns:
- The original string, trimmed from white-space at the beginning and end
- Throws:
StringTooLongException
- If the string is too long
-
setNotNullString
public static String setNotNullString(String value, String name, int maxLength) throws InvalidUseOfNullException, StringTooLongException Trim and check the length of a string. Null values are not allowed.- Parameters:
value
- The string to checkname
- The name of the attribute to use if an exception is thrownmaxLength
- The maximum length of the string- Returns:
- The original string, trimmed from white-space at the beginning and end
- Throws:
StringTooLongException
- If the string is too longInvalidUseOfNullException
- If the string is null
-
isEqualOrNull
Check if two strings are equal or both are null.- Parameters:
s1
- The first strings2
- The second string- Returns:
- TRUE if the strings are equal or both are null, FALSE otherwise
-
isEqualOrNull
Check if two strings are equal or both are null.- Parameters:
s1
- The first strings2
- The second stringcaseInsensitive
- TRUE if the comparison should ignore case, FALSE otherwise- Returns:
- TRUE if the strings are equal or both are null, FALSE otherwise
- Since:
- 2.4
-
join
Join a collection of values to a string, using the specified delimiator.- Parameters:
values
- The values to joindeliminator
- The deliminator between the valuesskipNull
- If null values should be skipper or not- Returns:
- The resulting string
-
trimString
Trim a string to a maximum length. String longer than the specified length will be cut off and three dots are appended. If no trimming is needed the original string is returned.- Parameters:
s
- The string to trimlength
- The maximum length of the string- Returns:
- The trimmed string
-
trimStringMiddle
Trim a string to a maximum length. Strings longer than the specified length will have text cut-out from the middle and replaced by three dots. If no trimming is needed the original string is returned.- Parameters:
s
- The string to trimlength
- The maximum length of the string- Returns:
- The trimmed string
- Since:
- 2.14
-
tokenize
Tokenize a string by splitting it at positions matching the regular expression, optionally ignoring matches that are inside quotes.- Parameters:
s
- The string to tokenizeregexp
- The regular expression for matching split sitesnotInsideQuotes
- If TRUE, the regexp is not matched inside quotes- Returns:
- An array with the elements of the array
- Since:
- 2.15
-
tokenize
public static final String[] tokenize(String s, String regexp, boolean notInsideQuotes, boolean removeQuotes) Tokenize a string by splitting it at positions matching the regular expression. Matches that are inside quotes can optionally be ignored. Elements with quotes can optionally have the quotes removed.- Parameters:
s
- The string to tokenizeregexp
- The regular expression for matching split sitesnotInsideQuotes
- If TRUE, the regexp is not matched inside quotesremoveQuotes
- If TRUE, remove quotes from all elements before returning the result- Returns:
- An array with the elements of the array
- Since:
- 2.17
-
getCommonPrefix
Find the longest common prefix for a collection of strings. If the collection is null, empty or contains at least one null element null is returned. If the given strings doesn't have a common prefix the empty string is returned.- Parameters:
strings
- A collection of string objects- Returns:
- The longes string such that
String.startsWith(String)
returns true for all strings in the given collection - Since:
- 2.17
-
decapitalize
Decapitalize the string by making sure the first character is lower-case. All other characters are left as they are. If the string is empty, the empty string is returned. If the string is null, null is returned.- Since:
- 3.2
-
coalesce
Return the first non-null value (or null if all values are null).- Since:
- 3.19
-