Package com.sun.enterprise.util
Class StringUtils
- java.lang.Object
-
- com.sun.enterprise.util.StringUtils
-
public class StringUtils extends Object
-
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static String
cat(String separator, String... list)
Concatenate a list of strings, putting a separator in between each one.static String
escapeForHtml(String str)
Escape characters to use result in html.static String
formatSQLException(SQLException ex)
Returns a String containing SQLState, message and error code of exception and all sub-exceptionsstatic String
getPenultimateDirName(String s)
e.g.static String
getProperty(String propName)
Get a system propety given a property name, possibly trying all combination of upercase, name mangling to get a value.static String
getStackTrace(Throwable t)
Gets a String version of the stack trace of an exceptionstatic boolean
isHex(char c)
Is this the char a valid hex digit?static boolean
isHex(String s)
Is this the String representation of a valid hex number? "5", "d", "D", "F454ecbb" all return true..static boolean
isToken(String s)
Returns true is the given string is of the form "${text}"static void
main(String[] args)
static String
makeFilePath(String[] strings, boolean addTrailing)
A utility to get the Operating System specific path from given array of Strings.static int
maxWidth(List strings)
Find longest String in a List of Strings...static String
nvl(String str)
If givenString
isnull
then returns emptyString
otherwise returns givenString
static boolean
ok(String s)
Returns true if a string is not null and has a non-zero length, false otherwisestatic String
padLeft(String s, int len)
Adds spaces to the front of a string to make it reach the specified length.static String
padRight(String s, int len)
Adds spaces to the end of a string to make it reach the specified length.static List<String>
parseStringList(String line)
Parses a string containing substrings separated from each other by the standard separator characters and returns a list of strings.static List<String>
parseStringList(String line, String sep)
Parses a string containing substrings separated from each other by the specified set of separator characters and returns a list of strings.static void
prepend(String[] ss, String what)
Puts a string before every element in an arraystatic String
quotePathIfNecessary(String path)
Nightmares can result from using a path with a space in it! This method will enclose in double-quotes if needed.static String
quotePathIfNecessary(String path, char quoteChar)
Nightmares can result from using a path with a space in it! This method will enclose in the specified quote characters if needed.static String
removeChar(String strOrig, char c)
Remove a character from a Stringstatic String
removeEnclosingQuotes(String s)
Removes the quoting around a String.static String
replace(String s, String token, String replace)
Deprecated.Now part ofString
since JDK 1.5static int
safeLength(String s)
return the length of the String - or 0 if it's nullstatic String
stripToken(String s)
Removes preceding${
and trailing}
from a Stringstatic void
testUpperCase()
static String[]
toLines(String s)
Converts a String into an array where every \n is a new used to signal a new element in the arraystatic String
toShortClassName(String className)
Returns the classname without package infostatic String
toString(Properties props)
Converts aProperties
object to stringstatic String
trimQuotes(String value)
static String
upperCaseFirstLetter(String s)
Converts the first letter of a string to upper case
-
-
-
Method Detail
-
safeLength
public static int safeLength(String s)
return the length of the String - or 0 if it's null- Parameters:
s
-- Returns:
-
ok
public static boolean ok(String s)
Returns true if a string is not null and has a non-zero length, false otherwise- Parameters:
s
-- Returns:
-
formatSQLException
public static String formatSQLException(SQLException ex)
Returns a String containing SQLState, message and error code of exception and all sub-exceptions- Parameters:
ex
- the exception to format
-
maxWidth
public static int maxWidth(List strings)
Find longest String in a List of Strings...- Parameters:
strings
- the list of strings
-
isHex
public static boolean isHex(String s)
Is this the String representation of a valid hex number? "5", "d", "D", "F454ecbb" all return true..- Parameters:
s
- .
-
isHex
public static boolean isHex(char c)
Is this the char a valid hex digit?Can be upper or lower case
- Parameters:
c
-- Returns:
-
getPenultimateDirName
public static String getPenultimateDirName(String s)
e.g. input: "a/b/c/d/foobar.txt" output: "d"- Parameters:
s
-- Returns:
-
toShortClassName
public static String toShortClassName(String className)
Returns the classname without package infoi.e. java.lang.String would return String
- Parameters:
className
- The classname to convert. Note that there is no checking that this is a valid classname.- Returns:
-
padRight
public static String padRight(String s, int len)
Adds spaces to the end of a string to make it reach the specified length.If the string is longer than the padded length then this function will return the original string.
- Parameters:
s
- String to padlen
- The length of the string with added padding- Returns:
- The padded string
-
padLeft
public static String padLeft(String s, int len)
Adds spaces to the front of a string to make it reach the specified length.If the string is longer than the padded length then this function will return the original string.
- Parameters:
s
- String to padlen
- The length of the string with added padding- Returns:
- The padded string
-
toLines
public static String[] toLines(String s)
Converts a String into an array where every \n is a new used to signal a new element in the array- Parameters:
s
- string to split into lines
-
prepend
public static void prepend(String[] ss, String what)
Puts a string before every element in an arrayi.e.
prepend(new String["foo", "bar"],"not ")
would result inString["not foo", "not bar"]
- Parameters:
ss
-what
-
-
upperCaseFirstLetter
public static String upperCaseFirstLetter(String s)
Converts the first letter of a string to upper case- Parameters:
s
-- Returns:
-
replace
public static String replace(String s, String token, String replace)
Deprecated.Now part ofString
since JDK 1.5Replaces the first instance of a token within a string- Parameters:
s
- The string to operate ontoken
- the token to be replacedreplace
- the new value- Returns:
- See Also:
String.replace(CharSequence, CharSequence)
,String.replaceFirst(String, String)
-
toString
public static String toString(Properties props)
Converts aProperties
object to stringIf there it is empty then this will return "No entries". Otherwise it will be in the form of "key= value" with each property on a new line.
- Parameters:
props
-- Returns:
-
main
public static void main(String[] args)
-
testUpperCase
public static void testUpperCase()
-
makeFilePath
public static String makeFilePath(String[] strings, boolean addTrailing)
A utility to get the Operating System specific path from given array of Strings.- Parameters:
strings
- an array of Strings participating in the path.addTrailing
- a boolean that determines whether the returned String should have a trailing File Separator character. None of the strings may be null or empty String. An exception is thrown.- Returns:
- a String that concatenates these Strings and gets a path. Returns a null if the array is null or contains no elements.
- Throws:
IllegalArgumentException
- if any of the arguments is null or is an empty string.
-
parseStringList
public static List<String> parseStringList(String line)
Parses a string containing substrings separated from each other by the standard separator characters and returns a list of strings. Splits the stringline
into individual string elements separated by the field separators, and returns these individual strings as a list of strings. The individual string elements are trimmed of leading and trailing whitespace. Only non-empty strings are returned in the list.- Parameters:
line
- The string to split- Returns:
- Returns the list containing the individual strings that the input string was split into.
-
parseStringList
public static List<String> parseStringList(String line, String sep)
Parses a string containing substrings separated from each other by the specified set of separator characters and returns a list of strings. Splits the stringline
into individual string elements separated by the field separators specified insep
, and returns these individual strings as a list of strings. The individual string elements are trimmed of leading and trailing whitespace. Only non-empty strings are returned in the list.- Parameters:
line
- The string to splitsep
- The list of separators to use for determining where the string should be split. If null, then the standard separators (see StringTokenizer javadocs) are used.- Returns:
- Returns the list containing the individual strings that the input string was split into.
-
getProperty
public static String getProperty(String propName)
Get a system propety given a property name, possibly trying all combination of upercase, name mangling to get a value.- Parameters:
propName
- the approximate system property name- Returns:
- the property value if found, null otherwise
-
removeChar
public static String removeChar(String strOrig, char c)
Remove a character from a String- Parameters:
strOrig
- original stringc
- character to remove from the string- Returns:
- String with specified characters removed
-
getStackTrace
public static String getStackTrace(Throwable t)
Gets a String version of the stack trace of an exception- Parameters:
t
-- Returns:
-
isToken
public static final boolean isToken(String s)
Returns true is the given string is of the form "${text}"- Parameters:
s
-- Returns:
-
stripToken
public static final String stripToken(String s)
Removes preceding${
and trailing}
from a String- Parameters:
s
-- Returns:
-
cat
public static String cat(String separator, String... list)
Concatenate a list of strings, putting a separator in between each one. If the list is one string, then the separator is not used. The separator will never be added to the start or end of the returned string. When empty or null strings are encountered in the list of strings they are ignore.- Parameters:
separator
- Separator to use between concatenated stringslist
- List of strings to concatenate together- Returns:
- String created by concatenating provided strings
-
removeEnclosingQuotes
public static String removeEnclosingQuotes(String s)
Removes the quoting around a String.- Parameters:
s
- The String that may have enclosing quotes- Returns:
- The String resulting from removing the enclosing quotes
-
quotePathIfNecessary
public static String quotePathIfNecessary(String path)
Nightmares can result from using a path with a space in it! This method will enclose in double-quotes if needed.- Parameters:
path
-- Returns:
-
quotePathIfNecessary
public static String quotePathIfNecessary(String path, char quoteChar)
Nightmares can result from using a path with a space in it! This method will enclose in the specified quote characters if needed.- Parameters:
path
-quoteChar
-- Returns:
-
escapeForHtml
public static String escapeForHtml(String str)
Escape characters to use result in html.Chars Escape sequence < < > > & & " " \t - Parameters:
str
-- Returns:
-
nvl
public static String nvl(String str)
If givenString
isnull
then returns emptyString
otherwise returns givenString
- Parameters:
str
-- Returns:
-
-