Top Description Fields Constructors Methods
uk.ac.ed.ph.snuggletex.internal.util

public final Class StringUtilities

extends Object
Class Inheritance
Imports
java.util.Iterator

Some vaguely useful String-related methods that are used from time to time.

(This is a cut-down version of the same class in ph-commons-util.)

Author
David McKain
Version
$Revision$

Field Summary

Modifier and TypeField and Description
public static final String[]
EMPTY_STRING_ARRAY

Shared instance of an empty array of Strings, which is sometimes useful!

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static String

Returns:

same string if it is non-empty, otherwise null.
emptyIfNull
(final String string)

Convenience method that turns a String to an empty String ("") if it is null.

public static boolean
fromBinaryValues(final String value, final String
value returning true
trueValue
,
final String
value returning false
falseValue
)

Converts the given String argument to a boolean using the scheme trueValue => true, falseValue => false.

public static boolean

Returns:

true if "yes", false if "no"
fromTrueFalse
(final String value)

Converts the given String argument to a boolean using the scheme "true"=>true, "false"=>false.

public static boolean

Returns:

true if "yes", false if "no"
fromYesNo
(final String value)

Converts the given String argument to a boolean using the scheme "yes"=>true, "no"=>false.

public static boolean
isNullOrEmpty(final String string)

Tests whether the given String is null or empty ("").

public static String

Returns:

objects joined using the given separator.
join
(final Iterable<? extends Object>
collection of Objects to join
objects
,
final CharSequence
separator to use
separator
)

Joins the given collection of Objects using the given separator and each Object's normal toString() method.

public static String
join(final Object[]
array of Objects to join
objects
,
final CharSequence
separator to use
separator
)

Same as join(Iterable, CharSequence) but simply takes an array of Objects.

public static void
join(final StringBuilder
StringBuilder to append results to
resultBuilder
,
final Object[]
array of Objects to join
objects
,
final CharSequence
separator to use
separator
)

Version of join(Object[], CharSequence) that allows you to pass in a StringBuilder that the result will be built up in. This is useful if you need to do add in other stuff later on.

public static String
join(final Object[]
array of Objects to join
objects
,
final CharSequence
separator to use
separator
,
final int
first index to join
startIndex
,
final int
index after last one to join
endIndex
)

Version of join(Object[], CharSequence) that allows you to specify a range of indices in the array to join.

public static void
join(final StringBuilder
StringBuilder to append results to
resultBuilder
,
final Object[]
array of Objects to join
objects
,
final CharSequence
separator to use
separator
,
final int
first index to join
startIndex
,
final int
index after last one to join
endIndex
)

Version of join(Object[], CharSequence, int, int) that allows you to pass in a StringBuilder that the result will be built up in. This is useful if you need to do add in other stuff later on.

public static String

Returns:

same string if it is non-null and non-empty, otherwise null.
nullIfEmpty
(final String string)

Convenience method that turns a String to null if it is empty (i.e. "") or null.

public static String

Returns:

"true" if true, "false" if false.
toTrueFalse
(final boolean
boolean to convert
state
)

Trivial helper method to convert a boolean into either "true" or "false" depending on its state.

public static String

Returns:

"yes" if true, "no" if false.
toYesNo
(final boolean
boolean to convert
state
)

Trivial helper method to convert a boolean into either "yes" or "no" depending on its state.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

EMPTY_STRING_ARRAYback to summary
public static final String[] EMPTY_STRING_ARRAY

Shared instance of an empty array of Strings, which is sometimes useful!

Constructor Detail

StringUtilitiesback to summary
public StringUtilities()

Method Detail

emptyIfNullback to summary
public static String emptyIfNull(final String string)

Convenience method that turns a String to an empty String ("") if it is null.

Returns:String

same string if it is non-empty, otherwise null.

fromBinaryValuesback to summary
public static boolean fromBinaryValues(final String value, final String trueValue, final String falseValue)

Converts the given String argument to a boolean using the scheme trueValue => true, falseValue => false. Any other value results in an IllegalArgumentException.

Parameters
trueValue:String

value returning true

falseValue:String

value returning false

Exceptions
IllegalArgumentException:
if value if null or neither trueValue nor falseValue
fromTrueFalseback to summary
public static boolean fromTrueFalse(final String value)

Converts the given String argument to a boolean using the scheme "true"=>true, "false"=>false. Any other value results in an IllegalArgumentException.

Returns:boolean

true if "yes", false if "no"

Exceptions
IllegalArgumentException:
if value if null or neither "yes" or "no"
fromYesNoback to summary
public static boolean fromYesNo(final String value)

Converts the given String argument to a boolean using the scheme "yes"=>true, "no"=>false. Any other value results in an IllegalArgumentException.

Returns:boolean

true if "yes", false if "no"

Exceptions
IllegalArgumentException:
if value if null or neither "yes" or "no"
isNullOrEmptyback to summary
public static boolean isNullOrEmpty(final String string)

Tests whether the given String is null or empty ("").

joinback to summary
public static String join(final Iterable<? extends Object> objects, final CharSequence separator)

Joins the given collection of Objects using the given separator and each Object's normal toString() method.

For example, joining the collection "a", "b", "c" with "/" gives "a/b/c".

Parameters
objects:Iterable<? extends Object>

collection of Objects to join

separator:CharSequence

separator to use

Returns:String

objects joined using the given separator.

joinback to summary
public static String join(final Object[] objects, final CharSequence separator)

Same as join(Iterable, CharSequence) but simply takes an array of Objects.

Parameters
objects:Object[]

array of Objects to join

separator:CharSequence

separator to use

joinback to summary
public static void join(final StringBuilder resultBuilder, final Object[] objects, final CharSequence separator)

Version of join(Object[], CharSequence) that allows you to pass in a StringBuilder that the result will be built up in. This is useful if you need to do add in other stuff later on.

Parameters
resultBuilder:StringBuilder

StringBuilder to append results to

objects:Object[]

array of Objects to join

separator:CharSequence

separator to use

joinback to summary
public static String join(final Object[] objects, final CharSequence separator, final int startIndex, final int endIndex)

Version of join(Object[], CharSequence) that allows you to specify a range of indices in the array to join. This can be useful in some cases.

Parameters
objects:Object[]

array of Objects to join

separator:CharSequence

separator to use

startIndex:int

first index to join

endIndex:int

index after last one to join

joinback to summary
public static void join(final StringBuilder resultBuilder, final Object[] objects, final CharSequence separator, final int startIndex, final int endIndex)

Version of join(Object[], CharSequence, int, int) that allows you to pass in a StringBuilder that the result will be built up in. This is useful if you need to do add in other stuff later on.

Parameters
resultBuilder:StringBuilder

StringBuilder to append results to

objects:Object[]

array of Objects to join

separator:CharSequence

separator to use

startIndex:int

first index to join

endIndex:int

index after last one to join

nullIfEmptyback to summary
public static String nullIfEmpty(final String string)

Convenience method that turns a String to null if it is empty (i.e. "") or null.

Returns:String

same string if it is non-null and non-empty, otherwise null.

toTrueFalseback to summary
public static String toTrueFalse(final boolean state)

Trivial helper method to convert a boolean into either "true" or "false" depending on its state.

Parameters
state:boolean

boolean to convert

Returns:String

"true" if true, "false" if false.

toYesNoback to summary
public static String toYesNo(final boolean state)

Trivial helper method to convert a boolean into either "yes" or "no" depending on its state.

Parameters
state:boolean

boolean to convert

Returns:String

"yes" if true, "no" if false.