edu.neu.ccs
Class Strings

java.lang.Object
  extended byedu.neu.ccs.Strings

public class Strings
extends Object

Class Strings contains a set of static methods to provide fast and human-readable encoding of an array of String data together with corresponding tools for decoding such data.

Class Strings also contains miscellaneous String utilities.

An encoded string will have one of the following forms that we will refer to as a string group:

A string group enclosed in [] uses the character ; as its string separator.

A string group enclosed in () uses the character , as its string separator.

A string group enclosed in {} uses the character | as its string separator.

The encoding methods in this class are NOT 100% robust since the decoding process will fail if any encoded string contains the separator character used for the encoding. To guarantee robust encoding, use the encoding methods in CodecUtilities.

Despite this consideration, these methods of encoding and decoding are quite valuable in the situations where what is to be encoded refers to data that should not contain the particular separator being used. It is for this reason that three forms of encoding are provided with three separators.

The method decodeStringGroup directly looks for the type of encoding performed by the encoding methods in this class. The more general method decode tries 3 algorithms in turn including CodecUtilities.decode to see if the given string has been encoded in any of 3 possible ways. It is recommended that the method decode in this class be used for general decoding of a string into a string array.

As a helpful support tool, this class contains a method tokenize that uses a StringTokenizer to break a string into a string array of tokens according to a given list of delimiters. Optionally, the trim method may be invoked to trim the tokens. There are parallel methods that make delimited strings.

As of 2.5.0, this class also contains methods that will split strings corresponding to a character delimiter and will not make adjacent delimiters be treated as a single delimiter. These methods begin with exactSplit in their name. There are also parallel methods with exactMake in their name that make delimited strings.

This class contains methods to build name-value string pairs of the form name=value and to decompose such pairs.

This class contains utility methods to convert between arrays of string and arrays of long or double.

This class contains utility methods to pad strings and to combine string pairs with a blank separator.

Finally, this class contains a simple utility to convert a string to the equivalent HTML string with ampersand, less than, greater than, and quote encoded.

Class Strings cannot be instantiated.

Since:
2.4.0
Version:
2.5.0

Field Summary
static char BRACES
          The start character for encodings {s0|s1|etc}.
static char BRACES_END
          The end character for encodings {s0|s1|etc}.
static char BRACES_SEP
          The separator character for encodings {s0|s1|etc} as a string.
static char BRACKETS
          The start character for encodings [s0;s1;etc].
static char BRACKETS_END
          The end character for encodings [s0;s1;etc].
static char BRACKETS_SEP
          The separator character for encodings [s0;s1;etc] as a string.
private static char EQUALS
          The equals character.
static char PARENS
          The start character for encodings (s0,s1,etc).
static char PARENS_END
          The end character for encodings (s0,s1,etc).
static char PARENS_SEP
          The separator character for encodings (s0,s1,etc) as a string.
 
Constructor Summary
private Strings()
          Prevent instantiation.
 
Method Summary
static String[] decode(String string)
          Attempts several algorithms to decode the given string into an array of strings and returns this array if successful and returns null if unsuccessful with all attempts.
static String[] decodeStringGroup(String string)
          Returns a String array by decoding the given string assuming it is encoded by the encoding conventions of this class.
static String[] doublesToStrings(double[] data)
          Returns an array of strings corresponding to the given array of double.
static String encode(String[] data)
          Returns the encoded String array as [s0;s1;etc] where s0, s1, etc represent the array elements.
static String encodeStringGroup(char start, String[] data)
          Returns the encoded String array as a sequence enclosed with the given start character, its matching end character, and its associated separator character.
static String encodeWithBraces(String[] data)
          Returns the encoded String array as {s0|s1|etc} where s0, s1, etc represent the array elements.
static String encodeWithBrackets(String[] data)
          Returns the encoded String array as [s0;s1;etc] where s0, s1, etc represent the array elements.
static String encodeWithParens(String[] data)
          Returns the encoded String array as (s0,s1,etc) where s0, s1, etc represent the array elements.
static String exactMakeCommaList(String[] data)
          Uses the array of strings to build a string that separates each item with a comma; automatically trims the array before the build.
static String exactMakeNewlineList(String[] data)
          Uses the array of strings to build a string that separates each item with a newline; automatically trims the array before the build.
static String exactMakeSeparatorList(String[] data, char delimiter)
          Uses the array of strings to build a string that separates each item with the given delimiter; automatically trims the array before the build.
static String exactMakeSeparatorList(String[] data, char delimiter, boolean trim)
          Uses the array of strings to build a string that separates each item with the given delimiter; if trim is true then trims the array before the build.
static String[] exactSplitCommaList(String string)
          Returns an array of strings obtained by splitting the given string at every occurence of the comma character; automatically trims the array returned.
static String[] exactSplitList(String string, char delimiter)
          Returns an array of strings obtained by splitting the given string at every occurence of the given delimiter; automatically trims the array returned.
static String[] exactSplitList(String string, char delimiter, boolean trim)
          Returns an array of strings obtained by splitting the given string at every occurence of the given delimiter; if trim is true then also trims the array returned.
static String[] exactSplitNewlineList(String string)
          Returns an array of strings obtained by splitting the given string at every occurence of the newline character; automatically trims the array returned.
static String flatten(String data)
          Returns a string with all whitespace but non-blank characters replaced by blanks.
static String[] flatten(String[] data)
          Returns a string array with all strings in the original array flattened using Strings.flatten.
static int getIndexValue(String message)
          Returns the array index that was prepended to an error message thrown by stringsToLongs or stringsToDoubles.
static String getName(String string)
          Given a string of the form name=value, return the name portion.
static String[] getNames(String[] pairs)
          Given an array of name-value pairs, returns the array of names.
static String getValue(String string)
          Given a string of the form name=value, return the value portion.
static String[] getValues(String[] pairs)
          Given an array of name-value pairs, returns the array of values.
static boolean isNonTrivialWhitespace(char c)
          Returns true if the given character is whitespace but is non-blank.
static boolean isStartEnd(String string, char start, char end)
          Returns true if the given string starts and ends with the given pair of distinct start and end characters.
static String joinWithSpace(String s, String t)
          If s and t are both non-null then return (s+blank+t).
static String[] longsToStrings(long[] data)
          Returns an array of strings corresponding to the given array of longs.
static ParseException makeAdjustedParseException(ParseException ex, String type, String[] names)
          Returns a new ParseException based on the given ParseException but adjusted to include information from the given type name and the given field names.
static String makeCommaList(String[] arrayList)
          Creates a comma separated list by trimming the data in the given String array and concatenating with commas.
static String makeNameValuePair(String name, String value)
          Given a pair of strings name and value, return the string name=value.
static String[] makeNameValuePairs(String[] names, String[] values)
          Given an array of names and an array of values, constructs and returns an array of pairs of the form: name=value.
static String makeSeparatorList(String[] arrayList, char separator)
          Creates a separated list by trimming the data in the given String array and concatenating with the given separator.
static String pad(String s)
          Returns the given string if it is non-null and has length at least 1; otherwise returns " ".
static String[] pad(String[] data)
          Returns a string array with all strings in the original array padded using Strings.pad.
static String[] padOnLeft(String[] data, int width)
          Returns a string array with all strings in the original array padded using Strings.padOnLeft.
static String padOnLeft(String s, int width)
          Return the string s padded with blanks on the left.
static String[] padOnRight(String[] data, int width)
          Returns a string array with all strings in the original array padded using Strings.padOnRight.
static String padOnRight(String s, int width)
          Return the string s padded with blanks on the right.
static String prefixRepeatChar(String string, char c, int count)
          Returns a string with count instances of the given character c prefixed to the given string.
static String removeIndexValue(String message)
          Returns the error message with its prepended index value removed.
static String[] splitCommaList(String commalist)
          Extract the non-empty strings from a comma separated list.
static String[] splitIn2(String string)
          Splits the given string into 2 parts: a prefix and a suffix and returns these 2 parts in a string array.
static String[] splitIn3(String string)
          Splits the given string into 3 parts: a prefix, a string group, and a suffix and returns these 3 parts in a string array.
static double[] stringsToDoubles(String[] data)
          Returns an array of doubles corresponding to the given array of strings.
static long[] stringsToLongs(String[] data)
          Returns an array of longs corresponding to the given array of strings.
static XComplex[] stringsToXComplexValues(String[] data)
          Returns an array of XComplex values corresponding to the given array of strings.
static String suffixRepeatChar(String string, char c, int count)
          Returns a string with count instances of the given character c suffixed to the given string.
static String textToSafeHTML(String text)
          Returns an HTML safe string built from the given string by replacing ampersand, less than, greater than, and quote with their HTML encodings; replaces tabs with spaces assuming a tab size of 4.
static String textToSafeHTML(String text, int tabsize)
          Returns an HTML safe string built from the given string by replacing ampersand, less than, greater than, and quote with their HTML encodings; replaces tabs with spaces assuming the given tab size.
static String[] tokenize(String string, String delimiters, boolean trim)
          Returns a string array contains the tokens obtained by using a StringTokenizer to split the string according to the given delimiters; if the given trim is true then the tokens are trimmed before being returned.
static String[] trim(String[] data)
          Returns a string array with all strings in the original array trimmed using String.trim.
static String[] XComplexValuesToStrings(XComplex[] data)
          Returns an array of strings corresponding to the given array of XComplex.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BRACKETS

public static final char BRACKETS
The start character for encodings [s0;s1;etc].

See Also:
Constant Field Values

PARENS

public static final char PARENS
The start character for encodings (s0,s1,etc).

See Also:
Constant Field Values

BRACES

public static final char BRACES
The start character for encodings {s0|s1|etc}.

See Also:
Constant Field Values

BRACKETS_END

public static final char BRACKETS_END
The end character for encodings [s0;s1;etc].

See Also:
Constant Field Values

PARENS_END

public static final char PARENS_END
The end character for encodings (s0,s1,etc).

See Also:
Constant Field Values

BRACES_END

public static final char BRACES_END
The end character for encodings {s0|s1|etc}.

See Also:
Constant Field Values

BRACKETS_SEP

public static final char BRACKETS_SEP
The separator character for encodings [s0;s1;etc] as a string.

See Also:
Constant Field Values

PARENS_SEP

public static final char PARENS_SEP
The separator character for encodings (s0,s1,etc) as a string.

See Also:
Constant Field Values

BRACES_SEP

public static final char BRACES_SEP
The separator character for encodings {s0|s1|etc} as a string.

See Also:
Constant Field Values

EQUALS

private static final char EQUALS
The equals character.

See Also:
Constant Field Values
Constructor Detail

Strings

private Strings()
Prevent instantiation.

Method Detail

encode

public static String encode(String[] data)

Returns the encoded String array as [s0;s1;etc] where s0, s1, etc represent the array elements.

Equivalent to encodeWithBrackets.

Parameters:
data - the data to encode
See Also:
encodeStringGroup(char, String[])

encodeWithBrackets

public static String encodeWithBrackets(String[] data)

Returns the encoded String array as [s0;s1;etc] where s0, s1, etc represent the array elements.

Equivalent to encode.

Parameters:
data - the data to encode
See Also:
encodeStringGroup(char, String[])

encodeWithParens

public static String encodeWithParens(String[] data)

Returns the encoded String array as (s0,s1,etc) where s0, s1, etc represent the array elements.

Parameters:
data - the data to encode
See Also:
encodeStringGroup(char, String[])

encodeWithBraces

public static String encodeWithBraces(String[] data)

Returns the encoded String array as {s0|s1|etc} where s0, s1, etc represent the array elements.

Parameters:
data - the data to encode
See Also:
encodeStringGroup(char, String[])

encodeStringGroup

public static String encodeStringGroup(char start,
                                       String[] data)

Returns the encoded String array as a sequence enclosed with the given start character, its matching end character, and its associated separator character.

If the given data array is null then returns null to signal failure.

Assume the array elements are s0, s1, etc. Then the elements are encoded in one of the following ways.

depending on whether the start character is [, (, or {.

If the start character is none of the three characters required then returns null to signal failure.

Each valid start character is paired with a corresponding separator character for the encoding.

As of 2.5.0, the encoding-decoding algorithms were changed to avoid the use of the Java class StringTokenizer. In fact, the portion of the output string between the start and end characters is computed by the following call:

    exactMakeSeparatorList(data,separator,false)

This call ignores null strings in the data array but makes an entry in the output string for all data strings that are non-null even for those of length 0. The parameter false means that no trimming is done on the data strings.

To be robust and decodable, the encoded strings must not contain the separator character used in the encoding.

For a fully robust but more complex encoding, see the classes in the codec package especially CodecUtilities.

Parameters:
start - the start character for the encoding which must be either [, (, or {
data - the array data to encode

decode

public static String[] decode(String string)

Attempts several algorithms to decode the given string into an array of strings and returns this array if successful and returns null if unsuccessful with all attempts.

Algorithm 1 uses CodecUtilities.decode to attempt to parse the given string as a codec-encoded string.

Algorithm 2 calls splitIn2 and then calls decodeStringGroup to parse the suffix as a string group if possible.

Algorithm 3 calls splitIn3 and then calls decodeStringGroup to parse the internal string group if this string group is non-empty.

In particular, since this decode method calls the decode method of CodecUtilities, it is more general and is recommended as the preferred method for decoding a string into an array of strings.

Parameters:
string - the string to decode

decodeStringGroup

public static String[] decodeStringGroup(String string)

Returns a String array by decoding the given string assuming it is encoded by the encoding conventions of this class.

The given string should be what we call a string group, that is, it should have one of the following forms.

The string array returned contains s0 s1 .... As of 2.5.0, even empty (zero length) strings will be included in the array returned. The method exactSplitList is used to split the string within the start and end grouping characters.

To avoid issues with leading and trailing whitespace, the given string is trimmed prior to processing.

Returns null if.

Parameters:
string - the string group to decode

isStartEnd

public static boolean isStartEnd(String string,
                                 char start,
                                 char end)

Returns true if the given string starts and ends with the given pair of distinct start and end characters.

Parameters:
string - the string to test
start - the start character
end - the end character

splitIn2

public static String[] splitIn2(String string)

Splits the given string into 2 parts: a prefix and a suffix and returns these 2 parts in a string array.

The prefix consists of the longest substring of the given string starting at the beginning that does not contain one of the following start characters: [, (, or {.

If the given string does not contain any of the start characters, then the prefix is the given string and the suffix is the empty string.

Otherwise, the prefix is the substring up to but not including the first start character and the suffix is the rest of the string.

This algorithm does not guarantee that the suffix is a string group but if it is then it is the largest possible string group contained in the original string.

Returns null if the given string is null. If the given string is non-null, then both strings in the return array will be non-null.

Parameters:
string - the string to split
Returns:
an array with the prefix and suffix

splitIn3

public static String[] splitIn3(String string)

Splits the given string into 3 parts: a prefix, a string group, and a suffix and returns these 3 parts in a string array.

The prefix consists of the longest substring of the given string starting at the beginning that does not contain one of the following start characters: [, (, or {.

If the given string does not contain any of the start characters, then the prefix is the given string and the other two parts are empty strings.

If a start character exists, then a search is made for the nearest matching end character that follows the start character. If such an end character is found then this defines the string group and the suffix is what is left. If no matching end character is found then the string group is empty and the suffix is the rest of the string including the unmatched start character.

With this specification, it is possible for the suffix to contain additional string groups that could be peeled off by repeated use of this method.

Returns null if the given string is null. If the given string is non-null, then all three strings in the return array will be non-null.

Parameters:
string - the string to split
Returns:
an array with the prefix, string group, and suffix

makeNameValuePair

public static String makeNameValuePair(String name,
                                       String value)

Given a pair of strings name and value, return the string name=value.

If either string is null, return null.

Parameters:
name - the name
value - the value

getName

public static String getName(String string)

Given a string of the form name=value, return the name portion.

Return null if the given string is null.

Return an empty string if the given string does not contain an equals sign.

Parameters:
string - the name-value string

getValue

public static String getValue(String string)

Given a string of the form name=value, return the value portion.

Return null if the given string is null.

Return the given string if the given string does not contain an equals sign.

Parameters:
string - the name-value string

makeNameValuePairs

public static String[] makeNameValuePairs(String[] names,
                                          String[] values)

Given an array of names and an array of values, constructs and returns an array of pairs of the form: name=value.

Returns null to signal failure if any of the following situations takes place.

Parameters:
names - the array of names
values - the array of values

getNames

public static String[] getNames(String[] pairs)

Given an array of name-value pairs, returns the array of names.

Returns null if the given array is null.

Returns null if any entry in the given array is null.

Parameters:
pairs - the array of name-value pairs

getValues

public static String[] getValues(String[] pairs)

Given an array of name-value pairs, returns the array of values.

Returns null if the given array is null.

Returns null if any entry in the given array is null.

Parameters:
pairs - the array of name-value pairs

longsToStrings

public static String[] longsToStrings(long[] data)

Returns an array of strings corresponding to the given array of longs.

Returns null if the given data is null.

Parameters:
data - the data to convert
Returns:
the converted data

doublesToStrings

public static String[] doublesToStrings(double[] data)

Returns an array of strings corresponding to the given array of double.

Returns null if the given data is null.

Parameters:
data - the data to convert
Returns:
the converted data

XComplexValuesToStrings

public static String[] XComplexValuesToStrings(XComplex[] data)

Returns an array of strings corresponding to the given array of XComplex.

Each XComplex is converted by the static method XComplex.toStringData.

Returns null if the given data is null.

Parameters:
data - the data to convert
Returns:
the converted data

stringsToLongs

public static long[] stringsToLongs(String[] data)
                             throws ParseException

Returns an array of longs corresponding to the given array of strings.

Returns null if the given data is null.

Trims the data strings prior to the numeric conversion.

In converting the i-th data item of the array to a long, the following conventions and procedures are followed.

Parameters:
data - the data to convert
Returns:
the converted data
Throws:
ParseException

stringsToDoubles

public static double[] stringsToDoubles(String[] data)
                                 throws ParseException

Returns an array of doubles corresponding to the given array of strings.

Returns null if the given data is null.

Trims the data strings prior to the numeric conversion.

In converting the i-th data item of the array to a double, the following conventions and procedures are followed.

Parameters:
data - the data to convert
Returns:
the converted data
Throws:
ParseException

stringsToXComplexValues

public static XComplex[] stringsToXComplexValues(String[] data)
                                          throws ParseException

Returns an array of XComplex values corresponding to the given array of strings.

Returns null if the given data is null.

Trims the data strings prior to the numeric conversion.

In converting the i-th data item of the array to an XComplex, the following conventions and procedures are followed.

Parameters:
data - the data to convert
Returns:
the converted data
Throws:
ParseException

getIndexValue

public static int getIndexValue(String message)

Returns the array index that was prepended to an error message thrown by stringsToLongs or stringsToDoubles.

This is a hack required since there is no extra data field in a ParseException to hold the array index directly.

Returns 0 if the message has no prepended value.

Parameters:
message - the error message

removeIndexValue

public static String removeIndexValue(String message)

Returns the error message with its prepended index value removed.

Assumes that the original error message was created by stringsToLongs or stringsToDoubles.

This is a hack required since there is no extra data field in a ParseException to hold the array index directly.

Returns the message if the message has no prepended index value.

Parameters:
message - the error message

makeAdjustedParseException

public static ParseException makeAdjustedParseException(ParseException ex,
                                                        String type,
                                                        String[] names)

Returns a new ParseException based on the given ParseException but adjusted to include information from the given type name and the given field names.

Assumes that the given ParseException was thrown by stringsToLongs or stringsToDoubles while parsing the given type.

The type name is used to make the user aware of the specific type whose data was being parsed.

The names array is used to replace the array index in the original message with a more understandable field name.

This is a helper method for the methods fromStringData in assorted Stringable classes.

Parameters:
ex - the original ParseException
type - the type being parsed
names - the field names that replace indices

trim

public static String[] trim(String[] data)

Returns a string array with all strings in the original array trimmed using String.trim.

null data strings are returned as is.

Returns null if the given data array is null.

Returns a newly constructed array but strings that were already trimmed in the original array will be returned as is.

Parameters:
data - the data to trim

isNonTrivialWhitespace

public static boolean isNonTrivialWhitespace(char c)

Returns true if the given character is whitespace but is non-blank.

Parameters:
c - the character to test

flatten

public static String flatten(String data)

Returns a string with all whitespace but non-blank characters replaced by blanks.

This method is called flatten because it will turn a string with line feeds and carriage returns into one that may be viewed on a single line.

Returns the given string if it does not contain a character that is whitespace but non-blank.

Parameters:
data - the string to flatten

flatten

public static String[] flatten(String[] data)

Returns a string array with all strings in the original array flattened using Strings.flatten.

null data strings are returned as is.

Returns null if the given data array is null.

Returns a newly constructed array but strings that were already flattened in the original array will be returned as is.

Parameters:
data - the data to flatten

tokenize

public static String[] tokenize(String string,
                                String delimiters,
                                boolean trim)

Returns a string array contains the tokens obtained by using a StringTokenizer to split the string according to the given delimiters; if the given trim is true then the tokens are trimmed before being returned.

If the given string is null, returns null.

If the given delimiters string is null or of length 0, return a string array with the given string as its only element (possibly trimmed).

Parameters:
string - the string to tokenize
delimiters - the delimiter characters
trim - whether or not to trim the tokens

splitCommaList

public static String[] splitCommaList(String commalist)

Extract the non-empty strings from a comma separated list.

Return a zero length array if the list is null or has length 0 after being trimmed.

This is a frequently used special case of the method tokenize and so is explicitly given its own name.

Parameters:
commalist - a comma separated list of strings

makeSeparatorList

public static String makeSeparatorList(String[] arrayList,
                                       char separator)

Creates a separated list by trimming the data in the given String array and concatenating with the given separator.

The strings that are null or that trim to length 0 strings will be omitted from the separator list.

Parameters:
arrayList - the array of strings to concatenate
separator - the separator character

makeCommaList

public static String makeCommaList(String[] arrayList)

Creates a comma separated list by trimming the data in the given String array and concatenating with commas.

The strings that are null or that trim to length 0 strings will be omitted from the separator list.

This is a frequently used special case of the method makeSeparatorList and so is explicitly given its own name.

Parameters:
arrayList - the array of strings to concatenate

exactSplitList

public static String[] exactSplitList(String string,
                                      char delimiter,
                                      boolean trim)

Returns an array of strings obtained by splitting the given string at every occurence of the given delimiter; if trim is true then also trims the array returned.

If the delimiter occurs N times in the given string then the array returned will have size (N+1).

If the delimiter occurs in adjacent positions in the string then the corresponding split will have length 0.

This method avoids StringTokenizer since that class groups adjacent delimiters.

If the given string is null, returns null.

Parameters:
string - the string to split
delimiter - the delimiter that defines the split
trim - whether or not to trim the array returned

exactSplitList

public static String[] exactSplitList(String string,
                                      char delimiter)

Returns an array of strings obtained by splitting the given string at every occurence of the given delimiter; automatically trims the array returned.

If the delimiter occurs N times in the given string then the array returned will have size (N+1).

If the delimiter occurs in adjacent positions in the string then the corresponding split will have length 0.

This method avoids StringTokenizer since that class groups adjacent delimiters.

If the given string is null, returns null.

Parameters:
string - the string to split
delimiter - the delimiter that defines the split

exactSplitCommaList

public static String[] exactSplitCommaList(String string)

Returns an array of strings obtained by splitting the given string at every occurence of the comma character; automatically trims the array returned.

If comma occurs N times in the given string then the array returned will have size (N+1).

If comma occurs in adjacent positions in the string then the corresponding split will have length 0.

This method avoids StringTokenizer since that class groups adjacent delimiters.

If the given string is null, returns null.

Parameters:
string - the string to split

exactSplitNewlineList

public static String[] exactSplitNewlineList(String string)

Returns an array of strings obtained by splitting the given string at every occurence of the newline character; automatically trims the array returned.

If newline occurs N times in the given string then the array returned will have size (N+1).

If newline occurs in adjacent positions in the string then the corresponding split will have length 0.

This method avoids StringTokenizer since that class groups adjacent delimiters.

If the given string is null, returns null.

Parameters:
string - the string to split

exactMakeSeparatorList

public static String exactMakeSeparatorList(String[] data,
                                            char delimiter,
                                            boolean trim)

Uses the array of strings to build a string that separates each item with the given delimiter; if trim is true then trims the array before the build.

If the given data is null, returns null.

If the given data is non-null, then the string returned is non-null.

If an item in the data array is null then that item is skipped.

If M is the number of non-null items in the data array then the delimiter will be placed (M-1) times into the returned string to separate the non-null items.

This method does not check whether or not the delimiter occurs within any item in the data array.

Parameters:
data - the string array to build into a string
delimiter - the delimiter to use as a separator
trim - whether or not to trim before the build

exactMakeSeparatorList

public static String exactMakeSeparatorList(String[] data,
                                            char delimiter)

Uses the array of strings to build a string that separates each item with the given delimiter; automatically trims the array before the build.

If the given data is null, returns null.

If the given data is non-null, then the string returned is non-null.

If an item in the data array is null then that item is skipped.

If M is the number of non-null items in the data array then the delimiter will be placed (M-1) times into the returned string to separate the non-null items.

This method does not check whether or not the delimiter occurs within any item in the data array.

Parameters:
data - the string array to build into a string
delimiter - the delimiter to use as a separator

exactMakeCommaList

public static String exactMakeCommaList(String[] data)

Uses the array of strings to build a string that separates each item with a comma; automatically trims the array before the build.

If the given data is null, returns null.

If the given data is non-null, then the string returned is non-null.

If an item in the data array is null then that item is skipped.

If M is the number of non-null items in the data array then the comma will be placed (M-1) times into the returned string to separate the non-null items.

This method does not check whether or not a comma occurs within any item in the data array.

Parameters:
data - the string array to build into a string

exactMakeNewlineList

public static String exactMakeNewlineList(String[] data)

Uses the array of strings to build a string that separates each item with a newline; automatically trims the array before the build.

If the given data is null, returns null.

If the given data is non-null, then the string returned is non-null.

If an item in the data array is null then that item is skipped.

If M is the number of non-null items in the data array then the newline will be placed (M-1) times into the returned string to separate the non-null items.

This method does not check whether or not a newline occurs within any item in the data array.

Parameters:
data - the string array to build into a string

pad

public static String pad(String s)

Returns the given string if it is non-null and has length at least 1; otherwise returns " ".

Parameters:
s - the string to pad to length at least 1

pad

public static String[] pad(String[] data)

Returns a string array with all strings in the original array padded using Strings.pad.

Returns null if the given data array is null.

Parameters:
data - the data to pad

prefixRepeatChar

public static String prefixRepeatChar(String string,
                                      char c,
                                      int count)

Returns a string with count instances of the given character c prefixed to the given string.

If string is null, it is set to the empty string.

If count is negative it is set to 0.

Parameters:
string - the string to prefix
c - the character to repeat
count - the number of repeats

suffixRepeatChar

public static String suffixRepeatChar(String string,
                                      char c,
                                      int count)

Returns a string with count instances of the given character c suffixed to the given string.

If string is null, it is set to the empty string.

If count is negative it is set to 0.

Parameters:
string - the string to suffix
c - the character to repeat
count - the number of repeats

padOnLeft

public static String padOnLeft(String s,
                               int width)

Return the string s padded with blanks on the left.

First, if s is null, replace s with an empty string.

Then, if s has length greater than or equal to width, return s.

Otherwise, return a string constructed with s on the right and (width-s.length()) blanks on the left.

Parameters:
s - the string to pad
width - the minimum width of the return string

padOnLeft

public static String[] padOnLeft(String[] data,
                                 int width)

Returns a string array with all strings in the original array padded using Strings.padOnLeft.

Returns null if the given data array is null.

Parameters:
data - the data to pad
width - the minimum width of each return string

padOnRight

public static String padOnRight(String s,
                                int width)

Return the string s padded with blanks on the right.

First, if s is null, replace s with an empty string.

Then, if s has length greater than or equal to width, return s.

Otherwise, return a string constructed with s on the left and (width-s.length()) blanks on the right.

Parameters:
s - the string to pad
width - the minimum width of the return string

padOnRight

public static String[] padOnRight(String[] data,
                                  int width)

Returns a string array with all strings in the original array padded using Strings.padOnRight.

Returns null if the given data array is null.

Parameters:
data - the data to pad
width - the minimum width of each return string

joinWithSpace

public static String joinWithSpace(String s,
                                   String t)

If s and t are both non-null then return (s+blank+t).

Otherwise, if one string is null, return the other.

Parameters:
s - string 1 to join
t - string 2 to join

textToSafeHTML

public static String textToSafeHTML(String text)

Returns an HTML safe string built from the given string by replacing ampersand, less than, greater than, and quote with their HTML encodings; replaces tabs with spaces assuming a tab size of 4.

Parameters:
text - the string to convert to HTML

textToSafeHTML

public static String textToSafeHTML(String text,
                                    int tabsize)

Returns an HTML safe string built from the given string by replacing ampersand, less than, greater than, and quote with their HTML encodings; replaces tabs with spaces assuming the given tab size.

Parameters:
text - the string to convert to HTML
tabsize - the tabsize for conversion of tabs to spaces