public class StringHelper
extends java.lang.Object
String
objects. This is intended to augment the limited
set of formatting and processing methods in the String
class. Methods are provided to left pad, right pad, center, trim and
truncate among other features.
Most (if not all) of these methods are stateless and thus are implemented as static methods.
Modifier and Type | Field and Description |
---|---|
private static java.lang.String |
LINE_SEP
Platform-specific line separator.
|
private static java.lang.String |
SLASH_STAR
Starting text for a slash-star style comment.
|
private static java.lang.String |
STAR_SLASH
Ending text for a slash-star style comment.
|
Constructor and Description |
---|
StringHelper() |
Modifier and Type | Method and Description |
---|---|
static java.lang.String |
centerText(java.lang.String text,
int width)
Create a new
String with the content of the passed
argument, centered in a space padded String of the
user-specified width. |
static java.lang.String |
centerText(java.lang.String text,
int width,
java.lang.StringBuilder buf)
Create a new
String in a user-created
StringBuilder with the content of the passed argument,
centered in a space padded String of the user-specified
width. |
static java.lang.String |
changeCase(java.lang.String name,
boolean upper)
Capitalize or decapitalize the given name by changing the case of its
first character.
|
static int |
countChar(java.lang.String text,
char c)
Counts the number of occurrences of the specified character in a string.
|
static boolean |
equal(java.lang.String s1,
java.lang.String s2)
Compare two (possibly
null ) strings |
static java.lang.String |
fixupFileName(java.lang.String text)
Fixing the file name for special Windows requirement.
|
static java.lang.String |
fixupRegex(java.lang.String text)
Fixing the regular expression string for special Windows requirement.
|
static java.lang.String |
generateBeanName(java.lang.String field,
boolean getter,
boolean bool)
Create a (mostly) Java-bean compatible getter or setter name for a
given field.
|
static java.lang.String |
indent(java.lang.Object o,
int padSize)
Create a string representation of
o and indent each line
of that representation with padSize whitespace characters. |
static java.lang.String |
indent(java.lang.Object o,
int padSize,
java.lang.StringBuilder buf)
Create a string representation of
o and indent each line
of that representation with padSize whitespace characters. |
static boolean |
isRepeatedChar(java.lang.StringBuilder buf,
char c)
Verifies whether the specified string buffer is made of copies of
a single character.
|
static java.lang.String |
leftAlignText(java.lang.String text,
int width)
Create a new
String with the content of the passed
argument, left-aligned and padded on the right with spaces up to the
user-specified width. |
static java.lang.String |
leftAlignText(java.lang.String text,
int width,
java.lang.StringBuilder buf)
Create a new
String in a user-created
StringBuilder with the content of the passed argument,
left-aligned and padded on the right with spaces up to the
user-specified width. |
static java.lang.String[] |
nolossWrapText(java.lang.String text,
int width)
A simple algorithm to wrap a text string to fit into one or more lines
of the specified width.
|
static java.util.List<java.lang.String> |
parseTokens(java.lang.String str,
java.lang.String delimiter,
boolean addIfStartsWithDelimiter,
boolean addIfEndsWithDelimiter,
boolean trim)
Get tokens from string.
|
static java.lang.String |
processEscapes(java.lang.String input)
Converts a string with valid Java escape sequences into a string in
which those sequences are converted to the corresponding character.
|
static java.lang.String |
removeChar(java.lang.String text,
char ch)
Removes all occurrences of the specified character in a string.
|
static void |
render(boolean[] array,
java.lang.StringBuilder buf)
Convert the given array into a string of comma separated elements which
starts with a '[' and ends with a ']'.
|
static void |
render(byte[] array,
java.lang.StringBuilder buf)
Convert the given array into a hexidecimal format for each byte, start
with a '[' and end with a ']'.
|
static void |
render(double[] array,
java.lang.StringBuilder buf)
Convert the given array into a string of comma separated elements which
starts with a '[' and ends with a ']'.
|
static void |
render(float[] array,
java.lang.StringBuilder buf)
Convert the given array into a string of comma separated elements which
starts with a '[' and ends with a ']'.
|
static void |
render(int[] array,
java.lang.StringBuilder buf)
Convert the given array into a string of comma separated elements which
starts with a '[' and ends with a ']'.
|
static void |
render(long[] array,
java.lang.StringBuilder buf)
Convert the given array into a string of comma separated elements which
starts with a '[' and ends with a ']'.
|
static java.lang.String |
render(java.lang.Object array)
Convert the given array into a string of comma separated elements which
starts with a '[' and ends with a ']'.
|
static void |
render(java.lang.Object array,
java.lang.StringBuilder buf)
Convert the given array into a string of comma separated elements which
starts with a '[' and ends with a ']'.
|
static void |
render(short[] array,
java.lang.StringBuilder buf)
Convert the given array into a string of comma separated elements which
starts with a '[' and ends with a ']'.
|
static java.lang.String |
repeatChar(char c,
int len)
Create a string composed of
c repeated len
times. |
static java.lang.String |
repeatChar(char c,
int len,
java.lang.StringBuilder buf)
Append
c to the specified string buffer len
times. |
static java.lang.String |
rightAlignText(java.lang.String text,
int width)
Create a new
String with the content of the passed
argument, right-aligned and padded on the left with spaces up to the
user-specified width. |
static java.lang.String |
rightAlignText(java.lang.String text,
int width,
java.lang.StringBuilder buf)
Create a new
String in a user-created
StringBuilder with the content of the passed argument,
right-aligned and padded on the left with spaces up to the
user-specified width. |
static java.lang.String |
safeTrim(java.lang.String text)
Trim leading and trailing whitespace from
text , without
triggering a NullPointerException if text is
null . |
static java.lang.String |
safeTrimLeading(java.lang.String text)
Trim leading whitespace from
text , without triggering
a NullPointerException if text is
null . |
static java.lang.String |
safeTrimTrailing(java.lang.String text)
Trim trailing whitespace from
text , without triggering a
NullPointerException if text is null . |
static java.lang.String |
safeTrimTrailingSpaces(java.lang.String text)
Trim trailing spaces from
text , without triggering a NullPointerException if
text is null . |
static java.lang.String |
stripEnclosing(java.lang.String text,
char c)
Strip a matched pair of the specified enclosing character, if it exists, from the
beginning and end of a string.
|
static java.lang.String |
stripEnclosing(java.lang.String text,
char[] ch)
Strip a matched pair of any of the specified enclosing characters, if they exist, from the
beginning and end of a string.
|
static java.lang.String |
stripMatchedComments(java.lang.String text)
Strip a matched pair of slash-star comments, including any nested matched pairs of comments
and return the result.
|
static java.lang.String |
sweep(java.lang.String text,
java.util.Map<java.lang.String,java.lang.String> parmMap)
Substitute all instances of
${key_name} (where
key_name identifies a valid replacement parameter)
in the specified text with the associated parameter value. |
static java.lang.String |
truncate(java.lang.String text,
int width)
Truncate input from
text to width characters
leaving behind an ellipses ("...") in cases where a truncation has
occurred. |
static java.lang.String[] |
wrapText(java.lang.String text,
int width)
A simple algorithm to wrap a text string to fit into one or more lines
of the specified width.
|
private static final java.lang.String SLASH_STAR
private static final java.lang.String STAR_SLASH
private static final java.lang.String LINE_SEP
public static java.lang.String sweep(java.lang.String text, java.util.Map<java.lang.String,java.lang.String> parmMap)
${key_name}
(where
key_name
identifies a valid replacement parameter)
in the specified text with the associated parameter value. Replacements
are specified as key and value pairs in the parameter map. Variables
are substituted in the order in which the key set is returned, which
may be undefined for some Map
implementations and will
be fixed and controllable by the caller for other Map
implementations.
Note: if a particular replacement key name cannot be found in the map of parameters, it is left untouched in the output string.
text
- String upon which variable substitutions are to be performed.parmMap
- A list of key and value pairs specifying the replacements
to be made.text
, with all variables substituted for their
parameter values.public static java.lang.String indent(java.lang.Object o, int padSize)
o
and indent each line
of that representation with padSize
whitespace characters.
The platform-specific line separator as returned by
System.getProperty
is used for parsing the input and in
generating the output.o
- Object to be rendered as a string.padSize
- Number of whitespace characters to indent each line of the
output.o
represented as a string, with each line
indented.public static java.lang.String indent(java.lang.Object o, int padSize, java.lang.StringBuilder buf)
o
and indent each line
of that representation with padSize
whitespace characters.
The platform-specific line separator as returned by
System.getProperty
is used for parsing the input and in
generating the output.o
- Object to be rendered as a string.padSize
- Number of whitespace characters to indent each line of the
output.buf
- Target buffer.o
represented as a string, with each line
indented.public static java.lang.String centerText(java.lang.String text, int width) throws java.lang.IllegalArgumentException
String
with the content of the passed
argument, centered in a space padded String
of the
user-specified width.text
- Text to be centered in the output String
. Note
that this text must not contain the platform-specific line
separator character(s).width
- Total number of characters in the resulting
String
. This must be greater than the current
size of the input text.java.lang.IllegalArgumentException
public static java.lang.String centerText(java.lang.String text, int width, java.lang.StringBuilder buf) throws java.lang.IllegalArgumentException
String
in a user-created
StringBuilder
with the content of the passed argument,
centered in a space padded String
of the user-specified
width.text
- Text to be centered in the output String
. Note
that this text must not contain the platform-specific line
separator character(s).width
- Total number of characters in the resulting
String
. This must be greater than the current
size of the input text.buf
- Buffer in which to create the resulting text.java.lang.IllegalArgumentException
public static java.lang.String rightAlignText(java.lang.String text, int width) throws java.lang.IllegalArgumentException
String
with the content of the passed
argument, right-aligned and padded on the left with spaces up to the
user-specified width.text
- Text to be right aligned in the output String
.
This text must not contain the platform-specific line
separator character(s).width
- Total number of characters in the resulting
String
. This must be greater than the current
size of the input text.java.lang.IllegalArgumentException
public static java.lang.String rightAlignText(java.lang.String text, int width, java.lang.StringBuilder buf) throws java.lang.IllegalArgumentException
String
in a user-created
StringBuilder
with the content of the passed argument,
right-aligned and padded on the left with spaces up to the
user-specified width.text
- Text to be right aligned in the output String
.
This text must not contain the platform-specific line
separator character(s).width
- Total number of characters in the resulting
String
. This must be greater than the current
size of the input text.buf
- Buffer in which to create the resulting text.java.lang.IllegalArgumentException
public static java.lang.String leftAlignText(java.lang.String text, int width) throws java.lang.IllegalArgumentException
String
with the content of the passed
argument, left-aligned and padded on the right with spaces up to the
user-specified width.text
- Text to be left aligned in the output String
.
This text must not contain the platform-specific line
separator character(s).width
- Total number of characters in the resulting
String
. This must be greater than the current
size of the input text.java.lang.IllegalArgumentException
public static java.lang.String leftAlignText(java.lang.String text, int width, java.lang.StringBuilder buf) throws java.lang.IllegalArgumentException
String
in a user-created
StringBuilder
with the content of the passed argument,
left-aligned and padded on the right with spaces up to the
user-specified width.text
- Text to be left aligned in the output String
.
This text must not contain the platform-specific line
separator character(s).width
- Total number of characters in the resulting
String
. This must be greater than the current
size of the input text.buf
- Buffer in which to create the resulting text.java.lang.IllegalArgumentException
public static java.lang.String[] wrapText(java.lang.String text, int width)
width
are not split, but will
appear in their own line. All lines are left-aligned within the
alloted width.
Warning: this algorithm will convert any sequential set of multiple
spaces between words into a single space between words. If this is
not wanted, then use nolossWrapText(java.lang.String, int)
instead.
text
- Text to be split.width
- Per-line width in characters.text
split into
lines of the specified width.public static java.lang.String[] nolossWrapText(java.lang.String text, int width)
width
are not split, but will
appear in their own line. If the text must be split into multiple lines
only the first line has its white-space preserved; all subsequent lines
lines will have their leading white-space removed.
This will honor embedded newlines as places to split a line. Then any lines that are still longer than the width will be wrapped as noted above.
text
- Text to be split.width
- Per-line width in characters.text
split into
lines of the specified width.public static java.lang.String truncate(java.lang.String text, int width)
text
to width
characters
leaving behind an ellipses ("...") in cases where a truncation has
occurred. If the size of text
is not greater than the
requested size, then no truncation will occur. Note that when the
ellipses is added, the total size (in characters) of the text will
not exceed the requested size.text
- Text string to be trimmed.width
- Total number of characters in the resulting output.String
object with the contents of
the input text
and an ellipses when truncation
has occurred.public static java.lang.String repeatChar(char c, int len)
c
repeated len
times.c
- Character from which to compose string.len
- Length of string to be created.public static java.lang.String repeatChar(char c, int len, java.lang.StringBuilder buf)
c
to the specified string buffer len
times.c
- Character to append to buf
.len
- Number of times to append c
.buf
- Target buffer.buf
which represents only the
new content appended (i.e., c
repeated
len
times.public static boolean isRepeatedChar(java.lang.StringBuilder buf, char c)
buf
- String buffer to verifyc
- Character to verify the buffer withyes
if the string buffer contains only
specified character and is non-empty.public static java.lang.String safeTrimTrailingSpaces(java.lang.String text)
text
, without triggering a NullPointerException
if
text
is null
. The tab, line feed, and carriage return are NOT trimmed.text
- Text string to be trimmed.text
with trailing spaces removed, or null
if text
is
null
.public static java.lang.String safeTrimTrailing(java.lang.String text)
text
, without triggering a
NullPointerException
if text
is null
. Whitespace includes space,
tab, line feed, and carriage return.text
- Text string to be trimmed.text
with trailing whitespace removed, or null
if text
is null
.public static java.lang.String safeTrimLeading(java.lang.String text)
text
, without triggering
a NullPointerException
if text
is
null
.text
- Text string to be trimmed.text
with leading whitespace removed, or
null
if text
is
null
.public static java.lang.String safeTrim(java.lang.String text)
text
, without
triggering a NullPointerException
if text
is
null
.text
- Text string to be trimmed.text
with leading and trailing whitespace
removed, or null
if text
is
null
.public static java.lang.String processEscapes(java.lang.String input) throws java.lang.IllegalArgumentException, java.lang.NumberFormatException, java.lang.IndexOutOfBoundsException
Note that in javac, the unicode conversion occurs prior to the escape sequence processing, which disallows certain unicode characters from being used inside a Java string literal. This method makes no such distinction so it accepts a superset of the valid Java syntax.
input
- The string to be converted.java.lang.IllegalArgumentException
java.lang.NumberFormatException
java.lang.IndexOutOfBoundsException
public static java.lang.String stripMatchedComments(java.lang.String text)
This code assumes that there will always be a matched set of start and end comment sequences.
text
- Text from which to remove the comments.public static java.lang.String stripEnclosing(java.lang.String text, char c)
text
.text
- Text from which to remove character.c
- Character to be removed.public static java.lang.String stripEnclosing(java.lang.String text, char[] ch)
text
.text
- Text from which to remove character.ch
- List of characters to be removed.public static int countChar(java.lang.String text, char c)
text
- string to inspect.c
- Character to be counted.public static java.lang.String removeChar(java.lang.String text, char ch)
text
- The string to modify.ch
- Character to be removed.public static java.lang.String changeCase(java.lang.String name, boolean upper)
name
- Name to be capitalized/decapitalized.upper
- true
to uppercase the first character of
name
; false
to lowercase it.name
with the
requested modification. If name
is empty
or null
, the empty string is returned.public static java.lang.String generateBeanName(java.lang.String field, boolean getter, boolean bool) throws java.lang.IllegalArgumentException
boolean
and a getter is
requested, then the prefix will be "is", otherwise getters are prefixed
with "get". All setters are prefixed with "set". The name of the
field always has its first character uppercased which is slightly
different than how Sun defines the standard.field
- The name to use as the basis, must be non-null and non-empty.getter
- If true
the resulting bean name will be a
getter.bool
- If getter
is true
and this is
true
then the resulting bean name will prefixed
with "is" instead of "get". Ignored for setters.java.lang.IllegalArgumentException
- If the field name is null
or is the empty
string.public static java.lang.String render(java.lang.Object array)
Object.toString
(which does not emit any of the contents of an array).
Note that for byte[]
and char[]
the rendering
is direct to a String
rather than using the comma separated
format (for bytes a hexidecimal format is used for each byte and only
the first 1024 bytes are processed).
array
- The array to render as text.null
if the parameter is
null
.public static void render(java.lang.Object array, java.lang.StringBuilder buf)
Object.toString
(which does not emit any of the contents of an array).
Note that for byte[]
and char[]
the rendering
is direct to a String
rather than using the comma separated
format (for bytes a hexidecimal format is used for each byte and only
the first 1024 bytes are processed).
array
- The array to render as text.buf
- The output buffer. Must not be null
.public static void render(byte[] array, java.lang.StringBuilder buf)
Object.toString
(which does not emit any of the
contents of an array).array
- The array to render as text.buf
- The output buffer. Must not be null
.public static void render(short[] array, java.lang.StringBuilder buf)
Object.toString
(which does not emit any of the contents of an array).array
- The array to render as text.buf
- The output buffer. Must not be null
.public static void render(int[] array, java.lang.StringBuilder buf)
Object.toString
(which does not emit any of the contents of an array).array
- The array to render as text.buf
- The output buffer. Must not be null
.public static void render(long[] array, java.lang.StringBuilder buf)
Object.toString
(which does not emit any of the contents of an array).array
- The array to render as text.buf
- The output buffer. Must not be null
.public static void render(float[] array, java.lang.StringBuilder buf)
Object.toString
(which does not emit any of the contents of an array).array
- The array to render as text.buf
- The output buffer. Must not be null
.public static void render(double[] array, java.lang.StringBuilder buf)
Object.toString
(which does not emit any of the contents of an array).array
- The array to render as text.buf
- The output buffer. Must not be null
.public static void render(boolean[] array, java.lang.StringBuilder buf)
Object.toString
(which does not emit any of the contents of an array).array
- The array to render as text.buf
- The output buffer. Must not be null
.public static java.lang.String fixupRegex(java.lang.String text)
text
- Regular expression string to be adjusted acccording to
OS special requirements.public static java.lang.String fixupFileName(java.lang.String text)
text
- The string representing the filename with path separator.public static java.util.List<java.lang.String> parseTokens(java.lang.String str, java.lang.String delimiter, boolean addIfStartsWithDelimiter, boolean addIfEndsWithDelimiter, boolean trim)
str
- The string to be tokenized.delimiter
- Delimiter.addIfStartsWithDelimiter
- If true
and str
starts with delimiter
then empty token will be added.addIfEndsWithDelimiter
- If true
and str
ends with delimiter
then empty token will be added.trim
- If true
tokens will be trimmednull
if str equals null
otherwise list of tokens.public static boolean equal(java.lang.String s1, java.lang.String s2)
null
) stringss1
- the first strings2
- the second stringtrue
if both strings are null
or equal