public class StringUtil extends Object
| Modifier and Type | Field and Description |
|---|---|
static String |
EMPTY_STRING |
static String |
LINE_BREAKS |
static String |
WHITE_SPACES |
| Modifier and Type | Method and Description |
|---|---|
static boolean |
allAscii(String s)
Determines if a string contains only ascii characters
|
static void |
appendHexJavaScriptRepresentation(StringBuilder sb,
char c)
Returns a javascript representation of the character in a hex escaped
format.
|
static String |
arrayMap2String(Map<String,String[]> map,
String keyValueDelim,
String entryDelim)
Serializes a map
|
static String |
bytesToHexString(byte[] bytes)
Convert a byte array to a hex-encoding string: "a33bff00..."
|
static String |
bytesToHexString(byte[] bytes,
Character delimiter)
Convert a byte array to a hex-encoding string with the specified
delimiter: "a3<delimiter>3b<delimiter>ff..."
|
static String |
bytesToLatin1(byte[] ba)
Convert a byte array to a String using Latin-1 (aka ISO-8859-1) encoding.
|
static List<String> |
bytesToStringList(byte[] bytes)
Convert an array of bytes into a List of Strings using UTF-8.
|
static String |
bytesToUtf8(byte[] ba)
Convert a byte array to a String using UTF-8 encoding.
|
static String |
capitalize(String s)
Returns a string that is equivalent to the specified string with its
first character converted to uppercase as by
String.toUpperCase(java.util.Locale). |
static String |
collapse(String str,
String chars,
String replacement)
Replaces any string of matched characters with the supplied string.
|
static String |
collapseWhitespace(String str)
Replaces any string of adjacent whitespace characters with the whitespace
character " ".
|
static String |
Collection2String(Collection<?> in,
String separator)
Deprecated.
Please use
But note that
Join does not consider null elements to be
equivalent to the empty string, as this method does. |
static boolean |
containsCharRef(String s)
Determines if a string contains what looks like an html character
reference.
|
static String |
convertEOLToCRLF(String input)
Deprecated.
Please inline this method.
|
static String |
convertEOLToLF(String input)
Converts any instances of "\r" or "\r\n" style EOLs into "\n" (Line Feed).
|
static void |
copyStreams(InputStream in,
OutputStream out)
Copy all data from in to out in 4096 byte chunks.
|
static String |
cropBetween(String in,
char limit)
The old interface to cropBetween - using a single char limit
|
static String |
cropBetween(String in,
String limit)
This removes characters between maching charLimit chars.
|
static int |
displayWidth(char ch)
Returns the approximate display width of the character, measured
in units of ascii characters.
|
static int |
displayWidth(String s)
Returns the approximate display width of the string, measured in units of
ascii characters.
|
static byte[] |
encodingToBytes(String str,
String encoding)
Convert a String to a byte array using the specified encoding.
|
static boolean |
equals(String s1,
String s2)
Compares two strings, guarding against nulls If both Strings are null we
return true
|
static String |
expandShardNames(String dbSpecComponent) |
static String |
fixedWidth(String[] lines,
int width)
Reformats the given array of lines to a fixed width by inserting
carriage returns and trimming unnecessary whitespace.
|
static String |
fixedWidth(String str,
int width)
Reformats the given string to a fixed width by inserting
carriage returns and trimming unnecessary whitespace.
|
static byte[] |
hexToBytes(String str)
Convert a string of hex digits to a byte array, with the first
byte in the array being the MSB.
|
static String |
htmlEscape(String s)
Escapes special characters (& < > ") from a string so it can safely be
included in an HTML document.
|
static String |
indent(String iString,
int iIndentDepth)
Indents the given String per line.
|
static int |
indexOfChars(String str,
String chars)
Like String.indexOf() except that it will look for any of the
characters in 'chars' (similar to C's strpbrk)
|
static int |
indexOfChars(String str,
String chars,
int fromIndex)
Like String.indexOf() except that it will look for any of the
characters in 'chars' (similar to C's strpbrk)
|
static String |
insertBreakingWhitespace(int lineLen,
String original)
Inserts spaces every splitLen characters so that the string will wrap.
|
static boolean |
isCjk(char ch)
Determines if a character is a CJK ideograph or a character typically
used only in CJK text.
|
static boolean |
isCjk(int codePoint)
Determines if a character is a CJK ideograph or a character typically
used only in CJK text.
|
static boolean |
isCjk(String s)
Determines if a string is a CJK word.
|
static boolean |
isEmpty(String s)
Helper function for null and empty string testing.
|
static boolean |
isEmptyOrWhitespace(String s)
Helper function for null, empty, and whitespace string testing.
|
static boolean |
isHebrew(int codePoint)
Determines if a character is a Hebrew character.
|
static boolean |
isHebrew(String s)
Determines if a string is a Hebrew word.
|
static String |
Iterator2String(Iterator<?> it,
String separator)
Deprecated.
Please use
But note that
Join does not consider null elements to be
equivalent to the empty string, as this method does. |
static String |
javaEscape(String s)
We escape some characters in s to be able to insert strings into Java code
|
static String |
javaEscapeWithinAttribute(String s)
Escape a string so that it can be safely placed as value of an
attribute.
|
static String |
javaScriptEscape(String s)
We escape some characters in s to be able to insert strings into JavaScript
code.
|
static String |
javaScriptEscapeToAscii(String s)
We escape some characters in s to be able to insert strings into JavaScript
code.
|
static String |
javaScriptUnescape(String s)
Undo escaping as performed in javaScriptEscape(.)
Throws an IllegalArgumentException if the string contains
bad escaping.
|
static String |
javaUtilRegexEscape(String s)
Escapes the special characters from a string so it can be used as part of
a regex pattern.
|
static String |
join(Collection tokens,
String delimiter)
Deprecated.
Please use
But note that
Join does not consider null elements to be
equivalent to the empty string, as this method does. |
static String |
join(Object[] tokens,
String delimiter)
Deprecated.
Please use
But note that
Join does not consider null elements to be
equivalent to the empty string, as this method does. |
static String |
joinInts(int[] tokens,
String delimiter)
Concatenates the given int[] array into one String, inserting a delimiter
between each pair of elements.
|
static String |
joinLongs(long[] tokens,
String delimiter)
Concatenates the given long[] array into one String, inserting a delimiter
between each pair of elements.
|
static int |
lastIndexNotOf(String str,
String chars,
int fromIndex)
Finds the last index in str of a character not in the characters
in 'chars' (similar to ANSI string.find_last_not_of).
|
static String |
lastToken(String s,
String delimiter)
Splits s with delimiters in delimiter and returns the last token
|
static byte[] |
latin1ToBytes(String str)
Convert a String to a byte array using Latin-1 (aka ISO-8859-1) encoding.
|
static String |
list2String(Collection<?> in,
String separator)
Deprecated.
Please use
But note that
Join does not consider null elements to be
equivalent to the empty string, as this method does. |
static <V> Map |
lowercaseKeys(Map<String,V> map)
Given a map, creates and returns a new map in which all keys are the
lower-cased version of each key.
|
static String |
lstrip(String str)
Deprecated.
ensure the string is not null and use
CharMatcher.LEGACY_WHITESPACE.trimLeadingFrom(str); also consider whether you
really want the legacy whitespace definition, or something more
standard like CharMatcher.WHITESPACE. |
static String |
makeSafe(String s)
Helper function for making null strings safe for comparisons, etc.
|
static <K,V> String |
map2String(Map<K,V> in,
String sepKey,
String sepEntry)
This function concatenates the elements of a Map in a string with form
"
|
static String |
maskLeft(String s,
int len,
char mask_ch)
Returns a string consisting of "s", with each of the first "len" characters
replaced by "mask_ch" character.
|
static String |
maskRight(String s,
int len,
char mask_ch)
Returns a string consisting of "s", with each of the last "len" characters
replaces by "mask_ch" character.
|
static String |
megastrip(String str,
boolean left,
boolean right,
String what)
Deprecated.
ensure the string is not null and use
|
static int |
numSharedChars(String str,
String chars)
Counts the number of (not necessarily distinct) characters in the
string that also happen to be in 'chars'
|
static String |
padLeft(String s,
int len,
char pad_ch)
Returns a string consisting of "s", plus enough copies of "pad_ch" on the
left hand side to make the length of "s" equal to or greater than len (if
"s" is already longer than "len", then "s" is returned).
|
static String |
padRight(String s,
int len,
char pad_ch)
Returns a string consisting of "s", plus enough copies of "pad_ch" on the
right hand side to make the length of "s" equal to or greater than len (if
"s" is already longer than "len", then "s" is returned).
|
static String[] |
parseDelimitedList(String list,
char delimiter)
Parse a list of substrings separated by a given delimiter.
|
static String |
pythonEscape(String s)
We escape some characters in s to be able to make the string executable
from a python string
|
static String |
regexEscape(String s)
Escapes the special characters from a string so it can be used as part of
a regex pattern.
|
static String |
regexReplacementEscape(String s)
Escapes the '\' and '$' characters, which comprise the subset of regex
characters that has special meaning in methods such as:
|
static String |
removeChars(String str,
String oldchars)
Remove any occurrances of 'oldchars' in 'str'.
|
static String |
repeat(String sourceString,
int factor)
Returns sourceString concatenated together 'factor' times.
|
static String |
replace(String str,
String what,
String with)
Deprecated.
Please use
String.replace(CharSequence, CharSequence). |
static String |
replaceChars(String str,
String oldchars,
char newchar)
Like String.replace() except that it accepts any number of old chars.
|
static String |
replaceSmartQuotes(String str)
Replaces microsoft "smart quotes" (curly " and ') with their
ascii counterparts.
|
static String |
retainAllChars(String str,
String retainChars)
Removes all characters from 'str' that are not in 'retainChars'.
|
static String |
rstrip(String str)
Deprecated.
ensure the string is not null and use
CharMatcher.LEGACY_WHITESPACE.trimTrailingFrom(str); also consider whether you
really want the legacy whitespace definition, or something more
standard like CharMatcher.WHITESPACE. |
static String[] |
split(String str,
String delims)
Split "str" by run of delimiters and return.
|
static String[] |
split(String str,
String delims,
boolean trimTokens)
Split "str" into tokens by delimiters and optionally remove white spaces
from the splitted tokens.
|
static String[] |
splitAndTrim(String str,
String delims)
Short hand for
split(str, delims, true) |
static int[] |
splitInts(String str)
Parse comma-separated list of ints and return as array.
|
static long[] |
splitLongs(String str)
Parse comma-separated list of longs and return as array.
|
static String |
stream2String(InputStream is,
int maxLength)
Read a String of up to maxLength bytes from an InputStream
|
static Collection<String> |
string2Collection(String in,
String delimiter,
boolean doStrip,
Collection<String> collection)
Converts a delimited string to a collection of strings.
|
static LinkedList<String> |
string2List(String in,
String delimiter,
boolean doStrip)
This converts a String to a list of strings by extracting the substrings
between delimiter
|
static HashMap<String,String> |
string2Map(String in,
String delimEntry,
String delimKey,
boolean doStripEntry)
This converts a string to a Map.
|
static Set |
string2Set(String in,
String delimiter,
boolean doStrip)
This converts a String to a Set of strings by extracting the substrings
between delimiter
|
static String |
strip(String str)
strip - strips both ways
|
static String |
stripAndCollapse(String str)
Strip white spaces from both end, and collapse white spaces
in the middle.
|
static String |
stripHtmlTags(String string)
Given a
String, returns an equivalent String with
all HTML tags stripped. |
static String |
stripNonDigits(String str)
Strips all non-digit characters from a string.
|
static String |
stripPrefix(String str,
String prefix)
Give me a string and a potential prefix, and I return the string
following the prefix if the prefix matches, else null.
|
static String |
stripPrefixIgnoreCase(String str,
String prefix)
Case insensitive version of stripPrefix.
|
static String |
toNullIfEmpty(String s)
Helper function for making empty strings into a null.
|
static String |
toNullIfEmptyOrWhitespace(String s)
Helper function for turning empty or whitespace strings into a null.
|
static String |
toString(float[] iArray) |
static String |
toString(int[] iArray) |
static String |
toString(int[][] iArray) |
static String |
toString(long[] iArray) |
static String |
toString(long[][] iArray) |
static String |
toString(Object[] obj) |
static String |
toString(String s)
Returns the string, in single quotes, or "NULL".
|
static String |
toString(String[] iArray) |
static String |
toUpperCase(String src)
Safely convert the string to uppercase.
|
static InputStream |
toUTF8InputStream(String str)
Replacement for deprecated StringBufferInputStream().
|
static String |
unescapeCString(String s)
Unescape any C escape sequences (\n, \r, \\, \ooo, etc) and return the
resulting string.
|
static String |
unescapeHTML(String s)
Replace all the occurences of HTML escape strings with the
respective characters.
|
static String |
unescapeMySQLString(String s)
Unescape any MySQL escape sequences.
|
static String |
unicodeEscape(String s)
Replaces each non-ascii character in s with its Unicode escape sequence
\\uxxxx where xxxx is a hex number.
|
static byte[] |
utf8ToBytes(String str)
Convert a String to a byte array using UTF-8 encoding.
|
static String |
xmlCDataEscape(String s)
Escape a string that is meant to be embedded in a CDATA section.
|
static String |
xmlContentEscape(String s)
Escape a string for use inside as XML element content.
|
static String |
xmlEscape(String s)
Returns a form of "s" appropriate for including in an XML document, after
escaping certain special characters (e.g.
|
static String |
xmlSingleQuotedEscape(String s)
Escape a string for use inside as XML single-quoted attributes.
|
public static final String EMPTY_STRING
public static final String WHITE_SPACES
public static final String LINE_BREAKS
public static String[] split(String str, String delims)
public static String[] split(String str, String delims, boolean trimTokens)
trimTokens - if true, then trim the tokenspublic static String[] splitAndTrim(String str, String delims)
split(str, delims, true)public static int[] splitInts(String str) throws IllegalArgumentException
IllegalArgumentExceptionpublic static long[] splitLongs(String str) throws IllegalArgumentException
IllegalArgumentExceptionpublic static String joinInts(int[] tokens, String delimiter)
public static String joinLongs(long[] tokens, String delimiter)
@Deprecated public static String join(Object[] tokens, String delimiter)
Join does not consider null elements to be
equivalent to the empty string, as this method does.This includes the String[] case, because if s is a String, then s.toString() returns s.
@Deprecated public static String join(Collection tokens, String delimiter)
Join does not consider null elements to be
equivalent to the empty string, as this method does.join(Object[],String), but takes a Collection
instead.@Deprecated public static String replace(String str, String what, String with)
String.replace(CharSequence, CharSequence).str - - the string o processwhat - - to replacewith - - replace with thispublic static String fixedWidth(String str, int width)
str - the string to formatwidth - the fixed width (in characters)public static String fixedWidth(String[] lines, int width)
lines - - array of lines to formatwidth - - the fixed width (in characters)public static String insertBreakingWhitespace(int lineLen, String original)
lineLen - the length of the substrings to separate with spaces.original - the original Stringpublic static String indent(String iString, int iIndentDepth)
iString - The string to indent.iIndentDepth - The depth of the indentation.@Deprecated public static String megastrip(String str, boolean left, boolean right, String what)
CharMatcher.anyOf(what).trimFrom(str)
if left == true and right == true
CharMatcher.anyOf(what).trimLeadingFrom(str)
if left == true and right == false
CharMatcher.anyOf(what).trimTrailingFrom(str)
if left == false and right == true
str - the string to stripleft - strip from leftright - strip from rightwhat - character(s) to strip@Deprecated public static String lstrip(String str)
CharMatcher.LEGACY_WHITESPACE.trimLeadingFrom(str); also consider whether you
really want the legacy whitespace definition, or something more
standard like CharMatcher.WHITESPACE.str - what to strip@Deprecated public static String rstrip(String str)
CharMatcher.LEGACY_WHITESPACE.trimTrailingFrom(str); also consider whether you
really want the legacy whitespace definition, or something more
standard like CharMatcher.WHITESPACE.str - what to strippublic static String strip(String str)
str - what to strippublic static String stripAndCollapse(String str)
str - what to strippublic static String stripPrefix(String str, String prefix)
public static String stripPrefixIgnoreCase(String str, String prefix)
public static String stripNonDigits(String str)
str - the string to strippublic static int numSharedChars(String str, String chars)
public static int indexOfChars(String str, String chars, int fromIndex)
public static int indexOfChars(String str, String chars)
public static int lastIndexNotOf(String str, String chars, int fromIndex)
public static String replaceChars(String str, String oldchars, char newchar)
public static String removeChars(String str, String oldchars)
public static String retainAllChars(String str, String retainChars)
public static String replaceSmartQuotes(String str)
public static byte[] hexToBytes(String str)
public static String convertEOLToLF(String input)
@Deprecated public static String convertEOLToCRLF(String input)
public static String padLeft(String s, int len, char pad_ch)
public static String padRight(String s, int len, char pad_ch)
public static String maskLeft(String s, int len, char mask_ch)
public static String maskRight(String s, int len, char mask_ch)
public static String unescapeCString(String s)
public static String unescapeMySQLString(String s) throws IllegalArgumentException
s - string to unescape, with the surrounding quotes.IllegalArgumentException - if s is not a valid MySQL string.public static final String unescapeHTML(String s)
s - a String valueString valuepublic static String stripHtmlTags(String string)
String, returns an equivalent String with
all HTML tags stripped. Note that HTML entities, such as "&" will
still be preserved.public static String pythonEscape(String s)
public static String javaScriptEscape(String s)
public static String javaScriptEscapeToAscii(String s)
public static void appendHexJavaScriptRepresentation(StringBuilder sb, char c)
sb - The buffer to which the hex representation should be appended.c - The character to be appended.public static String javaScriptUnescape(String s)
public static String xmlContentEscape(String s)
public static String xmlSingleQuotedEscape(String s)
public static String xmlCDataEscape(String s)
<[!CDATA[...]]>
The only invalid character sequence in a CDATA tag is "]]>".
If this sequence is present in the input string, we replace
it by closing the current CDATA field, then write ']]>',
then reopen a new CDATA section.public static String javaEscape(String s)
public static String javaEscapeWithinAttribute(String s)
javaEscape(java.lang.String) except that it escapes double quote
to the HTML literal ". This is to prevent the double
quote from being interpreted as the character closing the
attribute.public static String xmlEscape(String s)
public static String htmlEscape(String s)
xmlEscape except that
htmlEscape does not escape the apostrophe character).public static String regexEscape(String s)
public static String javaUtilRegexEscape(String s)
public static String regexReplacementEscape(String s)
java.util.regex.Matcher.appendReplacement(sb, replacement);
java.lang.String.replaceAll(str, replacement);Note that this method is offered in java version 1.5 as the method
java.util.regex.Matcher.quoteReplacement(String);
public static String cropBetween(String in, char limit)
public static String cropBetween(String in, String limit)
in - - the string to processlimit - - the limit of the string(s) to removepublic static LinkedList<String> string2List(String in, String delimiter, boolean doStrip)
in - - what to processdelimiter - - the delimiting stringdoStrip - - to strip the substrings before adding to the listpublic static Set string2Set(String in, String delimiter, boolean doStrip)
in - - what to processdelimiter - - the delimiting stringdoStrip - - to strip the substrings before adding to the listpublic static Collection<String> string2Collection(String in, String delimiter, boolean doStrip, Collection<String> collection)
in - The delimited input string to processdelimiter - The string delimiting entries in the input string.doString - Whether to strip the substrings before adding to the
collectioncollection - The collection to which the strings will be added. If
null, a new List will be created.@Deprecated public static String list2String(Collection<?> in, String separator)
Join does not consider null elements to be
equivalent to the empty string, as this method does.@Deprecated public static String Collection2String(Collection<?> in, String separator)
Join does not consider null elements to be
equivalent to the empty string, as this method does.in - - the collection that has to be conatenatedseparator - - a string to sepparate the elements from the list@Deprecated public static String Iterator2String(Iterator<?> it, String separator)
Join does not consider null elements to be
equivalent to the empty string, as this method does.public static HashMap<String,String> string2Map(String in, String delimEntry, String delimKey, boolean doStripEntry)
in - - the string to be processeddelimEntry - - delimiter for the entriesdelimKey - - delimiter between keys and valuesdoStripEntry - - strip entries before inserting in the mappublic static <K,V> String map2String(Map<K,V> in, String sepKey, String sepEntry)
in - - the map to be convertedsepKey - - the separator to put between key and valuesepEntry - - the separator to put between map entriespublic static <V> Map lowercaseKeys(Map<String,V> map)
map - A map containing String keys to be lowercasedIllegalArgumentException - if the map contains duplicate string keys
after lower casingpublic static String collapseWhitespace(String str)
str - the string you want to mungecollapsepublic static String collapse(String str, String chars, String replacement)
This is a more general version of collapseWhitespace.
E.g. collapse("hello world", " ", "::")
will return the following string: "hello::world"
str - the string you want to mungechars - all of the characters to be considered for mungereplacement - the replacement stringpublic static String stream2String(InputStream is, int maxLength) throws IOException
is - input streammaxLength - max number of bytes to read from "is". If this is -1, we
read everything.IOExceptionpublic static String[] parseDelimitedList(String list, char delimiter)
list - String containing delimited substringsdelimiter - Delimiter (anything except ' ' is allowed)public static boolean isEmpty(String s)
public static boolean isEmptyOrWhitespace(String s)
public static String makeSafe(String s)
public static String toNullIfEmpty(String s)
public static String toNullIfEmptyOrWhitespace(String s)
public static String arrayMap2String(Map<String,String[]> map, String keyValueDelim, String entryDelim)
map - A map of String keys to arrays of String valueskeyValueDelim - Delimiter between keys and valuesentryDelim - Delimiter between entriespublic static boolean equals(String s1, String s2)
public static String lastToken(String s, String delimiter)
public static boolean allAscii(String s)
public static boolean containsCharRef(String s)
public static boolean isHebrew(String s)
isHebrew(int) is true for any of its characters.public static boolean isHebrew(int codePoint)
public static boolean isCjk(String s)
isCjk(char) is true for any of its characters.public static boolean isCjk(char ch)
isCjk(int).public static boolean isCjk(int codePoint)
public static String unicodeEscape(String s)
public static int displayWidth(String s)
displayWidth(char)public static int displayWidth(char ch)
public static String toString(float[] iArray)
public static String toString(long[] iArray)
public static String toString(int[] iArray)
public static String toString(String[] iArray)
public static String toString(String s)
s - - the stringpublic static String toString(int[][] iArray)
public static String toString(long[][] iArray)
public static String toString(Object[] obj)
public static InputStream toUTF8InputStream(String str)
public static void copyStreams(InputStream in, OutputStream out) throws IOException
IOExceptionpublic static String bytesToLatin1(byte[] ba)
public static String bytesToHexString(byte[] bytes)
public static String bytesToHexString(byte[] bytes, Character delimiter)
public static byte[] latin1ToBytes(String str)
public static String bytesToUtf8(byte[] ba)
public static byte[] utf8ToBytes(String str)
public static byte[] encodingToBytes(String str, String encoding)
encoding - the encoding to usepublic static List<String> bytesToStringList(byte[] bytes)
bytes - the array to convertpublic static String toUpperCase(String src)
public static String expandShardNames(String dbSpecComponent) throws IllegalArgumentException, IllegalStateException
dbSpecComponent - a single component of a DBDescriptor spec
(e.g. the host or database component). The expected format of the string is:
IllegalArgumentException - if the string does not parse.IllegalStateExceptionpublic static String repeat(String sourceString, int factor)
sourceString - The string to repeatfactor - The number of times to repeat it.public static String capitalize(String s)
String.toUpperCase(java.util.Locale).
The returned string will have the same value as the specified string if
its first character is non-alphabetic, if its first character is already
uppercase, or if the specified string is of length 0.
For example:
capitalize("foo bar").equals("Foo bar");
capitalize("2b or not 2b").equals("2b or not 2b")
capitalize("Foo bar").equals("Foo bar");
capitalize("").equals("");
s - the string whose first character is to be uppercasedNullPointerException - if s is nullCopyright © 2012. All Rights Reserved.