Package org.cdlib.mrt.utility
Class StringUtil
java.lang.Object
org.cdlib.mrt.utility.StringUtil
This class is a collection of String Utility functions not found in the String class.
They are all static and so can be called with instanciating this class.
- Author:
- Michael Thwaites
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
Test if passed value is a "true" value Used to validate that an argument means "true" or "yes"static String
byteArrToString
(byte[] byteArr) Converts an array of bytes to a string guaranteeing utf-8 mapping if possible
Note works only with utf-8 and a byte-char set (e.g.static final String
compress multiple occurances of blank to one occurancestatic final String
compress multiple occurances of cval to one occurancestatic int
Count the number of times match occurs in sourcestatic String
escapeQuotes
(String in) replace all occurances of single quote (') with two single quotes.static String
getUTF8
(byte[] bytes) static final boolean
isAllBlank
(String s) Return true if only space characters are found or emptystatic final boolean
isEmpty
(byte[] b) test if the byte [] passed is null or emptystatic final boolean
test if the string passed is null or emptystatic final boolean
isEmpty
(StringBuffer sb) test if the StringBuffer passed is null or emptystatic final boolean
isNotEmpty
(byte[] b) test if the byte [] passed is not null and not emptystatic final boolean
isNotEmpty
(String s) test if the string passed is not null and not emptystatic final boolean
test if the StringBuffer passed is not null and not emptystatic final boolean
isNumeric (String) tests if all the characters of a string are digitsstatic String
static String
lowerCaseFirst
(String inString) lowerCaseFirst (String) returns the passed string with first char lowercased , e.g.static boolean
matchRegex
(String inString, String regex) MatchRegex (String) returns boolean value for whether string matches regex Added by SR For info on Java regex, which is quite similar to Perl regex, see http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html Examples: -- inString contains "a" or "b": regex = "a|b" -- inString contains "a" zero or more times: regex = "a*" -- inString contains "a" one or more times: regex = "a+"static String
A common normalization for parms: if null or has length zero then set null if all space characters set null trim space charactersstatic String
removeControl
(String inString) removeControl - replace ASCII control characters with blankstatic final String
squeeze the blanks from a stringstatic final String
squeeze a set of characters from a stringstatic String
squeezeControl
(String in) static String
stackTrace returns the stack trace for the exception passed.static byte[]
streamToByteArray
(InputStream ioStream) convert ioStream to Stringstatic String
streamToString
(InputStream ioStream, String encoding) convert ioStream to Stringstatic InputStream
stringToStream
(String inString, String encoding) convert String to InputStreamstatic String
static String
stripNonAlphabetic
(String inString) stripNonAlphabetic remove all non-alphabeticsstatic String
toHex
(byte[] inBytes) toHex (byte []) returns the passed byte array in hexadecimalstatic String
toHex
(byte[] inBytes, int maxbytes) toHex (byte []) returns the passed byte array in hexadecimalstatic String
toHex (String) returns the passed string in hexadecimalstatic String
upperCaseFirst
(String inString) upperCaseFirst (String) returns the passed string with first char uppercased , e.g.static String
xchange replaces all occurances of a String of characters for the corresponding string from an array of String.
-
Constructor Details
-
StringUtil
public StringUtil()
-
-
Method Details
-
isNumeric
isNumeric (String) tests if all the characters of a string are digits- Parameters:
s
- - the string to test
-
isAllBlank
Return true if only space characters are found or empty- Parameters:
s
-- Returns:
- true=all space; false=at least one non-space
-
isEmpty
test if the string passed is null or empty- Parameters:
s
- - the string test- Returns:
- true or false
-
isEmpty
test if the StringBuffer passed is null or empty- Parameters:
sb
- - the StringBuffer to test- Returns:
- true or false
-
isEmpty
public static final boolean isEmpty(byte[] b) test if the byte [] passed is null or empty- Parameters:
b
- - the byte [] to test- Returns:
- true or false
-
isNotEmpty
test if the string passed is not null and not empty- Parameters:
s
- - the string to test- Returns:
- true or false
-
isNotEmpty
test if the StringBuffer passed is not null and not empty- Parameters:
sb
- - the StringBuffer to test- Returns:
- true or false
-
isNotEmpty
public static final boolean isNotEmpty(byte[] b) test if the byte [] passed is not null and not empty- Parameters:
b
- - the byte [] to test- Returns:
- true or false
-
squeeze
squeeze the blanks from a string- Parameters:
s
- - the string to squeeze- Returns:
- a string with the blanks removed
-
squeeze
squeeze a set of characters from a string- Parameters:
s
- - the string to squeezetoRemove
- - the characters to remove- Returns:
- a string with the characters removed
-
compress
compress multiple occurances of cval to one occurance- Parameters:
s
- the string to compresscval
- compress string- Returns:
- compressed string
-
compress
compress multiple occurances of blank to one occurance- Parameters:
s
- the string to compress- Returns:
- compressed string
-
escapeQuotes
replace all occurances of single quote (') with two single quotes.- Parameters:
in
- - the string to process- Returns:
- the processed string
-
toHex
toHex (String) returns the passed string in hexadecimal- Parameters:
inString
- - the string to convert- Returns:
- the string in hex
-
removeControl
removeControl - replace ASCII control characters with blank- Parameters:
inString
- - the string to convert- Returns:
- string without controls
-
stripNonAlphabetic
stripNonAlphabetic remove all non-alphabetics- Parameters:
inString
- - the string to strip- Returns:
- string without alphabetics
-
strip
-
toHex
toHex (byte []) returns the passed byte array in hexadecimal- Parameters:
inBytes
- - the byte array to convert- Returns:
- the byte array in hex
-
toHex
toHex (byte []) returns the passed byte array in hexadecimal- Parameters:
inBytes
- - the byte array to convert- Returns:
- the byte array in hex
-
stackTrace
stackTrace returns the stack trace for the exception passed. This is the same stack trace that would be printed with the Exception.printStackTrace() call.- Parameters:
e
- - The throwable to give the stack trace for.
-
byteArrToString
Converts an array of bytes to a string guaranteeing utf-8 mapping if possible
Note works only with utf-8 and a byte-char set (e.g. any iso-8859-n)- Parameters:
byteArr
- array of bytes to convert to string- Returns:
- mapped String
-
streamToByteArray
convert ioStream to String- Parameters:
ioStream
- stream to be convertedencoding
- String encoding from byte buffer- Returns:
- converted String
-
streamToString
convert ioStream to String- Parameters:
ioStream
- stream to be convertedencoding
- String encoding from byte buffer- Returns:
- converted String
-
stringToStream
convert String to InputStream- Parameters:
inString
- string to be convertedencoding
- String encoding from byte buffer- Returns:
- converted String
-
lowerCaseFirst
lowerCaseFirst (String) returns the passed string with first char lowercased , e.g. "CapString" becomes "capString". Added by SR- Parameters:
inString
- - the string to convert- Returns:
- the string with lowercased first char
-
upperCaseFirst
upperCaseFirst (String) returns the passed string with first char uppercased , e.g. "capString" becomes "CapString". Added by SR- Parameters:
inString
- - the string to convert- Returns:
- the string with uppercased first char
-
matchRegex
MatchRegex (String) returns boolean value for whether string matches regex Added by SR For info on Java regex, which is quite similar to Perl regex, see http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html Examples: -- inString contains "a" or "b": regex = "a|b" -- inString contains "a" zero or more times: regex = "a*" -- inString contains "a" one or more times: regex = "a+"- Parameters:
inString
- - the string to testregex
- - the regex to match- Returns:
- boolean matches or not
-
squeezeControl
-
count
Count the number of times match occurs in source- Parameters:
source
- string to be testedmatch
- string to count in source- Returns:
- number of times match occurs in source
-
argIsTrue
Test if passed value is a "true" value Used to validate that an argument means "true" or "yes"- Parameters:
test
- String to be tested- Returns:
- true="true" or "yes" value, false="false" or "no"
- Throws:
TException
-
getUTF8
-
xchange
xchange replaces all occurances of a String of characters for the corresponding string from an array of String.- Parameters:
in
- - the string to make changes infrom
- - the Sting of characters to look forto
- - the array of Strings to substitute- Returns:
- the string with substitutions made.
-
normParm
A common normalization for parms: if null or has length zero then set null if all space characters set null trim space characters- Parameters:
in
- string to convert- Returns:
- string with leading and trailing non-space
-
leftPad
-