public class StringTools extends Object
Modifier and Type | Field and Description |
---|---|
static String |
BOOLEAN_FALSE
String variable 'false'
|
static String |
BOOLEAN_NO
String variable 'on'
|
static String |
BOOLEAN_OFF
String variable 'off'
|
static String |
BOOLEAN_ON
String variable 'on'
|
static String |
BOOLEAN_TRUE
String variable 'true'
|
static String |
BOOLEAN_YES
String variable 'yes'
|
static char |
CHR_BACK_SLASH
Standard char back slash
|
static char |
CHR_DOT
Standard char dot
|
static char |
CHR_FORWARD_SLASH
Standard char forward slash
|
static char |
CHR_TAB
Standard char tab
|
static String |
LINE_SEPARATOR
Cached line separator to concatenate strings
|
static int |
NO_ACTION
Values used in
truncateArray(java.lang.Object[], int[]) to specify that no
truncation should be made |
static String |
STR_AND
Standard string ampersand
|
static String |
STR_COMMA
Standard string comma
|
static String |
STR_EMPTY |
static String |
STR_EQUALS
Standard string equals
|
static String |
STR_FAILED
Standard string for failed
|
static String |
STR_QUESTION
Standard string question mark
|
static String |
STR_SEMICOLON
Standard string semicolon
|
static String |
STR_SPACE
Standard string space
|
static String |
STR_SUCCESSFUL
Standard string for successful
|
protected static String |
STRING_TOOLS_TLS_KEY
For TLS
|
static String |
SYSTEM_ENCODING
Standard system encoding (UTF-8)
|
Constructor and Description |
---|
StringTools() |
Modifier and Type | Method and Description |
---|---|
static String |
capitalizeName(String name)
Capitalizes name string
|
static String |
change(String s,
String oldp,
String newp)
Changes occurrences of a specified pattern to a replacement string.
|
static String |
change(String s,
String oldp,
String newp,
boolean ignoreCase)
Changes occurrences of a specified pattern to a replacement string.
|
static String |
change(String s,
String oldp,
String newp,
int start)
Changes occurrences of a specified pattern to a replacement string.
|
static String |
change(String s,
String oldp,
String newp,
int start,
boolean ignoreCase)
Changes occurrences of a specified pattern to a replacement string.
|
static String |
change(String s,
String oldp,
String newp,
int start,
int maxChanges)
Changes occurrences of a specified pattern to a replacement string.
|
static String |
change(String s,
String oldp,
String newp,
int start,
int maxChanges,
boolean ignoreCase)
Changes occurrences of a specified pattern to a replacement string.
|
static String |
charStr(char character,
int count)
Creates a string consisting of the given character
|
static void |
checkValueForControlCharacters(String key,
String value) |
static int |
compareStrings(String s1,
String s2,
boolean ignoreCase)
Compare two strings, always treating null as greater than
|
static String |
concat(String s1,
String s2)
Fast 2 string concatenation using
StringBuffer . |
static String |
concat(String s1,
String s2,
String s3)
Fast 3 strings concatenation using
StringBuffer |
static String |
concat(String s1,
String s2,
String s3,
String s4)
Fast 4 string concatenation using
StringBuffer |
static Set<String> |
csvToSet(String csvLine)
Convert comma-separated line to
Set of strings. |
static String |
ensureLeading(String value,
String prefix)
Ensures that string starts with specified symbol
|
static String |
ensureTrailing(String value,
String suffix)
Ensures that string ends with specified symbol
|
static boolean |
equals(char c1,
char c2,
boolean ignoreCase)
Checks if two characters are equal.
|
static boolean |
equals(String s1,
String s2,
boolean ignoreCase)
Checks if two strings are equal.
|
static long |
fastParseLong(String str)
Fast implementation of
Long.parseLong(java.lang.String, int)
It assumes that it parses decimal radix number
It doesn't not check Long constraints
It doesn't handle unicode string
|
static String[] |
getArrayFromCsvString(String csv)
Parses comma (",") delimited string to string array.
|
static boolean[] |
getBooleanArrayFromCsv(String data)
Parses csv string and returns array of boolean as a result of parsing.
|
static byte[] |
getByteArrayFromCsv(String data)
Parses csv string and returns array of boolean as a result of parsing.
|
static String |
getCsvFromList(List<?> list)
Constructs string from list of values
|
static ArrayList<String> |
getDelimitedList(String value,
String delims)
Get
ArrayList of tokens from delimited original string |
static ArrayList<String> |
getDelimitedList(String value,
String delims,
boolean includeDelim)
Get
ArrayList of tokens from delimited original string |
static double[] |
getDoubleArrayFromCsv(String data)
Parses csv string and returns array of double as a result of parsing.
|
static String |
getFirstSegment(String value,
String separator)
Get first string segment separated by passed separator
|
static float[] |
getFloatArrayFromCsv(String data)
Parses csv string and returns array of float as a result of parsing.
|
static String |
getFullStackTrace(Throwable t)
Utility function to construct full exception stack trace information.
|
static int[] |
getIntArrayFromCsv(String data)
Parses csv string and returns array of ints as a result of parsing.
|
static int[] |
getIntArrayFromDelimitedString(String data,
String delimiter)
Parses string with the specified delimiter and returns array of ints as a
result of parsing.
|
static String |
getLastSegment(String value,
String separator)
Get last string segment separated by passed separator
|
static String |
getLastSegment(String value,
String separator,
boolean includeSeperator)
Get last string segment separated by passed separator
|
static List<String> |
getListFromCsv(String value)
|
static List<String> |
getListFromCsv(String value,
boolean allowDups)
|
static List<String> |
getListFromDelimitedString(String data,
String delimiter)
Parses data with the specified delimiter
|
static long[] |
getLongArrayFromCsv(String data)
Parses csv string and returns array of long as a result of parsing.
|
static List<Long> |
getLongsListFromCsv(String value)
|
static String |
getSegment(String str,
String separator,
int segment)
Returns the segment, null if not found, first segment is segment 0
|
static short[] |
getShortArrayFromCsv(String data)
Parses csv string and returns array of short as a result of parsing.
|
static String |
getStackTrace(Throwable t)
Utility function to recieve exception stack trace as a string
|
static String |
hasSuccess(Object instance)
Checks instance to be not null, and returns either success or failed
string
|
static String |
humanizeName(String name)
Converts identifiers like "myJavaCompliantName123" into "My Java Compliant Name 123"
|
static int |
indexOf(String s,
String c,
int start,
boolean ignoreCase)
Returns the index of the first ocurrence of the specified string.
|
static String |
intToHex(int value,
boolean padding)
Convert integer to hex, optionally padding with 0s
|
static boolean |
isEmpty(String str)
Verifies whether passed string is empty or not.
|
static boolean |
isEmpty(String s1,
String s2)
Checks whether one of the 2 strings is empty
|
static boolean |
isEmptyString(Object obj)
Verifies whether passed string is a String or String[] and whether empty or not.
|
static boolean |
isFloat(String value)
Checks whether specified value convertable to float
|
static boolean |
isFormatCharsOnly(String value)
Verifies that string contains only formatting characters
|
static String |
loadStringFromReader(Reader reader)
Loads and return string from a reader.
|
static String |
loadStringfromStream(InputStream input)
Loads and return string from an
InputStream |
static String |
lowerCaseFirstChar(String s)
Lower cases first character of a string
|
static boolean |
notEmpty(String str)
Checks that string is not empty.
|
static boolean |
notEmpty(String s1,
String s2)
Checks that 2 strings are not empty
|
static boolean |
notEquals(String s1,
String s2,
boolean ignoreCase)
Checks if two strings are NOT equal.
|
static boolean |
notNulls(String value1,
String value2)
Checks that 2 strings are not nulls
|
static int |
occurrencesOf(String s,
int ch,
int start)
counts characters in string, case sensitive
|
static int |
occurrencesOf(String s,
String c,
int start,
boolean ignoreCase)
Returns the number of occurrences of the specified string.
|
static String |
padLeft(String string,
char padChar,
int length) |
static boolean |
parseBooleanDef(String s,
boolean def)
Parses string and returns converted boolean value.
|
static int |
parseIntDef(String s,
int def)
Parses string and returns converted integer value.
|
static long |
parseLongDef(String s,
long def)
Parses string and returns converted boolean value.
|
static void |
parseParameters(String url,
int start,
Map<String,String> parameters)
Adds parameters from url query string to specified map.
|
static String |
removeChars(String orig,
String chars)
Replaces all chars with the replacement char
|
static String |
removeCR(String s)
Replaces all CR(\n) symbols in string via " "(space) character
|
static String |
replaceChars(String orig,
String chars,
char replace)
Replaces all chars with the replacement char
|
static String |
replaceChars(String orig,
String chars,
String replace)
Replaces all chars with the replacement char
|
static String |
replaceHiAsciChars(String orig,
char replacementChar)
Strips out the hi ascii chars (>127) and replaces them with a replacement
char
|
static String |
replaceParams(String source,
String[] params)
Replace placeholders, or parameters, in a string that are of the format %[number].
|
static String |
smallIntToHex(int value,
boolean padding)
Convert small int to hex, optionally padding with 0s
|
static String |
stripFirstSegment(String value,
String separator)
Strips string first segment, defined by separator and returns stripped
string
|
static String |
stripLastSegment(String value,
String separator)
Strips string last segment, defined by separator and returns stripped
string
|
static String |
substituteBatch(String source,
Map<String,String> valuesMap)
Replace all patterns in the string with the ones given in the given
HashMap instance. |
static String |
tabStr(int count)
Return string containing the specified quantity of '\t' character
|
static boolean |
toBoolean(String value)
Converts string to boolean.
|
static String |
toCsv(boolean[] array)
Convert array of boolean to comma-separated string
|
static String |
toCsv(byte[] array)
Convert array of byte to comma-separated string
|
static String |
toCsv(double[] array)
Convert array of double to comma-separated string
|
static String |
toCsv(float[] array)
Convert array of float to comma-separated string
|
static String |
toCsv(int[] array)
Convert array of int to comma-separated string
|
static String |
toCsv(List<?> strings)
Convert
List of strings to comma-separated string |
static String |
toCsv(long[] array)
Convert array of long to comma-separated string
|
static String |
toCsv(Object[] array)
Convert array of objects to comma-separated string using the toString()
method to retrieve the value for each entry.
|
static String |
toCsv(short[] array)
Convert array of short to comma-separated string
|
static String |
toCsv(String[] array)
Convert array of strings to comma-separated string
|
static String |
toString(Collection<?> collection,
String lineSeperator)
Convert
Collection of object to single string for debugging
purposes |
static String |
toString(InputStream stream)
Loads stream contents to the stream
|
static String |
toString(Map<?,?> map)
Prints out a map with key value pairs combined with '=' separated by ',' and surrounded by { and }
|
static String |
toString(Map<?,?> map,
String[] ignoreKeys)
Prints out a map with key value pairs combined with '=' separated by ',' and surrounded by { and }
|
static String |
toString(Object object)
ToString object
|
static String |
toString(Object[] array)
To string an array of objects separated by ',' and surrounded by '[' and ']'.
|
static String |
toString(Reader reader)
Load string from reader
|
static String |
toString(String[] array)
Convert array to single string presentation for debugging purposes
|
static String |
toString(String[] array,
String lineSeparator)
Convert array of strings to single string presentation for debug purposes
|
static String |
trimString(String s,
String charsToTrim)
Trims specified characters from the beginning and end of the string
|
static String |
truncate(String value,
int maxLength)
Truncates input string to the specified size
|
static void |
truncateArray(Object[] values,
int[] actions)
Truncate array of strings using specified list of lengths (actions).
|
static String |
unwrap(String value,
String prefix,
String postfix)
Unwraps string using selected prefix and postfix
|
static String |
upperCaseFirstChar(String s)
Capilizes first character of a string
|
static String |
wrap(String value,
String prefix,
String postfix)
Wrap string value with prefix and postfix.
|
public static final String STR_EMPTY
public static final String STR_SUCCESSFUL
public static final String STR_FAILED
public static final String SYSTEM_ENCODING
public static final String BOOLEAN_ON
public static final String BOOLEAN_OFF
public static final String BOOLEAN_YES
public static final String BOOLEAN_NO
public static final String BOOLEAN_FALSE
public static final String BOOLEAN_TRUE
public static final String STR_COMMA
public static final String STR_SEMICOLON
public static final String STR_QUESTION
public static final String STR_EQUALS
public static final String STR_AND
public static final String STR_SPACE
public static final char CHR_DOT
public static final char CHR_FORWARD_SLASH
public static final char CHR_BACK_SLASH
public static final char CHR_TAB
public static String LINE_SEPARATOR
public static final int NO_ACTION
truncateArray(java.lang.Object[], int[])
to specify that no
truncation should be madeprotected static final String STRING_TOOLS_TLS_KEY
public static String hasSuccess(Object instance)
instance
- object instance to checkpublic static List<String> getListFromDelimitedString(String data, String delimiter)
data
- Original data to parsedelimiter
- Delimiter to use for data parsingLinkedList
of tokens parsed, delimiters are not returnedpublic static int[] getIntArrayFromDelimitedString(String data, String delimiter)
data
- Original data to parsedelimiter
- Delimiter to use for data parsingpublic static String[] getArrayFromCsvString(String csv)
csv
- Comma separated valuesString
extracted from linepublic static String getCsvFromList(List<?> list)
list
- source list of valuespublic static String getFirstSegment(String value, String separator)
value
- initial stringseparator
- is separator stringpublic static String getLastSegment(String value, String separator)
value
- initial stringseparator
- is separator stringpublic static String getLastSegment(String value, String separator, boolean includeSeperator)
value
- initial stringseparator
- is separator stringincludeSeperator
- true to include the seperator character in the result, false
otherwisepublic static String getSegment(String str, String separator, int segment)
str
- Source stringseparator
- Parsing separatorsegment
- String segment numberpublic static String stripFirstSegment(String value, String separator)
value
- initial stringseparator
- is separator stringpublic static String stripLastSegment(String value, String separator)
value
- initial stringseparator
- is separator stringpublic static boolean isEmpty(String str)
str
- Source string to analyzepublic static boolean notEmpty(String str)
isEmpty(java.lang.String)
str
- String value to analyzeisEmpty(java.lang.String)
public static String removeCR(String s)
s
- Source stringpublic static int parseIntDef(String s, int def)
s
- string to parsedef
- default int value, if string is unparsablepublic static boolean parseBooleanDef(String s, boolean def)
s
- String to parsedef
- Default boolean valuepublic static long parseLongDef(String s, long def)
s
- String to parsedef
- Default boolean valuepublic static void parseParameters(String url, int start, Map<String,String> parameters)
url
- Url with query string.start
- Index at which to start parse (ie url.indexOf('?')).parameters
- Map into which to put parsed param name=value pairs.public static String change(String s, String oldp, String newp, int start, int maxChanges, boolean ignoreCase)
s
- the input stringoldp
- the pattern to be replacednewp
- the replacement stringstart
- the starting position for the searchmaxChanges
- the maximum number of changesignoreCase
- if true case is ignored when comparing characterspublic static String change(String s, String oldp, String newp, int start, boolean ignoreCase)
s
- the input stringoldp
- the pattern to be replacednewp
- the replacement stringstart
- the starting position for the searchignoreCase
- if true case is ignored when comparing characterspublic static String change(String s, String oldp, String newp, int start, int maxChanges)
s
- the input stringoldp
- the pattern to be replacednewp
- the replacement stringstart
- the starting position for the searchmaxChanges
- the maximum number of changespublic static String change(String s, String oldp, String newp, int start)
s
- the input stringoldp
- the pattern to be replacednewp
- the replacement stringstart
- the starting position for the searchpublic static String change(String s, String oldp, String newp, boolean ignoreCase)
s
- the input stringoldp
- the pattern to be replacednewp
- the replacement stringignoreCase
- if true case is ignored when comparing characterspublic static String change(String s, String oldp, String newp)
s
- the input stringoldp
- the pattern to be replacednewp
- the replacement stringpublic static int occurrencesOf(String s, String c, int start, boolean ignoreCase)
s
- the string to be inspectedc
- the string to search forstart
- the starting position for the searchignoreCase
- if true case is ignored when comparing characterspublic static int occurrencesOf(String s, int ch, int start)
s
- string to analyzech
- character we are looking forstart
- start offset inside the stringpublic static int indexOf(String s, String c, int start, boolean ignoreCase)
s
- the string to be inspectedc
- the string to be searched forstart
- the starting position for the searchignoreCase
- if true case is ignored when comparing charactersString.indexOf(String, int)
public static boolean equals(char c1, char c2, boolean ignoreCase)
c1
- first characterc2
- second characterignoreCase
- if true case is ignored when comparing charactersString.equalsIgnoreCase(String)
public static boolean equals(String s1, String s2, boolean ignoreCase)
s1
- first strings2
- second stringignoreCase
- if true case is ignored when comparing stringString.equalsIgnoreCase(String)
public static boolean notEquals(String s1, String s2, boolean ignoreCase)
s1
- first strings2
- second stringignoreCase
- if true case is ignored when comparing stringString.equalsIgnoreCase(String)
public static int compareStrings(String s1, String s2, boolean ignoreCase)
s1
- first strings2
- second stringignoreCase
- if true case is ignored when comparing stringString.equalsIgnoreCase(String)
public static String substituteBatch(String source, Map<String,String> valuesMap)
HashMap
instance.
Example:
// This string is obtained on run-time. For example, template inner text String toChangeIn = "<H1> ${SomeVarName} </H1>\r\n<td>${AnotherVarName}</td>"; hashMap.put("${SomeVarName}", SomeRunTimeVarValue); hashMap.put("${AnotherVarName}", AnotherVarRuntime); String changedString = StringTools.substituteBatch(toChangeIn, hashMap);
public static String intToHex(int value, boolean padding)
value
- int value to convertpadding
- do we want to pad int with 0'spublic static String smallIntToHex(int value, boolean padding)
value
- small int value to convertpadding
- do we want to pad int with 0'spublic static String ensureTrailing(String value, String suffix)
value
- string value to ensuresuffix
- suffex to append (if not yet)public static String ensureLeading(String value, String prefix)
value
- string value to ensureprefix
- prefix to append (if not yet)public static String toString(InputStream stream) throws IOException
stream
- InputStream
to loadIOException
public static String toString(Reader reader)
reader
- Source readerpublic static String toString(Map<?,?> map, String[] ignoreKeys)
map
- map to be printedpublic static String toString(Map<?,?> map)
map
- map to be printedpublic static String toString(Object[] array)
array
- array to be printedpublic static String toString(Object object)
object
- object to be printedpublic static String toString(String[] array)
array
- Source string arraypublic static String toString(Collection<?> collection, String lineSeperator)
Collection
of object to single string for debugging
purposescollection
- Source data collectionlineSeperator
- Line separator to useCollection
public static String toString(String[] array, String lineSeparator)
array
- Source array of stringslineSeparator
- Line separator to delimit itemspublic static String replaceHiAsciChars(String orig, char replacementChar)
orig
- Original stringreplacementChar
- Replacement character to usepublic static String replaceChars(String orig, String chars, char replace)
orig
- Original stringchars
- Characters we want to replacereplace
- Replacement characterpublic static String replaceChars(String orig, String chars, String replace)
orig
- Original stringchars
- Characters we want to replacereplace
- Replacement characterpublic static boolean toBoolean(String value)
value
- Source string value to convertpublic static String wrap(String value, String prefix, String postfix)
result := prefix + value + postfix
value
- Source string value to wrapprefix
- Prefix to usepostfix
- Postfix to usepublic static String unwrap(String value, String prefix, String postfix)
value
- Previously wrapped stringprefix
- Prefix to usepostfix
- Postfix to usewrap(java.lang.String, java.lang.String, java.lang.String)
callwrap(java.lang.String, java.lang.String, java.lang.String)
public static String truncate(String value, int maxLength)
value
- Source string to analyzemaxLength
- Maximum string lengthpublic static void truncateArray(Object[] values, int[] actions)
String[] values = new String[] {
"one",
"Some reallllllllllllllllllllly big value",
"Anotherrrrrrrrrrrrrrrrrrrrrrrrrrrr big value"
};
int[] actions = new int[] {
NO_ACTION // See NO_ACTION
20,
20
};
StringTools.truncateArray(values, actions);
In this sample 1st element of the values will be left unchanged,
but all others will be truncated using 20 as a string length
limitvalues
- Original array of stringsactions
- Lengths(actions) to be taken over string valuesNO_ACTION
public static boolean isFloat(String value)
value
- public static String toCsv(List<?> strings)
List
of strings to comma-separated stringstrings
- Original strings containerpublic static String toCsv(String[] array)
array
- Original strings containerpublic static String toCsv(Object[] array)
array
- Original strings containerpublic static String toCsv(int[] array)
array
- Original int containerpublic static String toCsv(short[] array)
array
- Original short containerpublic static String toCsv(long[] array)
array
- Original long containerpublic static String toCsv(boolean[] array)
array
- Original boolean containerpublic static String toCsv(float[] array)
array
- Original float containerpublic static String toCsv(double[] array)
array
- Original double containerpublic static String toCsv(byte[] array)
array
- Original byte containerpublic static int[] getIntArrayFromCsv(String data)
data
- Original data to parsepublic static short[] getShortArrayFromCsv(String data)
data
- Original data to parsepublic static long[] getLongArrayFromCsv(String data)
data
- Original data to parsepublic static float[] getFloatArrayFromCsv(String data)
data
- Original data to parsepublic static double[] getDoubleArrayFromCsv(String data)
data
- Original data to parsepublic static boolean[] getBooleanArrayFromCsv(String data)
data
- Original data to parsepublic static byte[] getByteArrayFromCsv(String data)
data
- Original data to parsepublic static Set<String> csvToSet(String csvLine)
Set
of strings. This is
especially useful when you do know that line can convert duplicates and
you don't want to deal with them.csvLine
- Original comma-separated linepublic static String concat(String s1, String s2)
StringBuffer
.s1
- source string #1s2
- source string #2public static String concat(String s1, String s2, String s3)
StringBuffer
s1
- source string #1s2
- source string #2s3
- source string #3public static String concat(String s1, String s2, String s3, String s4)
StringBuffer
s1
- source string #1s2
- source string #2s3
- source string #3s4
- source string #4public static long fastParseLong(String str) throws NumberFormatException
Long.parseLong(java.lang.String, int)
str
- string to convertNumberFormatException
public static ArrayList<String> getDelimitedList(String value, String delims, boolean includeDelim)
ArrayList
of tokens from delimited original stringvalue
- Original string valuedelims
- Delimiters used to make tokenspublic static ArrayList<String> getDelimitedList(String value, String delims)
ArrayList
of tokens from delimited original stringvalue
- Original string valuedelims
- Delimiters used to make tokenspublic static String charStr(char character, int count)
character
- original charactercount
- length of the string to constructpublic static String tabStr(int count)
count
- Quantity of tabs you want to recievecharStr(char, int)
public static boolean isEmpty(String s1, String s2)
s1
- source string #1s2
- source string #2public static boolean notEmpty(String s1, String s2)
s1
- string to check #1s2
- string to check #2public static boolean notNulls(String value1, String value2)
value1
- source string #1value2
- source string #2public static String removeChars(String orig, String chars)
orig
- Original stringchars
- Chars to removepublic static boolean isFormatCharsOnly(String value)
value
- Original stringpublic static String replaceParams(String source, String[] params)
Example: Calling on string "Error locating resource %0 for user %1" with array {"root", "admin"} would result in "Error locating resource root for user admin"
source
- a string with parameters, starting with 0params
- the values for the parameters, the index corresponding to the
parameter value + 1public static String trimString(String s, String charsToTrim)
s
- charsToTrim
- public static String humanizeName(String name)
name
- the value to humanizepublic static String lowerCaseFirstChar(String s)
s
- null or Stringpublic static String upperCaseFirstChar(String s)
s
- null or Stringpublic static String getStackTrace(Throwable t)
t
- Throwable to work withpublic static String loadStringfromStream(InputStream input) throws IOException
InputStream
IOException
- in case of any I/O errorpublic static String loadStringFromReader(Reader reader) throws IOException
StringAccessReader
reader
- Source readerIOException
StringAccessReader
public static boolean isEmptyString(Object obj)
str
- Source string to analyzepublic static String getFullStackTrace(Throwable t)
t
- Throwable to analyze