|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectnl.knowledgeplaza.util.StringUtil
public class StringUtil
This class contains several String helper methods.
| Field Summary | |
|---|---|
static java.lang.String |
FILTERCHARS_BACKSLASH
|
static java.lang.String |
FILTERCHARS_DIGITS
|
static java.lang.String |
FILTERCHARS_LOWERLETTERS
|
static java.lang.String |
FILTERCHARS_PUNCTUATION
|
static java.lang.String |
FILTERCHARS_QUOTES
|
static java.lang.String |
FILTERCHARS_UPPERLETTERS
|
static java.lang.String |
SOURCECODE_VERSION
Standard variable for determining version of a class file. |
| Constructor Summary | |
|---|---|
StringUtil()
|
|
| Method Summary | |
|---|---|
static java.lang.String |
byteArrayToString(byte[] ba)
Returns a byte array as a java string |
static java.lang.String |
byteArrayToString(byte[] ba,
int offset,
int len)
Returns a byte array as a java string |
static java.lang.String |
byteToHex(byte b)
Returns hex String representation of byte b |
static java.lang.String |
byteToHex(byte[] b)
Returns hex String representation of bytes b |
static java.lang.String |
charToHex(char c)
Returns hex String representation of char c |
static boolean |
contains(java.lang.String str,
char chr)
Return true if the string contains the other character. |
static boolean |
contains(java.lang.String str,
java.lang.String sub)
Return true if the string contains the other string. |
static java.lang.String |
convertStandardTypesToString(java.lang.Object o)
|
static java.lang.Object |
convertStringToStandardTypes(java.lang.String s,
java.lang.Class c)
|
static int |
count(java.lang.String text,
java.lang.String pattern)
counts how many times the given pattern occurs in the given text. |
static int |
countWords(java.lang.String s,
java.lang.String delimiter)
|
static java.lang.String |
deleteChars(java.lang.String str,
java.lang.String chars)
Delete all occurencies of certain characters in a string. |
static boolean |
equalsTrueInSomeForm(java.lang.String s)
all of "true", "yes" or "1", case insensitive and trimmed, are considered 'true' |
static java.lang.String |
escapeForSQL(java.lang.String in)
Helper function for escaping strings that should be used in SQL queries. |
static java.lang.String |
escapeHTML(java.lang.String s)
Replaces characters that may be confused by a HTML parser with their equivalent character entity references. |
static java.lang.String |
escapeHTMLTag(java.lang.String aTagFragment)
Replace characters having special meaning inside HTML tags with their escaped equivalents, using character entities such as '&'. |
static java.lang.String |
escapeJavaLiteral(java.lang.String s)
Replaces characters that are not allowed in a Java style string literal with their escape characters. |
static java.lang.String |
escapeRegex(java.lang.String aRegexFragment)
Replace characters having special meaning in regular expressions with their escaped equivalents. |
static java.lang.String |
escapeSQL(java.lang.String s)
Replaces characters that may be confused by an SQL parser with their equivalent escape characters. |
static java.lang.String |
escapeString(java.lang.String in)
Escape all String delimiter with \ |
static java.lang.String |
escapeURL(java.lang.String aURLFragment)
Synonym for URLEncoder.encode(String, "UTF-8"). |
static java.lang.String |
filter(java.lang.String original,
java.lang.String filter)
Only allow those characters that are present in the filter filter("aAAcA-1%23", "ABCDEFGHIJKLMNOPQRSTIUVWXYZ1234567890") returns AAA123 |
static java.lang.String |
filterChars(java.lang.String from,
java.lang.String chars)
Leave any of the characters in chars in the string |
static java.lang.String |
firstWord(java.lang.String s)
|
static java.lang.String |
flattenString(java.lang.String in)
throw away all line feeds |
static java.lang.String |
fromJavaStringLiteral(java.lang.String str)
Convert a backslash notation into a String (e.g. |
static java.lang.String |
ifEmpty(java.lang.String s,
java.lang.String d)
if s is null or length=0 return d else s |
static java.lang.String |
ifNull(java.lang.String s,
java.lang.String d)
if s is null return d else s |
static java.lang.String |
insertAt(java.lang.String s,
int startIdx,
java.lang.String s2)
insert a piece into a string |
static boolean |
isEmpty(java.lang.String s)
true if s is null or length=0 |
static boolean |
isMatch(java.lang.String pattern,
java.lang.String string)
This method does a wildcard match (using * and ?) on pattern with the given string. |
static boolean |
isNotEmpty(java.lang.String s)
true if s != null and length > 0 |
static java.lang.String |
left(java.lang.String aString,
int len)
equivalent to the basic "left" function |
static java.lang.String |
makeFirstLetterLowercase(java.lang.String id)
|
static java.lang.String |
makeFirstLetterUppercase(java.lang.String id)
|
static java.lang.String |
midpad(java.lang.String s,
int length)
Pad the beginning and end of the given String with spaces until the String is of the given length. |
static java.lang.String |
midpad(java.lang.String s,
int length,
char c)
Pad the beginning and end of the given String with the given character until the result is the desired length. |
static int |
nthIndexOfWord(java.lang.String s,
java.lang.String word,
int n)
return the index of the nth occurence of word. |
static java.lang.String |
nthWord(java.lang.String s,
int nr,
char delimiter)
return the nthWord delimited by a delimiter |
static java.lang.String |
postpad(java.lang.String s,
int length)
Pad the end of the given String with spaces until the String is of the given length. |
static java.lang.String |
postpad(java.lang.String s,
int length,
char c)
Append the given character to the String until the result is the desired length. |
static java.lang.String |
prepad(java.lang.String s,
int length,
char c)
Pre-pend the given character to the String until the result is the desired length. |
static java.lang.String |
pullToken(java.lang.StringBuffer buf,
java.lang.String delim)
Remove a token from a the beginning of a StringBuffer,
and return it. |
static java.lang.String |
removeChars(java.lang.String from,
java.lang.String chars)
Remove any of the characters in chars from the string |
static java.lang.String |
removeSubstring(java.lang.String s,
int startIdx,
int endIdx)
snip a portion from a string and return the remainder |
static java.lang.String |
repeat(java.lang.String s,
int n)
Repeat string S for N times |
static java.lang.String |
replace(java.lang.String strReplaceInThis,
java.lang.String strReplaceThis,
java.lang.String strWithThis)
replace function for strings instead of chars |
static java.lang.String |
replace(java.lang.String strReplaceInThis,
java.lang.String strReplaceThis,
java.lang.String strWithThis,
int fromIdx,
int toIdx)
Replace a substring in a string but only between the specified indexes (substring). |
static java.lang.String |
replaceFirst(java.lang.String strReplaceInThis,
java.lang.String strReplaceThis,
java.lang.String strWithThis)
replace function for strings instead of chars, only replace first occurence |
static java.lang.String |
replaceSubstring(java.lang.String s,
int startIdx,
int endIdx,
java.lang.String s2)
remove a piece from within a string and insert new at that location |
static java.lang.String |
reverse(java.lang.String s)
reverse a string |
static java.lang.String |
right(java.lang.String aString,
int len)
equivalent to the basic "right" function |
static byte[] |
stringToByteArray(java.lang.String str)
Returns a java string as a byte array |
static byte[] |
stringToByteArray(java.lang.String str,
int offset,
int length)
Returns a java string as a byte array |
static java.lang.String |
subWords(java.lang.String s,
int start)
Get the string starting from the start'th word until end. |
static java.lang.String |
subWords(java.lang.String s,
int start,
int end)
Get the string starting from the start'th word ending before the end'th word |
static java.lang.String |
toHexString(byte[] b)
|
static java.lang.String |
toJavaStringLiteral(java.lang.String str,
boolean useRaw)
Convert a String into backslash notation (e.g. |
static java.lang.String[] |
tokenizeDelimitedToArray(java.lang.String toBeDelimited,
java.lang.String delimiters)
Tokenize on each character |
static java.lang.String[] |
tokenizeDelimitedToArray(java.lang.String toBeDelimited,
java.lang.String delimiters,
boolean returnDelims)
Return an array of String from a String containing delimited values. |
static java.lang.String |
toRoman(int num)
Convert an integer to Roman notation |
static java.lang.String |
translate(java.lang.String str,
java.lang.String fromChars,
java.lang.String toChars)
Translate characters in a string, like the t// operator in perl. |
static java.lang.String |
unescapeHTML(java.lang.String s)
Turn any HTML escape entities in the string into characters and return the resulting string. |
static java.lang.String |
wrap(java.lang.String text,
int width)
lines may not be longer than X characters (words are cut) |
static java.lang.String |
wrapApproximate(java.lang.String aString,
int approximateWidth)
wraps a string to a maximum length by inserting newlines |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public static final java.lang.String SOURCECODE_VERSION
public static final java.lang.String FILTERCHARS_LOWERLETTERS
public static final java.lang.String FILTERCHARS_UPPERLETTERS
public static final java.lang.String FILTERCHARS_DIGITS
public static final java.lang.String FILTERCHARS_PUNCTUATION
public static final java.lang.String FILTERCHARS_BACKSLASH
public static final java.lang.String FILTERCHARS_QUOTES
| Constructor Detail |
|---|
public StringUtil()
| Method Detail |
|---|
public static java.lang.String[] tokenizeDelimitedToArray(java.lang.String toBeDelimited,
java.lang.String delimiters,
boolean returnDelims)
delimited - the String value to tokenizedelimiters - the delimiter ("," or ";" for example)
public static java.lang.String[] tokenizeDelimitedToArray(java.lang.String toBeDelimited,
java.lang.String delimiters)
toBeDelimited - delimiters -
public static byte[] stringToByteArray(java.lang.String str)
public static byte[] stringToByteArray(java.lang.String str,
int offset,
int length)
public static java.lang.String byteArrayToString(byte[] ba,
int offset,
int len)
public static java.lang.String byteArrayToString(byte[] ba)
public static java.lang.String wrapApproximate(java.lang.String aString,
int approximateWidth)
aString - approximateWidth -
public static java.lang.String wrap(java.lang.String text,
int width)
public static java.lang.String left(java.lang.String aString,
int len)
aString - len -
public static java.lang.String right(java.lang.String aString,
int len)
aString - len -
public static java.lang.String replace(java.lang.String strReplaceInThis,
java.lang.String strReplaceThis,
java.lang.String strWithThis)
strReplaceInThis - in this stringstrReplaceThis - this orignal textstrWithThis - with this text
public static java.lang.String replaceFirst(java.lang.String strReplaceInThis,
java.lang.String strReplaceThis,
java.lang.String strWithThis)
strReplaceInThis - in this stringstrReplaceThis - this orignal textstrWithThis - with this text
public static java.lang.String replace(java.lang.String strReplaceInThis,
java.lang.String strReplaceThis,
java.lang.String strWithThis,
int fromIdx,
int toIdx)
strReplaceInThis - strReplaceThis - strWithThis - fromIdx - toIdx -
public static java.lang.String escapeURL(java.lang.String aURLFragment)
Used to ensure that HTTP query strings are in proper form, by escaping special characters such as spaces.
An example use case for this method is a login scheme in which, after successful login, the user is redirected to the "original" target destination. Such a target might be passed around as a request parameter. Such a request parameter will have a URL as its value, as in "LoginTarget=Blah.jsp?this=that&blah=boo", and would need to be URL-encoded in order to escape its special characters.
It is important to note that if a query string appears in an HREF attribute, then there are two issues - ensuring the query string is valid HTTP (it is URL-encoded), and ensuring it is valid HTML (ensuring the ampersand is escaped).
public static java.lang.String escapeHTMLTag(java.lang.String aTagFragment)
The escaped characters are :
Use cases for this method include :
public static java.lang.String escapeRegex(java.lang.String aRegexFragment)
The escaped characters include :
public static java.lang.String escapeHTML(java.lang.String s)
Any data that will appear as text on a web page should be be escaped. This is especially important for data that comes from untrusted sources such as Internet users. A common mistake in CGI programming is to ask a user for data and then put that data on a web page. For example:
Server: What is your name? User: <b>Joe<b> Server: Hello Joe, WelcomeIf the name is put on the page without checking that it doesn't contain HTML code or without sanitizing that HTML code, the user could reformat the page, insert scripts, and control the the content on your web server.
This method will replace HTML characters such as > with their HTML entity reference (>) so that the html parser will be sure to interpret them as plain text rather than HTML or script.
This method should be used for both data to be displayed in text
in the html document, and data put in form elements. For example:
<html><body>This in not a <tag>
in HTML</body></html>
and
<form><input type="hidden" name="date" value="This data could
be "malicious""></form>
In the second example, the form data would be properly be resubmitted
to your cgi script in the URLEncoded format:
This data could be %22malicious%22
s - String to be escaped
java.lang.NullPointerException - if s is null.public static java.lang.String unescapeHTML(java.lang.String s)
s - String to be unescaped.
java.lang.NullPointerException - if s is null.public static java.lang.String escapeSQL(java.lang.String s)
Any data that will be put in an SQL query should be be escaped. This is especially important for data that comes from untrusted sources such as Internet users.
For example if you had the following SQL query:
"SELECT * FROM addresses WHERE name='" + name + "' AND private='N'"
Without this function a user could give " OR 1=1 OR ''='"
as their name causing the query to be:
"SELECT * FROM addresses WHERE name='' OR 1=1 OR ''='' AND private='N'"
which will give all addresses, including private ones.
Correct usage would be:
"SELECT * FROM addresses WHERE name='" + StringHelper.escapeSQL(name) + "' AND private='N'"
Another way to avoid this problem is to use a PreparedStatement with appropriate placeholders.
s - String to be escaped
java.lang.NullPointerException - if s is null.public static java.lang.String escapeJavaLiteral(java.lang.String s)
s - String to be escaped
public static java.lang.String prepad(java.lang.String s,
int length,
char c)
If a String is longer than the desired length, it will not be truncated, however no padding will be added. Most notably this can be used to generate leading zero's: prepad("" + some_int_being_one, 5, '0') results in "00001".
s - String to be padded.length - desired length of result.c - padding character.
java.lang.NullPointerException - if s is null.
public static java.lang.String postpad(java.lang.String s,
int length)
If a String is longer than the desired length, it will not be truncated, however no padding will be added.
s - String to be padded.length - desired length of result.
java.lang.NullPointerException - if s is null.
public static java.lang.String postpad(java.lang.String s,
int length,
char c)
If a String is longer than the desired length, it will not be truncated, however no padding will be added.
s - String to be padded.length - desired length of result.c - padding character.
java.lang.NullPointerException - if s is null.
public static java.lang.String midpad(java.lang.String s,
int length)
If the number of characters to pad is even, then the padding will be split evenly between the beginning and end, otherwise, the extra character will be added to the end.
If a String is longer than the desired length, it will not be truncated, however no padding will be added.
s - String to be padded.length - desired length of result.
java.lang.NullPointerException - if s is null.
public static java.lang.String repeat(java.lang.String s,
int n)
s - n -
public static java.lang.String midpad(java.lang.String s,
int length,
char c)
If the number of characters to pad is even, then the padding will be split evenly between the beginning and end, otherwise, the extra character will be added to the end.
If a String is longer than the desired length, it will not be truncated, however no padding will be added.
s - String to be padded.length - desired length of result.c - padding character.
java.lang.NullPointerException - if s is null.public static java.lang.String toHexString(byte[] b)
public static java.lang.String byteToHex(byte b)
b -
public static java.lang.String byteToHex(byte[] b)
b -
public static java.lang.String charToHex(char c)
c -
public static boolean isMatch(java.lang.String pattern,
java.lang.String string)
pattern - the wildcard pattern to usestring - the string to check against pattern
public static java.lang.String subWords(java.lang.String s,
int start)
s - string that contains words separated by blanks
public static java.lang.String subWords(java.lang.String s,
int start,
int end)
public static java.lang.String nthWord(java.lang.String s,
int nr,
char delimiter)
public static java.lang.String firstWord(java.lang.String s)
public static int nthIndexOfWord(java.lang.String s,
java.lang.String word,
int n)
public static java.lang.String escapeString(java.lang.String in)
public static java.lang.String escapeForSQL(java.lang.String in)
public static java.lang.String flattenString(java.lang.String in)
public static int countWords(java.lang.String s,
java.lang.String delimiter)
public static java.lang.String translate(java.lang.String str,
java.lang.String fromChars,
java.lang.String toChars)
public static java.lang.String deleteChars(java.lang.String str,
java.lang.String chars)
public static java.lang.String pullToken(java.lang.StringBuffer buf,
java.lang.String delim)
StringBuffer,
and return it. The delim argument is a string
of possible characters that may ends the token.
This method does not return an empty ("") string - instead
null is returned.
This is a "helper" method, should probably be found in some other class. Let me know if you find a suitable replacement.
buf - the line with words to remove a word fromdelim - string of characters that may end token
public static boolean contains(java.lang.String str,
java.lang.String sub)
This method is provided merely for improved code
readability. It is equivalent to
str.indexOf(sub) != -1.
public static boolean contains(java.lang.String str,
char chr)
This method is provided merely for improved code
readability. It is equivalent to
str.indexOf(chr) != -1.
public static java.lang.String toJavaStringLiteral(java.lang.String str,
boolean useRaw)
str - useRaw -
public static java.lang.String fromJavaStringLiteral(java.lang.String str)
str - useRaw -
public static java.lang.String filter(java.lang.String original,
java.lang.String filter)
original - filter -
public static java.lang.String replaceSubstring(java.lang.String s,
int startIdx,
int endIdx,
java.lang.String s2)
s - startIdx - endIdx -
public static java.lang.String removeSubstring(java.lang.String s,
int startIdx,
int endIdx)
s - startIdx - endIdx -
public static java.lang.String insertAt(java.lang.String s,
int startIdx,
java.lang.String s2)
s - startIdx - s2 -
public static java.lang.String reverse(java.lang.String s)
public static java.lang.String toRoman(int num)
num -
public static java.lang.String convertStandardTypesToString(java.lang.Object o)
s - c -
public static java.lang.Object convertStringToStandardTypes(java.lang.String s,
java.lang.Class c)
s - c -
public static java.lang.String ifNull(java.lang.String s,
java.lang.String d)
s - d -
public static java.lang.String ifEmpty(java.lang.String s,
java.lang.String d)
s - d -
public static boolean isEmpty(java.lang.String s)
s -
public static boolean isNotEmpty(java.lang.String s)
s -
public static java.lang.String removeChars(java.lang.String from,
java.lang.String chars)
from - chars -
public static java.lang.String filterChars(java.lang.String from,
java.lang.String chars)
from - chars - public static boolean equalsTrueInSomeForm(java.lang.String s)
s -
public static java.lang.String makeFirstLetterLowercase(java.lang.String id)
id -
public static java.lang.String makeFirstLetterUppercase(java.lang.String id)
id -
public static int count(java.lang.String text,
java.lang.String pattern)
count("ababababab", "abab") returns 2.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||