public final class StringUtil extends Object
Modifier and Type | Field | Description |
---|---|---|
static String |
DELIMITER_STRING |
The default delimiter string, used by the
toXXXArray()
methods. |
Modifier and Type | Method | Description |
---|---|---|
static String |
camelToLisp(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 String |
capitalize(String pString) |
Makes the first letter of a String uppercase.
|
static String |
capitalize(String pString,
int pIndex) |
Makes the Nth letter of a String uppercase.
|
static boolean |
contains(String pString,
int pChar) |
Tests if a string contains a specific character.
|
static boolean |
contains(String pContainer,
String pLookFor) |
Tests if a string contains another string.
|
static boolean |
containsIgnoreCase(String pString,
int pChar) |
Tests if a string contains a specific character, ignoring case.
|
static boolean |
containsIgnoreCase(String pContainer,
String pLookFor) |
Tests if a string contains another string, ignoring case.
|
static String |
cut(String pString,
int pMaxLen,
String pPad) |
Cuts a string between two words, before a sepcified length, if the
string is longer than the maxium lenght.
|
static String |
decode(byte[] pData,
int pOffset,
int pLength,
String pCharset) |
Constructs a new
String by decoding the specified sub array of bytes using the specified charset. |
static String |
deepToString(Object pObject) |
|
static String |
deepToString(Object pObject,
boolean pForceDeep,
int pDepth) |
|
static String |
getFirstElement(String pSource,
String pDelimiter) |
Gets the first element of a
String containing string elements delimited by a given delimiter. |
static String |
getLastElement(String pSource,
String pDelimiter) |
Gets the last element of a
String containing string elements
delimited by a given delimiter. |
static String |
identityToString(Object pObject) |
Returns a string on the same format as
Object.toString() . |
static int |
indexOfIgnoreCase(String pString,
int pChar) |
Returns the index within this string of the first occurrence of the
specified character.
|
static int |
indexOfIgnoreCase(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(String pString,
String pLookFor) |
Returns the index within this string of the first occurrence of the
specified substring.
|
static int |
indexOfIgnoreCase(String pString,
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(String pString) |
Tests if a String is null, or contains nothing but white-space.
|
static boolean |
isEmpty(String[] pStringArray) |
Tests a string array, to see if all items are null or an empty string.
|
static boolean |
isNumber(String pString) |
Tests a string, to see if it is an number (element of Z).
|
static int |
lastIndexOfIgnoreCase(String pString,
int pChar) |
Returns the index within this string of the last occurrence of the
specified character.
|
static int |
lastIndexOfIgnoreCase(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(String pString,
String pLookFor) |
Returns the index within this string of the rightmost occurrence of the
specified substring.
|
static int |
lastIndexOfIgnoreCase(String pString,
String pLookFor,
int pPos) |
Returns the index within this string of the rightmost occurrence of the
specified substring.
|
static String |
lispToCamel(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 String |
lispToCamel(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 String |
ltrim(String pString) |
Trims the argument string for whitespace on the left side only.
|
boolean |
matches(String pString,
String pRegex) |
Tells whether or not the given string string matches the given regular
expression.
|
static String |
pad(String pSource,
int pRequiredLength,
String pPadString,
boolean pPrepend) |
String length check with simple concatenation of selected pad-string.
|
static String |
replace(String pSource,
String pPattern,
String pReplace) |
Replaces a substring of a string with another string.
|
String |
replaceAll(String pString,
String pRegex,
String pReplacement) |
Replaces each substring of this string that matches the given
regular expression with the given pReplacement.
|
String |
replaceFirst(String pString,
String pRegex,
String pReplacement) |
Replaces the first substring of the given string that matches the given
regular expression with the given pReplacement.
|
static String |
replaceIgnoreCase(String pSource,
String pPattern,
String pReplace) |
Replaces a substring of a string with another string, ignoring case.
|
static String |
reverse(String pString) |
|
static String |
rtrim(String pString) |
Trims the argument string for whitespace on the right side only.
|
String[] |
split(String pString,
String pRegex) |
Splits this string around matches of the given regular expression.
|
String[] |
split(String pString,
String pRegex,
int pLimit) |
Splits this string around matches of the given regular expression.
|
static String |
substring(String pSource,
String pBeginBoundaryString,
String pEndBoundaryString,
int pOffset) |
Gets the first substring between the given string boundaries.
|
static Color |
toColor(String pString) |
Parses a string to a Color.
|
static String |
toColorString(Color pColor) |
Creates a HTML/CSS String representation of the given color.
|
static String |
toCSVString(Object[] pStringArray) |
Converts a string array to a string of comma-separated values.
|
static String |
toCSVString(Object[] pStringArray,
String pDelimiterString) |
Converts a string array to a string separated by the given delimiter.
|
static Date |
toDate(String pString) |
Converts the string to a date, using the default date format.
|
static Date |
toDate(String pString,
String pFormat) |
Converts the string to a date, using the given format.
|
static Date |
toDate(String pString,
DateFormat pFormat) |
Converts the string to a date, using the given format.
|
static double[] |
toDoubleArray(String pString) |
Converts a comma-separated String to an array of doubles.
|
static double[] |
toDoubleArray(String pString,
String pDelimiters) |
Converts a comma-separated String to an array of doubles.
|
static int[] |
toIntArray(String pString) |
Converts a comma-separated String to an array of ints.
|
static int[] |
toIntArray(String pString,
String pDelimiters) |
Converts a comma-separated String to an array of ints.
|
static int[] |
toIntArray(String pString,
String pDelimiters,
int pBase) |
Converts a comma-separated String to an array of ints.
|
static long[] |
toLongArray(String pString) |
Converts a comma-separated String to an array of longs.
|
static long[] |
toLongArray(String pString,
String pDelimiters) |
Converts a comma-separated String to an array of longs.
|
static String |
toLowerCase(String pString) |
Converts a string to lowercase.
|
static String[] |
toStringArray(String pString) |
Converts a comma-separated String to an array of Strings.
|
static String[] |
toStringArray(String pString,
String pDelimiters) |
Converts a delimiter separated String to an array of Strings.
|
static Timestamp |
toTimestamp(String pValue) |
Converts the string to a jdbc Timestamp, using the standard Timestamp
escape format.
|
static String |
toUpperCase(String pString) |
Converts a string to uppercase.
|
static String |
valueOf(Object pObj) |
Returns the value of the given
Object , as a String . |
public static final String DELIMITER_STRING
toXXXArray()
methods.
Its value is ", \t\n\r\f"
.
public static String decode(byte[] pData, int pOffset, int pLength, 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 setUnsupportedCharsetException
String(byte[], int, int, String)
public static String valueOf(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 String toUpperCase(String pString)
pString
- the string to convertpublic static String toLowerCase(String pString)
pString
- the string to convertpublic static boolean isEmpty(String pString)
pString
- The string to testpublic static boolean isEmpty(String[] pStringArray)
pStringArray
- The string array to check.public static boolean contains(String pContainer, 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(String pContainer, 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(String pString, int pChar)
pString
- The string to check.pChar
- The character to search for.public static boolean containsIgnoreCase(String pString, int pChar)
pString
- The string to check.pChar
- The character to search for.public static int indexOfIgnoreCase(String pString, String pLookFor)
pString
- The string to testpLookFor
- The string to look forString.indexOf(String)
public static int indexOfIgnoreCase(String pString, 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(String pString, String pLookFor)
pString.length() - 1
.pString
- The string to testpLookFor
- The string to look forString.lastIndexOf(String)
public static int lastIndexOfIgnoreCase(String pString, String pLookFor, int pPos)
pPos
pString
- The string to testpLookFor
- The string to look forpPos
- The last index to testString.lastIndexOf(String,int)
public static int indexOfIgnoreCase(String pString, int pChar)
pString
- The string to testpChar
- The character to look forString.indexOf(int)
public static int indexOfIgnoreCase(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(String pString, int pChar)
pString
- The string to testpChar
- The character to look forString.lastIndexOf(int)
public static int lastIndexOfIgnoreCase(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 String ltrim(String pString)
pString
- the string to trimnull
if
the string argument is null
.rtrim(java.lang.String)
,
String.trim()
public static String rtrim(String pString)
pString
- the string to trimnull
if
the string argument is null
.ltrim(java.lang.String)
,
String.trim()
public static String replace(String pSource, String pPattern, String pReplace)
pSource
- The source StringpPattern
- The pattern to replacepReplace
- The new String to be inserted instead of the
replace Stringpublic static String replaceIgnoreCase(String pSource, String pPattern, 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 String cut(String pString, int pMaxLen, 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 String capitalize(String pString, int pIndex)
pString
- The string to capitalizepIndex
- The base-0 index of the char to capitalize.public static String capitalize(String pString)
pString
- The string to capitalizepublic static String pad(String pSource, int pRequiredLength, 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 Date toDate(String pString)
pString
- the string to convertDateFormat
,
DateFormat.getInstance()
public static Date toDate(String pString, String pFormat)
pString
- the string to convertpFormat
- the date formatSimpleDateFormat
,
SimpleDateFormat(String)
public static Date toDate(String pString, DateFormat pFormat)
pString
- the string to convertpFormat
- the date formatSimpleDateFormat
,
SimpleDateFormat(String)
,
DateFormat
public static Timestamp toTimestamp(String pValue)
pValue
- the valueTimestamp
Timestamp
,
Timestamp.valueOf(String)
public static String[] toStringArray(String pString, String pDelimiters)
pString
- The comma-separated stringpDelimiters
- The delimiter stringString
array containing the delimiter separated elementspublic static String[] toStringArray(String pString)
pString
- The comma-separated stringString
array containing the comma-separated elementstoStringArray(String,String)
public static int[] toIntArray(String pString, String pDelimiters, int pBase)
pString
- The comma-separated stringpDelimiters
- The delimiter stringpBase
- The radixint
arrayNumberFormatException
- if any of the elements are not parseable
as an intpublic static int[] toIntArray(String pString)
pString
- The comma-separated stringint
arrayNumberFormatException
- if any of the elements are not parseable
as an inttoStringArray(String,String)
,
DELIMITER_STRING
public static int[] toIntArray(String pString, String pDelimiters)
pString
- The comma-separated stringpDelimiters
- The delimiter stringint
arrayNumberFormatException
- if any of the elements are not parseable
as an inttoIntArray(String,String)
public static long[] toLongArray(String pString, String pDelimiters)
pString
- The comma-separated stringpDelimiters
- The delimiter stringlong
arrayNumberFormatException
- if any of the elements are not parseable
as a longpublic static long[] toLongArray(String pString)
pString
- The comma-separated stringlong
arrayNumberFormatException
- if any of the elements are not parseable
as a longtoStringArray(String,String)
,
DELIMITER_STRING
public static double[] toDoubleArray(String pString, String pDelimiters)
pString
- The comma-separated stringpDelimiters
- The delimiter stringdouble
arrayNumberFormatException
- if any of the elements are not parseable
as a doublepublic static double[] toDoubleArray(String pString)
pString
- The comma-separated stringdouble
arrayNumberFormatException
- if any of the elements are not parseable
as a doubletoDoubleArray(String,String)
,
DELIMITER_STRING
public static Color toColor(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 null
IllegalArgumentException
- if the string does not map to a color.Color
public static String toColorString(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.toColorString(Color.red) == "#ff0000"
,
toColorString(new Color(0xcc, 0xcc, 0xcc)) == "#cccccc"
.pColor
- the colorpublic static boolean isNumber(String pString)
pString
- The string to check.public static String substring(String pSource, String pBeginBoundaryString, 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 String getFirstElement(String pSource, 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 String getLastElement(String pSource, 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 String toCSVString(Object[] pStringArray)
pStringArray
- the string arraypublic static String toCSVString(Object[] pStringArray, String pDelimiterString)
pStringArray
- the string arraypDelimiterString
- the delimiter stringIllegalArgumentException
- if pDelimiterString == null
public static String deepToString(Object pObject)
pObject
- the objectpublic static String deepToString(Object pObject, boolean pForceDeep, int pDepth)
pObject
- the objectpDepth
- the maximum depthpForceDeep
- true
to force deep toString
, even
if object overrides toStringpublic static String identityToString(Object pObject)
Object.toString()
.pObject
- the objectString
on the format of
Object.toString()
public boolean matches(String pString, String pRegex) throws PatternSyntaxException
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 expressionPatternSyntaxException
- if the regular expression's syntax is invalidPattern
,
String.matches(String)
public String replaceFirst(String pString, String pRegex, String pReplacement)
Pattern
.compile
(regex).matcher
(str).replaceFirst
(repl)
pString
- the stringpRegex
- the regular expression to which this string is to be matchedpReplacement
- the replacement textString
PatternSyntaxException
- if the regular expression's syntax is invalidPattern
,
Matcher.replaceFirst(String)
public String replaceAll(String pString, String pRegex, String pReplacement)
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 stringString
PatternSyntaxException
- if the regular expression's syntax is invalidPattern
,
String.replaceAll(String,String)
public String[] split(String pString, String pRegex, int pLimit)
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 abovePatternSyntaxException
- if the regular expression's syntax is invalidPattern
,
String.split(String,int)
public String[] split(String pString, String pRegex)
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 expressionPatternSyntaxException
- if the regular expression's syntax is invalidPattern
,
String.split(String)
public static String camelToLisp(String pString)
"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 stringIllegalArgumentException
- if pString == null
lispToCamel(String)
public static String lispToCamel(String pString)
"foo" => "foo"
,
"foo-bar" => "fooBar"
,
"http-request-wrapper" => "httpRequestWrapper"
"my-45-caliber" => "my45Caliber"
"allreadyCamel" => "allreadyCamel"
pString
- the lisp-style input stringIllegalArgumentException
- if pString == null
lispToCamel(String,boolean)
,
camelToLisp(String)
public static String lispToCamel(String pString, boolean pFirstUpperCase)
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-45-caliber" => "My45Caliber"
pString
- the lisp-style input stringpFirstUpperCase
- true
if the first char should be
upper caseIllegalArgumentException
- if pString == null
camelToLisp(String)
Copyright © 2018. All rights reserved.