public final class StringUtil
extends java.lang.Object
| Modifier and Type | Field and Description |
|---|---|
static java.lang.String |
DELIMITER_STRING
The default delimiter string, used by the
toXXXArray()
methods. |
| Modifier and Type | Method and Description |
|---|---|
static java.lang.String |
camelToLisp(java.lang.String pString)
Converts the input string
from camel-style (Java in-fix) naming convention
to Lisp-style naming convention (hyphen delimitted, all lower case).
|
static java.lang.String |
capitalize(java.lang.String pString)
Makes the first letter of a String uppercase.
|
static java.lang.String |
capitalize(java.lang.String pString,
int pIndex)
Makes the Nth letter of a String uppercase.
|
static boolean |
contains(java.lang.String pString,
int pChar)
Tests if a string contains a specific character.
|
static boolean |
contains(java.lang.String pContainer,
java.lang.String pLookFor)
Tests if a string contains another string.
|
static boolean |
containsIgnoreCase(java.lang.String pString,
int pChar)
Tests if a string contains a specific character, ignoring case.
|
static boolean |
containsIgnoreCase(java.lang.String pContainer,
java.lang.String pLookFor)
Tests if a string contains another string, ignoring case.
|
static java.lang.String |
cut(java.lang.String pString,
int pMaxLen,
java.lang.String pPad)
Cuts a string between two words, before a sepcified length, if the
string is longer than the maxium lenght.
|
static java.lang.String |
decode(byte[] pData,
int pOffset,
int pLength,
java.lang.String pCharset)
Constructs a new
String by decoding the specified sub array of bytes using the specified charset. |
static java.lang.String |
deepToString(java.lang.Object pObject) |
static java.lang.String |
deepToString(java.lang.Object pObject,
boolean pForceDeep,
int pDepth) |
static java.lang.String |
getFirstElement(java.lang.String pSource,
java.lang.String pDelimiter)
Gets the first element of a
String containing string elements delimited by a given delimiter. |
static java.lang.String |
getLastElement(java.lang.String pSource,
java.lang.String pDelimiter)
Gets the last element of a
String containing string elements
delimited by a given delimiter. |
static java.lang.String |
identityToString(java.lang.Object pObject)
Returns a string on the same format as
Object.toString(). |
static int |
indexOfIgnoreCase(java.lang.String pString,
int pChar)
Returns the index within this string of the first occurrence of the
specified character.
|
static int |
indexOfIgnoreCase(java.lang.String pString,
int pChar,
int pPos)
Returns the index within this string of the first occurrence of the
specified character, starting at the specified index.
|
static int |
indexOfIgnoreCase(java.lang.String pString,
java.lang.String pLookFor)
Returns the index within this string of the first occurrence of the
specified substring.
|
static int |
indexOfIgnoreCase(java.lang.String pString,
java.lang.String pLookFor,
int pPos)
Returns the index within this string of the first occurrence of the
specified substring, starting at the specified index.
|
static boolean |
isEmpty(java.lang.String pString)
Tests if a String is null, or contains nothing but white-space.
|
static boolean |
isEmpty(java.lang.String[] pStringArray)
Tests a string array, to see if all items are null or an empty string.
|
static boolean |
isNumber(java.lang.String pString)
Tests a string, to see if it is an number (element of Z).
|
static int |
lastIndexOfIgnoreCase(java.lang.String pString,
int pChar)
Returns the index within this string of the last occurrence of the
specified character.
|
static int |
lastIndexOfIgnoreCase(java.lang.String pString,
int pChar,
int pPos)
Returns the index within this string of the last occurrence of the
specified character, searching backward starting at the specified index.
|
static int |
lastIndexOfIgnoreCase(java.lang.String pString,
java.lang.String pLookFor)
Returns the index within this string of the rightmost occurrence of the
specified substring.
|
static int |
lastIndexOfIgnoreCase(java.lang.String pString,
java.lang.String pLookFor,
int pPos)
Returns the index within this string of the rightmost occurrence of the
specified substring.
|
static java.lang.String |
lispToCamel(java.lang.String pString)
Converts the input string
from Lisp-style naming convention (hyphen delimitted, all lower case)
to camel-style (Java in-fix) naming convention.
|
static java.lang.String |
lispToCamel(java.lang.String pString,
boolean pFirstUpperCase)
Converts the input string
from Lisp-style naming convention (hyphen delimitted, all lower case)
to camel-style (Java in-fix) naming convention.
|
static java.lang.String |
ltrim(java.lang.String pString)
Trims the argument string for whitespace on the left side only.
|
boolean |
matches(java.lang.String pString,
java.lang.String pRegex)
Tells whether or not the given string string matches the given regular
expression.
|
static java.lang.String |
pad(java.lang.String pSource,
int pRequiredLength,
java.lang.String pPadString,
boolean pPrepend)
String length check with simple concatenation of selected pad-string.
|
static java.lang.String |
replace(java.lang.String pSource,
java.lang.String pPattern,
java.lang.String pReplace)
Replaces a substring of a string with another string.
|
java.lang.String |
replaceAll(java.lang.String pString,
java.lang.String pRegex,
java.lang.String pReplacement)
Replaces each substring of this string that matches the given
regular expression with the given pReplacement.
|
java.lang.String |
replaceFirst(java.lang.String pString,
java.lang.String pRegex,
java.lang.String pReplacement)
Replaces the first substring of the given string that matches the given
regular expression with the given pReplacement.
|
static java.lang.String |
replaceIgnoreCase(java.lang.String pSource,
java.lang.String pPattern,
java.lang.String pReplace)
Replaces a substring of a string with another string, ignoring case.
|
static java.lang.String |
reverse(java.lang.String pString) |
static java.lang.String |
rtrim(java.lang.String pString)
Trims the argument string for whitespace on the right side only.
|
java.lang.String[] |
split(java.lang.String pString,
java.lang.String pRegex)
Splits this string around matches of the given regular expression.
|
java.lang.String[] |
split(java.lang.String pString,
java.lang.String pRegex,
int pLimit)
Splits this string around matches of the given regular expression.
|
static java.lang.String |
substring(java.lang.String pSource,
java.lang.String pBeginBoundaryString,
java.lang.String pEndBoundaryString,
int pOffset)
Gets the first substring between the given string boundaries.
|
static java.awt.Color |
toColor(java.lang.String pString)
Parses a string to a Color.
|
static java.lang.String |
toColorString(java.awt.Color pColor)
Creates a HTML/CSS String representation of the given color.
|
static java.lang.String |
toCSVString(java.lang.Object[] pStringArray)
Converts a string array to a string of comma-separated values.
|
static java.lang.String |
toCSVString(java.lang.Object[] pStringArray,
java.lang.String pDelimiterString)
Converts a string array to a string separated by the given delimiter.
|
static java.util.Date |
toDate(java.lang.String pString)
Converts the string to a date, using the default date format.
|
static java.util.Date |
toDate(java.lang.String pString,
java.text.DateFormat pFormat)
Converts the string to a date, using the given format.
|
static java.util.Date |
toDate(java.lang.String pString,
java.lang.String pFormat)
Converts the string to a date, using the given format.
|
static double[] |
toDoubleArray(java.lang.String pString)
Converts a comma-separated String to an array of doubles.
|
static double[] |
toDoubleArray(java.lang.String pString,
java.lang.String pDelimiters)
Converts a comma-separated String to an array of doubles.
|
static int[] |
toIntArray(java.lang.String pString)
Converts a comma-separated String to an array of ints.
|
static int[] |
toIntArray(java.lang.String pString,
java.lang.String pDelimiters)
Converts a comma-separated String to an array of ints.
|
static int[] |
toIntArray(java.lang.String pString,
java.lang.String pDelimiters,
int pBase)
Converts a comma-separated String to an array of ints.
|
static long[] |
toLongArray(java.lang.String pString)
Converts a comma-separated String to an array of longs.
|
static long[] |
toLongArray(java.lang.String pString,
java.lang.String pDelimiters)
Converts a comma-separated String to an array of longs.
|
static java.lang.String |
toLowerCase(java.lang.String pString)
Converts a string to lowercase.
|
static java.lang.String[] |
toStringArray(java.lang.String pString)
Converts a comma-separated String to an array of Strings.
|
static java.lang.String[] |
toStringArray(java.lang.String pString,
java.lang.String pDelimiters)
Converts a delimiter separated String to an array of Strings.
|
static java.sql.Timestamp |
toTimestamp(java.lang.String pValue)
Converts the string to a jdbc Timestamp, using the standard Timestamp
escape format.
|
static java.lang.String |
toUpperCase(java.lang.String pString)
Converts a string to uppercase.
|
static java.lang.String |
valueOf(java.lang.Object pObj)
Returns the value of the given
Object, as a String. |
public static final java.lang.String DELIMITER_STRING
toXXXArray()
methods.
Its value is ", \t\n\r\f".
public static java.lang.String decode(byte[] pData,
int pOffset,
int pLength,
java.lang.String pCharset)
String by decoding the specified sub array of bytes using the specified charset.
Replacement for new String(byte[], int, int, String), that does
not throw the checked UnsupportedEncodingException,
but instead the unchecked UnsupportedCharsetException if the character set is not supported.pData - the bytes to be decoded to characterspOffset - the index of the first byte to decodepLength - the number of bytes to decodepCharset - the name of a supported character setjava.nio.charset.UnsupportedCharsetExceptionString.String(byte[], int, int, String)public static java.lang.String valueOf(java.lang.Object pObj)
Object, as a String.
Unlike String.valueOf, this method returns null
instead of the String "null", if null is given as
the argument.pObj - the Object to find the String value of.null if the
pObj == null.String.valueOf(Object),
String.toString()public static java.lang.String toUpperCase(java.lang.String pString)
pString - the string to convertpublic static java.lang.String toLowerCase(java.lang.String pString)
pString - the string to convertpublic static boolean isEmpty(java.lang.String pString)
pString - The string to testpublic static boolean isEmpty(java.lang.String[] pStringArray)
pStringArray - The string array to check.public static boolean contains(java.lang.String pContainer,
java.lang.String pLookFor)
pContainer - The string to testpLookFor - The string to look fortrue if the container string is contains the string, and
both parameters are non-null, otherwise false.public static boolean containsIgnoreCase(java.lang.String pContainer,
java.lang.String pLookFor)
pContainer - The string to testpLookFor - The string to look fortrue if the container string is contains the string, and
both parameters are non-null, otherwise false.contains(String,String)public static boolean contains(java.lang.String pString,
int pChar)
pString - The string to check.pChar - The character to search for.public static boolean containsIgnoreCase(java.lang.String pString,
int pChar)
pString - The string to check.pChar - The character to search for.public static int indexOfIgnoreCase(java.lang.String pString,
java.lang.String pLookFor)
pString - The string to testpLookFor - The string to look forString.indexOf(String)public static int indexOfIgnoreCase(java.lang.String pString,
java.lang.String pLookFor,
int pPos)
pString - The string to testpLookFor - The string to look forpPos - The first index to testString.indexOf(String,int)public static int lastIndexOfIgnoreCase(java.lang.String pString,
java.lang.String pLookFor)
pString.length() - 1.pString - The string to testpLookFor - The string to look forString.lastIndexOf(String)public static int lastIndexOfIgnoreCase(java.lang.String pString,
java.lang.String pLookFor,
int pPos)
pPospString - The string to testpLookFor - The string to look forpPos - The last index to testString.lastIndexOf(String,int)public static int indexOfIgnoreCase(java.lang.String pString,
int pChar)
pString - The string to testpChar - The character to look forString.indexOf(int)public static int indexOfIgnoreCase(java.lang.String pString,
int pChar,
int pPos)
pString - The string to testpChar - The character to look forpPos - The first index to testString.indexOf(int,int)public static int lastIndexOfIgnoreCase(java.lang.String pString,
int pChar)
pString - The string to testpChar - The character to look forString.lastIndexOf(int)public static int lastIndexOfIgnoreCase(java.lang.String pString,
int pChar,
int pPos)
pString - The string to testpChar - The character to look forpPos - The last index to testString.lastIndexOf(int,int)public static java.lang.String ltrim(java.lang.String pString)
pString - the string to trimnull if
the string argument is null.rtrim(java.lang.String),
String.trim()public static java.lang.String rtrim(java.lang.String pString)
pString - the string to trimnull if
the string argument is null.ltrim(java.lang.String),
String.trim()public static java.lang.String replace(java.lang.String pSource,
java.lang.String pPattern,
java.lang.String pReplace)
pSource - The source StringpPattern - The pattern to replacepReplace - The new String to be inserted instead of the
replace Stringpublic static java.lang.String replaceIgnoreCase(java.lang.String pSource,
java.lang.String pPattern,
java.lang.String pReplace)
pSource - The source StringpPattern - The pattern to replacepReplace - The new String to be inserted instead of the
replace Stringreplace(String,String,String)public static java.lang.String cut(java.lang.String pString,
int pMaxLen,
java.lang.String pPad)
pString - The string to cutpMaxLen - The maximum length before cuttingpPad - The string to append at the end, aftrer cuttingpad(String,int,String,boolean)public static java.lang.String capitalize(java.lang.String pString,
int pIndex)
pString - The string to capitalizepIndex - The base-0 index of the char to capitalize.public static java.lang.String capitalize(java.lang.String pString)
pString - The string to capitalizepublic static java.lang.String pad(java.lang.String pSource,
int pRequiredLength,
java.lang.String pPadString,
boolean pPrepend)
pSource - The source string.pRequiredLength - The accurate length of the resulting string.pPadString - The string for concatenation.pPrepend - The location of fill-ins, prepend (true),
or append (false)cut(String,int,String)public static java.util.Date toDate(java.lang.String pString)
pString - the string to convertDateFormat,
DateFormat.getInstance()public static java.util.Date toDate(java.lang.String pString,
java.lang.String pFormat)
pString - the string to convertpFormat - the date formatSimpleDateFormat,
SimpleDateFormat.SimpleDateFormat(String)public static java.util.Date toDate(java.lang.String pString,
java.text.DateFormat pFormat)
pString - the string to convertpFormat - the date formatSimpleDateFormat,
SimpleDateFormat.SimpleDateFormat(String),
DateFormatpublic static java.sql.Timestamp toTimestamp(java.lang.String pValue)
pValue - the valueTimestampTimestamp,
Timestamp.valueOf(String)public static java.lang.String[] toStringArray(java.lang.String pString,
java.lang.String pDelimiters)
pString - The comma-separated stringpDelimiters - The delimiter stringString array containing the delimiter separated elementspublic static java.lang.String[] toStringArray(java.lang.String pString)
pString - The comma-separated stringString array containing the comma-separated elementstoStringArray(String,String)public static int[] toIntArray(java.lang.String pString,
java.lang.String pDelimiters,
int pBase)
pString - The comma-separated stringpDelimiters - The delimiter stringpBase - The radixint arrayjava.lang.NumberFormatException - if any of the elements are not parseable
as an intpublic static int[] toIntArray(java.lang.String pString)
pString - The comma-separated stringint arrayjava.lang.NumberFormatException - if any of the elements are not parseable
as an inttoStringArray(String,String),
DELIMITER_STRINGpublic static int[] toIntArray(java.lang.String pString,
java.lang.String pDelimiters)
pString - The comma-separated stringpDelimiters - The delimiter stringint arrayjava.lang.NumberFormatException - if any of the elements are not parseable
as an inttoIntArray(String,String)public static long[] toLongArray(java.lang.String pString,
java.lang.String pDelimiters)
pString - The comma-separated stringpDelimiters - The delimiter stringlong arrayjava.lang.NumberFormatException - if any of the elements are not parseable
as a longpublic static long[] toLongArray(java.lang.String pString)
pString - The comma-separated stringlong arrayjava.lang.NumberFormatException - if any of the elements are not parseable
as a longtoStringArray(String,String),
DELIMITER_STRINGpublic static double[] toDoubleArray(java.lang.String pString,
java.lang.String pDelimiters)
pString - The comma-separated stringpDelimiters - The delimiter stringdouble arrayjava.lang.NumberFormatException - if any of the elements are not parseable
as a doublepublic static double[] toDoubleArray(java.lang.String pString)
pString - The comma-separated stringdouble arrayjava.lang.NumberFormatException - if any of the elements are not parseable
as a doubletoDoubleArray(String,String),
DELIMITER_STRINGpublic static java.awt.Color toColor(java.lang.String pString)
java.awt.Color), like black or
red, or it can be HTML/CSS-style, on the format:
#RRGGBB, where RR, GG and BB means two digit
hexadecimal for red, green and blue values respectively.#AARRGGBB, as above, with AA as alpha component.#RGB, where R, G and B means one digit
hexadecimal for red, green and blue values respectively.#ARGB, as above, with A as alpha component.pString - the string representation of the colorColor object, or null if the argument
is nulljava.lang.IllegalArgumentException - if the string does not map to a color.Colorpublic static java.lang.String toColorString(java.awt.Color pColor)
#RRGGBB, where RR, GG and BB means two digit
hexadecimal for red, green and blue values respectively.#AARRGGBB, as above, with AA as alpha component.
Examlples: toColorString(Color.red) == "#ff0000",
toColorString(new Color(0xcc, 0xcc, 0xcc)) == "#cccccc".
pColor - the colorpublic static boolean isNumber(java.lang.String pString)
Note that there is no guarantees made, that this number can be represented as either an int or a long.
pString - The string to check.public static java.lang.String substring(java.lang.String pSource,
java.lang.String pBeginBoundaryString,
java.lang.String pEndBoundaryString,
int pOffset)
pSource - The source string.pBeginBoundaryString - The string that marks the beginning.pEndBoundaryString - The string that marks the end.pOffset - The index to start searching in the source
string. If it is less than 0, the index will be set to 0.public static java.lang.String getFirstElement(java.lang.String pSource,
java.lang.String pDelimiter)
String containing string elements delimited by a given delimiter.
NB - Straightforward implementation!pSource - The source string.pDelimiter - The delimiter used in the source string.public static java.lang.String getLastElement(java.lang.String pSource,
java.lang.String pDelimiter)
String containing string elements
delimited by a given delimiter.
NB - Straightforward implementation!pSource - The source string.pDelimiter - The delimiter used in the source string.public static java.lang.String toCSVString(java.lang.Object[] pStringArray)
pStringArray - the string arraypublic static java.lang.String toCSVString(java.lang.Object[] pStringArray,
java.lang.String pDelimiterString)
pStringArray - the string arraypDelimiterString - the delimiter stringjava.lang.IllegalArgumentException - if pDelimiterString == nullpublic static java.lang.String deepToString(java.lang.Object pObject)
pObject - the objectpublic static java.lang.String deepToString(java.lang.Object pObject,
boolean pForceDeep,
int pDepth)
pObject - the objectpDepth - the maximum depthpForceDeep - true to force deep toString, even
if object overrides toStringpublic static java.lang.String identityToString(java.lang.Object pObject)
Object.toString().pObject - the objectString on the format of
Object.toString()public boolean matches(java.lang.String pString,
java.lang.String pRegex)
throws java.util.regex.PatternSyntaxException
An invocation of this method of the form matches(str, regex) yields exactly the same result as the expression
Pattern.matches(regex, str)
pString - the stringpRegex - the regular expression to which this string is to be matchedtrue if, and only if, this string matches the
given regular expressionjava.util.regex.PatternSyntaxException - if the regular expression's syntax is invalidPattern,
String.matches(String)public java.lang.String replaceFirst(java.lang.String pString,
java.lang.String pRegex,
java.lang.String pReplacement)
An invocation of this method of the form replaceFirst(str, regex, repl) yields exactly the same result as the expression:
Pattern.compile(regex).matcher(str).replaceFirst(repl)
pString - the stringpRegex - the regular expression to which this string is to be matchedpReplacement - the replacement textStringjava.util.regex.PatternSyntaxException - if the regular expression's syntax is invalidPattern,
Matcher.replaceFirst(String)public java.lang.String replaceAll(java.lang.String pString,
java.lang.String pRegex,
java.lang.String pReplacement)
An invocation of this method of the form replaceAll(str, pRegex, repl) yields exactly the same result as the expression
Pattern.compile(pRegex).matcher(str). {@link java.util.regex.Matcher#replaceAll replaceAll}(repl)
pString - the stringpRegex - the regular expression to which this string is to be matchedpReplacement - the replacement stringStringjava.util.regex.PatternSyntaxException - if the regular expression's syntax is invalidPattern,
String.replaceAll(String,String)public java.lang.String[] split(java.lang.String pString,
java.lang.String pRegex,
int pLimit)
The array returned by this method contains each substring of this string that is terminated by another substring that matches the given expression or is terminated by the end of the string. The substrings in the array are in the order in which they occur in this string. If the expression does not match any part of the input then the resulting array has just one element, namely this string.
The pLimit parameter controls the number of times the
pattern is applied and therefore affects the length of the resulting
array. If the pLimit n is greater than zero then the pattern
will be applied at most n - 1 times, the array's
length will be no greater than n, and the array's last entry
will contain all input beyond the last matched delimiter. If n
is non-positive then the pattern will be applied as many times as
possible and the array can have any length. If n is zero then
the pattern will be applied as many times as possible, the array can
have any length, and trailing empty strings will be discarded.
An invocation of this method of the form split(str, regex, n) yields the same result as the expression:
Pattern.compile(regex).split(str, n)
pString - the stringpRegex - the delimiting regular expressionpLimit - the result threshold, as described abovejava.util.regex.PatternSyntaxException - if the regular expression's syntax is invalidPattern,
String.split(String,int)public java.lang.String[] split(java.lang.String pString,
java.lang.String pRegex)
This method works as if by invoking the two-argument
split method with the given
expression and a limit argument of zero.
Trailing empty strings are therefore not included in the resulting array.
pString - the stringpRegex - the delimiting regular expressionjava.util.regex.PatternSyntaxException - if the regular expression's syntax is invalidPattern,
String.split(String)public static java.lang.String camelToLisp(java.lang.String pString)
Eg.
"foo" => "foo",
"fooBar" => "foo-bar",
"myURL" => "my-url",
"HttpRequestWrapper" => "http-request-wrapper"
"HttpURLConnection" => "http-url-connection"
"my45Caliber" => "my-45-caliber"
"allready-lisp" => "allready-lisp"
pString - the camel-style input stringjava.lang.IllegalArgumentException - if pString == nulllispToCamel(String)public static java.lang.String lispToCamel(java.lang.String pString)
Eg.
"foo" => "foo",
"foo-bar" => "fooBar",
"http-request-wrapper" => "httpRequestWrapper"
"my-45-caliber" => "my45Caliber"
"allreadyCamel" => "allreadyCamel"
pString - the lisp-style input stringjava.lang.IllegalArgumentException - if pString == nulllispToCamel(String,boolean),
camelToLisp(String)public static java.lang.String lispToCamel(java.lang.String pString,
boolean pFirstUpperCase)
To create a string starting with a lower case letter
(like Java variable names, etc),
specify the pFirstUpperCase paramter to be false.
Eg.
"foo" => "foo",
"foo-bar" => "fooBar",
"allreadyCamel" => "allreadyCamel"
To create a string starting with an upper case letter
(like Java class name, etc),
specify the pFirstUpperCase paramter to be true.
Eg.
"http-request-wrapper" => "HttpRequestWrapper"
"my-12-monkeys" => "My12Monkeys"
pString - the lisp-style input stringpFirstUpperCase - true if the first char should be
upper casejava.lang.IllegalArgumentException - if pString == nullcamelToLisp(String)public static java.lang.String reverse(java.lang.String pString)
Copyright © 2020. All Rights Reserved.