|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES All Classes | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.webmethods.caf.common.StringTools
public class StringTools
Various string utility functions and operations
Field Summary | |
---|---|
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 StringTools.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_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 |
static String |
SYSTEM_ENCODING
Standard system encoding (UTF-8) |
Constructor Summary | |
---|---|
StringTools()
|
Method Summary | |
---|---|
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 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 |
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 |
getDelimitedList(String value,
String delims)
Get ArrayList of tokens from delimited original string |
static ArrayList |
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 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 |
getListFromCsv(String value)
Parses comma delimited string to List of String instances |
static List |
getListFromCsv(String value,
boolean allowDups)
Parses comma delimited string to List of String instances |
static List |
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 |
getLongsListFromCsv(String value)
Parses comma delimited string to List of Long instances |
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 |
isFloat(String value)
Checks whether specified value convertable to float |
static boolean |
isFormatCharsOnly(String value)
Verifies that string contains only formatting characters |
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 |
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 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 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 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(Object object)
ToString object |
static String |
toString(Object[] array)
To string an array of objects separated by ',' and surrounded by '[' and ']'. |
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. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
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
StringTools.truncateArray(java.lang.Object[], int[])
to specify that no
truncation should be made
Constructor Detail |
---|
public StringTools()
Method Detail |
---|
public static String hasSuccess(Object instance)
instance
- object instance to checkpublic static List getListFromDelimitedString(String data, String delimiter)
data
- Original data to parsedelimiter
- Delimiter to use for data parsing
LinkedList
of tokens parsed, delimiters are not returnedpublic static int[] getIntArrayFromDelimitedString(String data, String delimiter)
data
- Original data to parsedelimiter
- Delimiter to use for data parsing
public static String[] getArrayFromCsvString(String csv)
csv
- Comma separated values
String
extracted from linepublic static List getListFromCsv(String value)
List
of String
instances
value
- Comma delimited string to parse
List
of String
s extractecd from comma-separated
linepublic static List getListFromCsv(String value, boolean allowDups)
List
of String
instances
value
- Comma delimited string to parseallowDups
- true to allow duplicates within the list
List
of String
s extractecd from comma-separated
linepublic static List getLongsListFromCsv(String value)
List
of Long
instances
value
- Comma delimited string to parse
List
of Long
spublic static String getCsvFromList(List list)
list
- source list of values
public static String getFirstSegment(String value, String separator)
value
- initial stringseparator
- is separator string
public static String getLastSegment(String value, String separator)
value
- initial stringseparator
- is separator string
public 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
otherwise
public static String getSegment(String str, String separator, int segment)
str
- Source stringseparator
- Parsing separatorsegment
- String segment number
public static String stripFirstSegment(String value, String separator)
value
- initial stringseparator
- is separator string
public static String stripLastSegment(String value, String separator)
value
- initial stringseparator
- is separator string
public static boolean isEmpty(String str)
str
- Source string to analyze
public static boolean notEmpty(String str)
StringTools.isEmpty(java.lang.String)
str
- String value to analyze
StringTools.isEmpty(java.lang.String)
public static String removeCR(String s)
s
- Source string
public static int parseIntDef(String s, int def)
s
- string to parsedef
- default int value, if string is unparsable
public static boolean parseBooleanDef(String s, boolean def)
s
- String to parsedef
- Default boolean value
public static long parseLongDef(String s, long def)
s
- String to parsedef
- Default boolean value
public static void parseParameters(String url, int start, Map 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 characters
public 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 characters
public 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 changes
public 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 search
public 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 characters
public static String change(String s, String oldp, String newp)
s
- the input stringoldp
- the pattern to be replacednewp
- the replacement string
public 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 characters
public static int occurrencesOf(String s, int ch, int start)
s
- string to analyzech
- character we are looking forstart
- start offset inside the string
public 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 characters
String.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 characters
String.equalsIgnoreCase(String)
public static boolean equals(String s1, String s2, boolean ignoreCase)
s1
- first strings2
- second stringignoreCase
- if true case is ignored when comparing string
String.equalsIgnoreCase(String)
public static String substituteBatch(String source, Map 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's
public static String smallIntToHex(int value, boolean padding)
value
- small int value to convertpadding
- do we want to pad int with 0's
public 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 load
IOException
public static String toString(Map map)
map
- map to be printed
public static String toString(Object[] array)
array
- array to be printed
public static String toString(Object object)
object
- object to be printed
public static String toString(String[] array)
array
- Source string array
public static String toString(Collection collection, String lineSeperator)
Collection
of object to single string for debugging
purposes
collection
- Source data collectionlineSeperator
- Line separator to use
Collection
public static String toString(String[] array, String lineSeparator)
array
- Source array of stringslineSeparator
- Line separator to delimit items
public static String replaceHiAsciChars(String orig, char replacementChar)
orig
- Original stringreplacementChar
- Replacement character to use
public static String replaceChars(String orig, String chars, char replace)
orig
- Original stringchars
- Characters we want to replacereplace
- Replacement character
public static String replaceChars(String orig, String chars, String replace)
orig
- Original stringchars
- Characters we want to replacereplace
- Replacement character
public static boolean toBoolean(String value)
value
- Source string value to convert
public static String wrap(String value, String prefix, String postfix)
result := prefix + value + postfix
value
- Source string value to wrapprefix
- Prefix to usepostfix
- Postfix to use
public static String unwrap(String value, String prefix, String postfix)
value
- Previously wrapped stringprefix
- Prefix to usepostfix
- Postfix to use
StringTools.wrap(java.lang.String, java.lang.String, java.lang.String)
callStringTools.wrap(java.lang.String, java.lang.String, java.lang.String)
public static String truncate(String value, int maxLength)
value
- Source string to analyzemaxLength
- Maximum string length
public 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 StringTools.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
limit
values
- Original array of stringsactions
- Lengths(actions) to be taken over string valuesStringTools.NO_ACTION
public static boolean isFloat(String value)
value
-
public static String toCsv(List strings)
List
of strings to comma-separated string
strings
- Original strings container
public static String toCsv(String[] array)
array
- Original strings container
public static String toCsv(Object[] array)
array
- Original strings container
public static String toCsv(int[] array)
array
- Original int container
public static String toCsv(short[] array)
array
- Original short container
public static String toCsv(long[] array)
array
- Original long container
public static String toCsv(boolean[] array)
array
- Original boolean container
public static String toCsv(float[] array)
array
- Original float container
public static String toCsv(double[] array)
array
- Original double container
public static String toCsv(byte[] array)
array
- Original byte container
public static int[] getIntArrayFromCsv(String data)
data
- Original data to parse
public static short[] getShortArrayFromCsv(String data)
data
- Original data to parse
public static long[] getLongArrayFromCsv(String data)
data
- Original data to parse
public static float[] getFloatArrayFromCsv(String data)
data
- Original data to parse
public static double[] getDoubleArrayFromCsv(String data)
data
- Original data to parse
public static boolean[] getBooleanArrayFromCsv(String data)
data
- Original data to parse
public static byte[] getByteArrayFromCsv(String data)
data
- Original data to parse
public static Set 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 line
public static String concat(String s1, String s2)
StringBuffer
.
s1
- source string #1s2
- source string #2
public static String concat(String s1, String s2, String s3)
StringBuffer
s1
- source string #1s2
- source string #2s3
- source string #3
public static String concat(String s1, String s2, String s3, String s4)
StringBuffer
s1
- source string #1s2
- source string #2s3
- source string #3s4
- source string #4
public static long fastParseLong(String str) throws NumberFormatException
Long.parseLong(java.lang.String, int)
str
- string to convert
NumberFormatException
public static ArrayList getDelimitedList(String value, String delims, boolean includeDelim)
ArrayList
of tokens from delimited original string
value
- Original string valuedelims
- Delimiters used to make tokens
public static ArrayList getDelimitedList(String value, String delims)
ArrayList
of tokens from delimited original string
value
- Original string valuedelims
- Delimiters used to make tokens
public static String charStr(char character, int count)
character
- original charactercount
- length of the string to construct
public static String tabStr(int count)
count
- Quantity of tabs you want to recieve
StringTools.charStr(char, int)
public static boolean isEmpty(String s1, String s2)
s1
- source string #1s2
- source string #2
public static boolean notEmpty(String s1, String s2)
s1
- string to check #1s2
- string to check #2
public static boolean notNulls(String value1, String value2)
value1
- source string #1value2
- source string #2
public static String removeChars(String orig, String chars)
orig
- Original stringchars
- Chars to remove
public static boolean isFormatCharsOnly(String value)
value
- Original string
public 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 + 1
public static String trimString(String s, String charsToTrim)
s
- charsToTrim
- public static String capitalizeName(String name)
public static String humanizeName(String name)
name
- the value to humanize
public static String lowerCaseFirstChar(String s)
s
- null or String
public static String upperCaseFirstChar(String s)
s
- null or String
public static String getStackTrace(Throwable t)
t
- Throwable to work with
public static String loadStringfromStream(InputStream input) throws IOException
InputStream
IOException
- in case of any I/O error
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES All Classes | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |