Class StringUtil
- java.lang.Object
-
- com.twelvemonkeys.lang.StringUtil
-
public final class StringUtil extends java.lang.Object
A utility class with some useful string manipulation methods.- Version:
- $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/main/java/com/twelvemonkeys/lang/StringUtil.java#2 $ return values, null-value handling and parameter names (cosmetics).
- Author:
- Harald Kuhr, Eirik Torske, last modified by $Author: haku $
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
DELIMITER_STRING
The default delimiter string, used by thetoXXXArray()
methods.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method 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 newString
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 aString
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 aString
containing string elements delimited by a given delimiter.static java.lang.String
identityToString(java.lang.Object pObject)
Returns a string on the same format asObject.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.lang.String pFormat)
Converts the string to a date, using the given 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 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 givenObject
, as aString
.
-
-
-
Method Detail
-
decode
public static java.lang.String decode(byte[] pData, int pOffset, int pLength, java.lang.String pCharset)
Constructs a newString
by decoding the specified sub array of bytes using the specified charset. Replacement fornew String(byte[], int, int, String)
, that does not throw the checkedUnsupportedEncodingException
, but instead the uncheckedUnsupportedCharsetException
if the character set is not supported.- Parameters:
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 set- Returns:
- a newly created string.
- Throws:
java.nio.charset.UnsupportedCharsetException
- See Also:
String(byte[], int, int, String)
-
valueOf
public static java.lang.String valueOf(java.lang.Object pObj)
Returns the value of the givenObject
, as aString
. Unlike String.valueOf, this method returnsnull
instead of theString
"null", ifnull
is given as the argument.- Parameters:
pObj
- the Object to find theString
value of.- Returns:
- the String value of the given object, or
null
if thepObj
==null
. - See Also:
String.valueOf(Object)
,String.toString()
-
toUpperCase
public static java.lang.String toUpperCase(java.lang.String pString)
Converts a string to uppercase.- Parameters:
pString
- the string to convert- Returns:
- the string converted to uppercase, or null if the argument was null.
-
toLowerCase
public static java.lang.String toLowerCase(java.lang.String pString)
Converts a string to lowercase.- Parameters:
pString
- the string to convert- Returns:
- the string converted to lowercase, or null if the argument was null.
-
isEmpty
public static boolean isEmpty(java.lang.String pString)
Tests if a String is null, or contains nothing but white-space.- Parameters:
pString
- The string to test- Returns:
- true if the string is null or contains only whitespace, otherwise false.
-
isEmpty
public static boolean isEmpty(java.lang.String[] pStringArray)
Tests a string array, to see if all items are null or an empty string.- Parameters:
pStringArray
- The string array to check.- Returns:
- true if the string array is null or only contains string items that are null or contain only whitespace, otherwise false.
-
contains
public static boolean contains(java.lang.String pContainer, java.lang.String pLookFor)
Tests if a string contains another string.- Parameters:
pContainer
- The string to testpLookFor
- The string to look for- Returns:
true
if the container string is contains the string, and both parameters are non-null
, otherwisefalse
.
-
containsIgnoreCase
public static boolean containsIgnoreCase(java.lang.String pContainer, java.lang.String pLookFor)
Tests if a string contains another string, ignoring case.- Parameters:
pContainer
- The string to testpLookFor
- The string to look for- Returns:
true
if the container string is contains the string, and both parameters are non-null
, otherwisefalse
.- See Also:
contains(String,String)
-
contains
public static boolean contains(java.lang.String pString, int pChar)
Tests if a string contains a specific character.- Parameters:
pString
- The string to check.pChar
- The character to search for.- Returns:
- true if the string contains the specific character.
-
containsIgnoreCase
public static boolean containsIgnoreCase(java.lang.String pString, int pChar)
Tests if a string contains a specific character, ignoring case.- Parameters:
pString
- The string to check.pChar
- The character to search for.- Returns:
- true if the string contains the specific character.
-
indexOfIgnoreCase
public 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.- Parameters:
pString
- The string to testpLookFor
- The string to look for- Returns:
- if the string argument occurs as a substring within this object, 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.
- See Also:
String.indexOf(String)
-
indexOfIgnoreCase
public 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.- Parameters:
pString
- The string to testpLookFor
- The string to look forpPos
- The first index to test- Returns:
- if the string argument occurs as a substring within this object, 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.
- See Also:
String.indexOf(String,int)
-
lastIndexOfIgnoreCase
public 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. The rightmost empty string "" is considered to occur at the index valuepString.length() - 1
.- Parameters:
pString
- The string to testpLookFor
- The string to look for- Returns:
- If the string argument occurs one or more times as a substring within this object at a starting index no greater than fromIndex, then the index of the first character of the last such substring is returned. If it does not occur as a substring starting at fromIndex or earlier, -1 is returned.
- See Also:
String.lastIndexOf(String)
-
lastIndexOfIgnoreCase
public 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. The rightmost empty string "" is considered to occur at the index valuepPos
- Parameters:
pString
- The string to testpLookFor
- The string to look forpPos
- The last index to test- Returns:
- If the string argument occurs one or more times as a substring within this object at a starting index no greater than fromIndex, then the index of the first character of the last such substring is returned. If it does not occur as a substring starting at fromIndex or earlier, -1 is returned.
- See Also:
String.lastIndexOf(String,int)
-
indexOfIgnoreCase
public static int indexOfIgnoreCase(java.lang.String pString, int pChar)
Returns the index within this string of the first occurrence of the specified character.- Parameters:
pString
- The string to testpChar
- The character to look for- Returns:
- if the string argument occurs as a substring within this object, 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.
- See Also:
String.indexOf(int)
-
indexOfIgnoreCase
public 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.- Parameters:
pString
- The string to testpChar
- The character to look forpPos
- The first index to test- Returns:
- if the string argument occurs as a substring within this object, 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.
- See Also:
String.indexOf(int,int)
-
lastIndexOfIgnoreCase
public static int lastIndexOfIgnoreCase(java.lang.String pString, int pChar)
Returns the index within this string of the last occurrence of the specified character.- Parameters:
pString
- The string to testpChar
- The character to look for- Returns:
- if the string argument occurs as a substring within this object, 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.
- See Also:
String.lastIndexOf(int)
-
lastIndexOfIgnoreCase
public 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.- Parameters:
pString
- The string to testpChar
- The character to look forpPos
- The last index to test- Returns:
- if the string argument occurs as a substring within this object, 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.
- See Also:
String.lastIndexOf(int,int)
-
ltrim
public static java.lang.String ltrim(java.lang.String pString)
Trims the argument string for whitespace on the left side only.- Parameters:
pString
- the string to trim- Returns:
- the string with no whitespace on the left, or
null
if the string argument isnull
. - See Also:
rtrim(java.lang.String)
,String.trim()
-
rtrim
public static java.lang.String rtrim(java.lang.String pString)
Trims the argument string for whitespace on the right side only.- Parameters:
pString
- the string to trim- Returns:
- the string with no whitespace on the right, or
null
if the string argument isnull
. - See Also:
ltrim(java.lang.String)
,String.trim()
-
replace
public 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. All matches are replaced.- Parameters:
pSource
- The source StringpPattern
- The pattern to replacepReplace
- The new String to be inserted instead of the replace String- Returns:
- The new String with the pattern replaced
-
replaceIgnoreCase
public 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. All matches are replaced.- Parameters:
pSource
- The source StringpPattern
- The pattern to replacepReplace
- The new String to be inserted instead of the replace String- Returns:
- The new String with the pattern replaced
- See Also:
replace(String,String,String)
-
cut
public 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. The string is optionally padded with the pad argument. The method assumes words to be separated by the space character (" "). Note that the maximum length argument is absolute, and will also include the length of the padding.- Parameters:
pString
- The string to cutpMaxLen
- The maximum length before cuttingpPad
- The string to append at the end, aftrer cutting- Returns:
- The cutted string with padding, or the original string, if it was shorter than the max length.
- See Also:
pad(String,int,String,boolean)
-
capitalize
public static java.lang.String capitalize(java.lang.String pString, int pIndex)
Makes the Nth letter of a String uppercase. If the index is outside the the length of the argument string, the argument is simply returned.- Parameters:
pString
- The string to capitalizepIndex
- The base-0 index of the char to capitalize.- Returns:
- The capitalized string, or null, if a null argument was given.
-
capitalize
public static java.lang.String capitalize(java.lang.String pString)
Makes the first letter of a String uppercase.- Parameters:
pString
- The string to capitalize- Returns:
- The capitalized string, or null, if a null argument was given.
-
pad
public 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. E.g. a zip number from 123 to the correct 0123.- Parameters:
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)- Returns:
- a concatenated string.
- See Also:
cut(String,int,String)
-
toDate
public static java.util.Date toDate(java.lang.String pString)
Converts the string to a date, using the default date format.- Parameters:
pString
- the string to convert- Returns:
- the date
- See Also:
DateFormat
,DateFormat.getInstance()
-
toDate
public static java.util.Date toDate(java.lang.String pString, java.lang.String pFormat)
Converts the string to a date, using the given format.- Parameters:
pString
- the string to convertpFormat
- the date format- Returns:
- the date
- See Also:
SimpleDateFormat
,SimpleDateFormat(String)
-
toDate
public static java.util.Date toDate(java.lang.String pString, java.text.DateFormat pFormat)
Converts the string to a date, using the given format.- Parameters:
pString
- the string to convertpFormat
- the date format- Returns:
- the date
- See Also:
SimpleDateFormat
,SimpleDateFormat(String)
,DateFormat
-
toTimestamp
public static java.sql.Timestamp toTimestamp(java.lang.String pValue)
Converts the string to a jdbc Timestamp, using the standard Timestamp escape format.- Parameters:
pValue
- the value- Returns:
- a new
Timestamp
- See Also:
Timestamp
,Timestamp.valueOf(String)
-
toStringArray
public static java.lang.String[] toStringArray(java.lang.String pString, java.lang.String pDelimiters)
Converts a delimiter separated String to an array of Strings.- Parameters:
pString
- The comma-separated stringpDelimiters
- The delimiter string- Returns:
- a
String
array containing the delimiter separated elements
-
toStringArray
public static java.lang.String[] toStringArray(java.lang.String pString)
Converts a comma-separated String to an array of Strings.- Parameters:
pString
- The comma-separated string- Returns:
- a
String
array containing the comma-separated elements - See Also:
toStringArray(String,String)
-
toIntArray
public static int[] toIntArray(java.lang.String pString, java.lang.String pDelimiters, int pBase)
Converts a comma-separated String to an array of ints.- Parameters:
pString
- The comma-separated stringpDelimiters
- The delimiter stringpBase
- The radix- Returns:
- an
int
array - Throws:
java.lang.NumberFormatException
- if any of the elements are not parseable as an int
-
toIntArray
public static int[] toIntArray(java.lang.String pString)
Converts a comma-separated String to an array of ints.- Parameters:
pString
- The comma-separated string- Returns:
- an
int
array - Throws:
java.lang.NumberFormatException
- if any of the elements are not parseable as an int- See Also:
toStringArray(String,String)
,DELIMITER_STRING
-
toIntArray
public static int[] toIntArray(java.lang.String pString, java.lang.String pDelimiters)
Converts a comma-separated String to an array of ints.- Parameters:
pString
- The comma-separated stringpDelimiters
- The delimiter string- Returns:
- an
int
array - Throws:
java.lang.NumberFormatException
- if any of the elements are not parseable as an int- See Also:
toIntArray(String,String)
-
toLongArray
public static long[] toLongArray(java.lang.String pString, java.lang.String pDelimiters)
Converts a comma-separated String to an array of longs.- Parameters:
pString
- The comma-separated stringpDelimiters
- The delimiter string- Returns:
- a
long
array - Throws:
java.lang.NumberFormatException
- if any of the elements are not parseable as a long
-
toLongArray
public static long[] toLongArray(java.lang.String pString)
Converts a comma-separated String to an array of longs.- Parameters:
pString
- The comma-separated string- Returns:
- a
long
array - Throws:
java.lang.NumberFormatException
- if any of the elements are not parseable as a long- See Also:
toStringArray(String,String)
,DELIMITER_STRING
-
toDoubleArray
public static double[] toDoubleArray(java.lang.String pString, java.lang.String pDelimiters)
Converts a comma-separated String to an array of doubles.- Parameters:
pString
- The comma-separated stringpDelimiters
- The delimiter string- Returns:
- a
double
array - Throws:
java.lang.NumberFormatException
- if any of the elements are not parseable as a double
-
toDoubleArray
public static double[] toDoubleArray(java.lang.String pString)
Converts a comma-separated String to an array of doubles.- Parameters:
pString
- The comma-separated string- Returns:
- a
double
array - Throws:
java.lang.NumberFormatException
- if any of the elements are not parseable as a double- See Also:
toDoubleArray(String,String)
,DELIMITER_STRING
-
toColor
public static java.awt.Color toColor(java.lang.String pString)
Parses a string to a Color. The argument can be a color constant (static constant fromjava.awt.Color
), likeblack
orred
, 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.
- Parameters:
pString
- the string representation of the color- Returns:
- the
Color
object, ornull
if the argument isnull
- Throws:
java.lang.IllegalArgumentException
- if the string does not map to a color.- See Also:
Color
-
toColorString
public static java.lang.String toColorString(java.awt.Color pColor)
Creates a HTML/CSS String representation of the given color. The HTML/CSS color format is defined as:#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"
.- Parameters:
pColor
- the color- Returns:
- A String representation of the color on HTML/CSS form
-
isNumber
public static boolean isNumber(java.lang.String pString)
Tests a string, to see if it is an number (element of Z). Valid integers are positive natural numbers (1, 2, 3, ...), their negatives (?1, ?2, ?3, ...) and the number zero.Note that there is no guarantees made, that this number can be represented as either an int or a long.
- Parameters:
pString
- The string to check.- Returns:
- true if the String is a natural number.
-
substring
public 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.- Parameters:
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.- Returns:
- the substring demarcated by the given string boundaries or null if not both string boundaries are found.
-
getFirstElement
public static java.lang.String getFirstElement(java.lang.String pSource, java.lang.String pDelimiter)
Gets the first element of aString
containing string elements delimited by a given delimiter. NB - Straightforward implementation!- Parameters:
pSource
- The source string.pDelimiter
- The delimiter used in the source string.- Returns:
- The last string element.
-
getLastElement
public static java.lang.String getLastElement(java.lang.String pSource, java.lang.String pDelimiter)
Gets the last element of aString
containing string elements delimited by a given delimiter. NB - Straightforward implementation!- Parameters:
pSource
- The source string.pDelimiter
- The delimiter used in the source string.- Returns:
- The last string element.
-
toCSVString
public static java.lang.String toCSVString(java.lang.Object[] pStringArray)
Converts a string array to a string of comma-separated values.- Parameters:
pStringArray
- the string array- Returns:
- A string of comma-separated values
-
toCSVString
public 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.- Parameters:
pStringArray
- the string arraypDelimiterString
- the delimiter string- Returns:
- string of delimiter separated values
- Throws:
java.lang.IllegalArgumentException
- ifpDelimiterString == null
-
deepToString
public static java.lang.String deepToString(java.lang.Object pObject)
- Parameters:
pObject
- the object- Returns:
- a deep string representation of the given object
-
deepToString
public static java.lang.String deepToString(java.lang.Object pObject, boolean pForceDeep, int pDepth)
- Parameters:
pObject
- the objectpDepth
- the maximum depthpForceDeep
-true
to force deeptoString
, even if object overrides toString- Returns:
- a deep string representation of the given object
-
identityToString
public static java.lang.String identityToString(java.lang.Object pObject)
Returns a string on the same format asObject.toString()
.- Parameters:
pObject
- the object- Returns:
- the object as a
String
on the format ofObject.toString()
-
matches
public boolean matches(java.lang.String pString, java.lang.String pRegex) throws java.util.regex.PatternSyntaxException
Tells whether or not the given string string matches the given regular expression.An invocation of this method of the form matches(str, regex) yields exactly the same result as the expression
Pattern
.matches
(regex, str)- Parameters:
pString
- the stringpRegex
- the regular expression to which this string is to be matched- Returns:
true
if, and only if, this string matches the given regular expression- Throws:
java.util.regex.PatternSyntaxException
- if the regular expression's syntax is invalid- See Also:
Pattern
,String.matches(String)
-
replaceFirst
public 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.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)- Parameters:
pString
- the stringpRegex
- the regular expression to which this string is to be matchedpReplacement
- the replacement text- Returns:
- The resulting
String
- Throws:
java.util.regex.PatternSyntaxException
- if the regular expression's syntax is invalid- See Also:
Pattern
,Matcher.replaceFirst(String)
-
replaceAll
public 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.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)
- Parameters:
pString
- the stringpRegex
- the regular expression to which this string is to be matchedpReplacement
- the replacement string- Returns:
- The resulting
String
- Throws:
java.util.regex.PatternSyntaxException
- if the regular expression's syntax is invalid- See Also:
Pattern
,String.replaceAll(String,String)
-
split
public java.lang.String[] split(java.lang.String pString, java.lang.String pRegex, int pLimit)
Splits this string around matches of the given regular expression.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)- Parameters:
pString
- the stringpRegex
- the delimiting regular expressionpLimit
- the result threshold, as described above- Returns:
- the array of strings computed by splitting this string around matches of the given regular expression
- Throws:
java.util.regex.PatternSyntaxException
- if the regular expression's syntax is invalid- See Also:
Pattern
,String.split(String,int)
-
split
public java.lang.String[] split(java.lang.String pString, java.lang.String pRegex)
Splits this string around matches of the given regular expression.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.- Parameters:
pString
- the stringpRegex
- the delimiting regular expression- Returns:
- the array of strings computed by splitting this string around matches of the given regular expression
- Throws:
java.util.regex.PatternSyntaxException
- if the regular expression's syntax is invalid- See Also:
Pattern
,String.split(String)
-
camelToLisp
public 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). Other characters in the string are left untouched.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"
- Parameters:
pString
- the camel-style input string- Returns:
- the string converted to lisp-style naming convention
- Throws:
java.lang.IllegalArgumentException
- ifpString == null
- See Also:
lispToCamel(String)
-
lispToCamel
public 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. Other characters in the string are left untouched.Eg.
"foo" => "foo"
,"foo-bar" => "fooBar"
,"http-request-wrapper" => "httpRequestWrapper"
"my-45-caliber" => "my45Caliber"
"allreadyCamel" => "allreadyCamel"
- Parameters:
pString
- the lisp-style input string- Returns:
- the string converted to camel-style
- Throws:
java.lang.IllegalArgumentException
- ifpString == null
- See Also:
lispToCamel(String,boolean)
,camelToLisp(String)
-
lispToCamel
public 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. Other characters in the string are left untouched.To create a string starting with a lower case letter (like Java variable names, etc), specify the
pFirstUpperCase
paramter to befalse
. 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 betrue
. Eg."http-request-wrapper" => "HttpRequestWrapper"
"my-12-monkeys" => "My12Monkeys"
- Parameters:
pString
- the lisp-style input stringpFirstUpperCase
-true
if the first char should be upper case- Returns:
- the string converted to camel-style
- Throws:
java.lang.IllegalArgumentException
- ifpString == null
- See Also:
camelToLisp(String)
-
reverse
public static java.lang.String reverse(java.lang.String pString)
-
-