Package org.teiid.core.util
Class StringUtil
- java.lang.Object
-
- org.teiid.core.util.StringUtil
-
public final class StringUtil extends Object
This is a common place to put String utility methods.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
StringUtil.Constants
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T extends Enum<T>>
TcaseInsensitiveValueOf(Class<T> enumType, String name)
static boolean
endsWithIgnoreCase(String text, String suffix)
Tests if the string ends with the specified suffix.static boolean
equalsIgnoreCase(String s1, String s2)
static String
getFirstToken(String str, String delimiter)
Return the first token in the string.static String
getLastToken(String str, String delimiter)
Return the last token in the string.static String
getStackTrace(Throwable t)
static List<String>
getTokens(String str, String delimiter)
Return the tokens in a string in a list.static int
indexOfIgnoreCase(String text, String str)
Returns the index within this string of the first occurrence of the specified substring.static boolean
isDigit(char c)
static boolean
isEmpty(String text)
Returns whether the specified text is either empty or null.static boolean
isLetter(char c)
static boolean
isLetterOrDigit(char c)
static String
join(Collection<String> strings, String delimiter)
Join string pieces and separate with a delimiter.static String
replace(String source, String search, String replace)
Replace a single occurrence of the search string with the replace string in the source string.static String
replaceAll(String source, String search, String replace)
Replace all occurrences of the search string with the replace string in the source string.static List<String>
split(String str, String splitter)
Split a string into pieces based on delimiters.static boolean
startsWithIgnoreCase(String text, String prefix)
Tests if the string starts with the specified prefix.static List<String>
tokenize(String str, char delim)
static String
toString(Object[] array)
Return a stringified version of the array, using a ',' as a delimiterstatic String
toString(Object[] array, String delim)
Return a stringified version of the array.static String
toString(Object[] array, String delim, boolean includeBrackets)
Return a stringified version of the array.static String
unescape(CharSequence string, int quoteChar, boolean useAsciiEscapes, StringBuilder sb)
Unescape the given stringstatic <T> T
valueOf(String value, Class type)
Convert the given value to specified type.
-
-
-
Method Detail
-
join
public static String join(Collection<String> strings, String delimiter)
Join string pieces and separate with a delimiter. Similar to the perl function of the same name. If strings or delimiter are null, null is returned. Otherwise, at least an empty string will be returned.- Parameters:
strings
- String pieces to joindelimiter
- Delimiter to put between string pieces- Returns:
- One merged string
- See Also:
split(java.lang.String, java.lang.String)
-
toString
public static String toString(Object[] array, String delim)
Return a stringified version of the array.- Parameters:
array
- the arraydelim
- the delimiter to use between array components- Returns:
- the string form of the array
-
toString
public static String toString(Object[] array, String delim, boolean includeBrackets)
Return a stringified version of the array.- Parameters:
array
- the arraydelim
- the delimiter to use between array components- Returns:
- the string form of the array
-
toString
public static String toString(Object[] array)
Return a stringified version of the array, using a ',' as a delimiter- Parameters:
array
- the array- Returns:
- the string form of the array
- See Also:
toString(Object[], String)
-
split
public static List<String> split(String str, String splitter)
Split a string into pieces based on delimiters. Similar to the perl function of the same name. The delimiters are not included in the returned strings.- Parameters:
str
- Full stringsplitter
- Characters to split on- Returns:
- List of String pieces from full string
- See Also:
join(java.util.Collection<java.lang.String>, java.lang.String)
-
replace
public static String replace(String source, String search, String replace)
Replace a single occurrence of the search string with the replace string in the source string. If any of the strings is null or the search string is zero length, the source string is returned.- Parameters:
source
- the source string whose contents will be alteredsearch
- the string to search for in sourcereplace
- the string to substitute for search if present- Returns:
- source string with the *first* occurrence of the search string replaced with the replace string
-
replaceAll
public static String replaceAll(String source, String search, String replace)
Replace all occurrences of the search string with the replace string in the source string. If any of the strings is null or the search string is zero length, the source string is returned.- Parameters:
source
- the source string whose contents will be alteredsearch
- the string to search for in sourcereplace
- the string to substitute for search if present- Returns:
- source string with *all* occurrences of the search string replaced with the replace string
-
getTokens
public static List<String> getTokens(String str, String delimiter)
Return the tokens in a string in a list. This is particularly helpful if the tokens need to be processed in reverse order. In that case, a list iterator can be acquired from the list for reverse order traversal.- Parameters:
str
- String to be tokenizeddelimiter
- Characters which are delimit tokens- Returns:
- List of string tokens contained in the tokenized string
-
getLastToken
public static String getLastToken(String str, String delimiter)
Return the last token in the string.- Parameters:
str
- String to be tokenizeddelimiter
- Characters which are delimit tokens- Returns:
- the last token contained in the tokenized string
-
getFirstToken
public static String getFirstToken(String str, String delimiter)
Return the first token in the string.- Parameters:
str
- String to be tokenizeddelimiter
- Characters which are delimit tokens- Returns:
- the first token contained in the tokenized string
-
isEmpty
public static boolean isEmpty(String text)
Returns whether the specified text is either empty or null.
- Parameters:
text
- The text to check; may be null;- Returns:
- True if the specified text is either empty or null.
- Since:
- 4.0
-
indexOfIgnoreCase
public static int indexOfIgnoreCase(String text, String str)
Returns the index within this string of the first occurrence of the specified substring. The integer returned is the smallest value k such that:
isthis.startsWith(str, k)
true
.- Parameters:
text
- any string.str
- any string.- Returns:
- if the str argument occurs as a substring within text,
then the index of the first character of the first
such substring is returned; if it does not occur as a
substring,
-1
is returned. If the text or str argument is null or empty then-1
is returned.
-
startsWithIgnoreCase
public static boolean startsWithIgnoreCase(String text, String prefix)
Tests if the string starts with the specified prefix.- Parameters:
text
- the string to test.prefix
- the prefix.- Returns:
true
if the character sequence represented by the argument is a prefix of the character sequence represented by this string;false
otherwise. Note also thattrue
will be returned if the prefix is an empty string or is equal to the textString
object as determined by theObject.equals(Object)
method. If the text or prefix argument is nullfalse
is returned.- Since:
- JDK1. 0
-
endsWithIgnoreCase
public static boolean endsWithIgnoreCase(String text, String suffix)
Tests if the string ends with the specified suffix.
-
isLetter
public static boolean isLetter(char c)
-
isDigit
public static boolean isDigit(char c)
-
isLetterOrDigit
public static boolean isLetterOrDigit(char c)
-
valueOf
public static <T> T valueOf(String value, Class type)
Convert the given value to specified type.- Parameters:
value
-type
-- Returns:
-
caseInsensitiveValueOf
public static <T extends Enum<T>> T caseInsensitiveValueOf(Class<T> enumType, String name)
-
unescape
public static String unescape(CharSequence string, int quoteChar, boolean useAsciiEscapes, StringBuilder sb)
Unescape the given string- Parameters:
string
-quoteChar
-useAsciiEscapes
-sb
- a scratch buffer to use- Returns:
-
-