org.codehaus.plexus.util

Class StringUtils

public class StringUtils extends Object

Common String manipulation routines.

Originally from Turbine and the GenerationJavaCore library.

Since: 1.0

Version: $Id: StringUtils.java 5958 2007-02-28 10:29:55Z olamy $

Author: Jon S. Stevens Daniel Rall Greg Coladonato Henri Yandell Ed Korthof Rand McNeely Stephen Colebourne Fredrik Westermarck Holger Krauth Alexander Day Chaffee

Constructor Summary
StringUtils()

StringUtils instances should NOT be constructed in standard programming.

Method Summary
static Stringabbreviate(String s, int maxWidth)
Turn "Now is the time for all good men" into "Now is the time for..."

Specifically:

If str is less than max characters long, return it.

static Stringabbreviate(String s, int offset, int maxWidth)
Turn "Now is the time for all good men" into "...is the time for..."

Works like abbreviate(String, int), but allows you to specify a "left edge" offset.

static StringaddAndDeHump(String view)
static Stringcapitalise(String str)

Capitalise a String.

That is, convert the first character into title-case.

static StringcapitaliseAllWords(String str)

Capitalise all the words in a String.

Uses Character#isWhitespace(char) as a separator between words.

null will return null.

static StringcapitalizeFirstLetter(String data)
static Stringcenter(String str, int size)

Center a String in a larger String of size n.

Uses spaces as the value to buffer the String with.

static Stringcenter(String str, int size, String delim)

Center a String in a larger String of size n.

Uses a supplied String as the value to buffer the String with.

static Stringchomp(String str)

Remove the last newline, and everything after it from a String.

static Stringchomp(String str, String sep)

Remove the last value of a supplied String, and everything after it from a String.

static StringchompLast(String str)

Remove a newline if and only if it is at the end of the supplied String.

static StringchompLast(String str, String sep)

Remove a value if and only if the String ends with that value.

static Stringchop(String str)

Remove the last character from a String.

If the String ends in \r\n, then remove both of them.

static StringchopNewline(String str)

Remove \n from end of a String if it's there.

static Stringclean(String str)

Removes control characters, including whitespace, from both ends of this String, handling null by returning an empty String.

static Stringconcatenate(Object[] array)

Concatenates elements of an array into a single String.

The difference from join is that concatenate has no delimiter.

static intcountMatches(String str, String sub)

How many times is the substring in the larger String.

null returns 0.

static StringdefaultString(Object obj)

Returns either the passed in Object as a String, or, if the Object is null, an empty String.

static StringdefaultString(Object obj, String defaultString)

Returns either the passed in Object as a String, or, if the Object is null, a passed in default String.

static StringdeleteWhitespace(String str)

Deletes all whitespaces from a String.

Whitespace is defined by Character#isWhitespace(char).

static Stringdifference(String s1, String s2)
Compare two strings, and return the portion where they differ.
static intdifferenceAt(String s1, String s2)
Compare two strings, and return the index at which the strings begin to differ.
static booleanequals(String str1, String str2)

Compares two Strings, returning true if they are equal.

nulls are handled without exceptions.

static booleanequalsIgnoreCase(String str1, String str2)

Compares two Strings, returning true if they are equal ignoring the case.

Nulls are handled without exceptions.

static Stringescape(String str)

Escapes any values it finds into their String form.

So a tab becomes the characters '\\' and 't'.

static StringgetChomp(String str, String sep)

Remove everything and return the last value of a supplied String, and everything after it from a String.

static StringgetNestedString(String str, String tag)

Get the String that is nested in between two instances of the same String.

If str is null, will return null.

static StringgetNestedString(String str, String open, String close)

Get the String that is nested in between two Strings.

static StringgetPrechomp(String str, String sep)

Remove and return everything before the first value of a supplied String from another String.

static intindexOfAny(String str, String[] searchStrs)

Find the first index of any of a set of potential substrings.

null String will return -1.

static Stringinterpolate(String text, Map namespace)
static booleanisAlpha(String str)

Checks if the String contains only unicode letters.

null will return false.

static booleanisAlphanumeric(String str)

Checks if the String contains only unicode letters or digits.

null will return false.

static booleanisAlphanumericSpace(String str)

Checks if the String contains only unicode letters, digits or space (' ').

null will return false.

static booleanisAlphaSpace(String str)

Checks if the String contains only unicode letters and space (' ').

null will return false.

static booleanisEmpty(String str)

Checks if a (trimmed) String is null or empty.

static booleanisNotEmpty(String str)

Checks if a String is non null and is not empty (length > 0).

static booleanisNumeric(String str)

Checks if the String contains only unicode digits.

null will return false.

static booleanisNumericSpace(String str)

Checks if the String contains only unicode digits or space (' ').

null will return false.

static booleanisWhitespace(String str)

Checks if the String contains only whitespace.

null will return false.

static Stringjoin(Object[] array, String separator)

Joins the elements of the provided array into a single String containing the provided list of elements.

No delimiter is added before or after the list.

static Stringjoin(Iterator iterator, String separator)

Joins the elements of the provided Iterator into a single String containing the provided elements.

No delimiter is added before or after the list.

static intlastIndexOfAny(String str, String[] searchStrs)

Find the latest index of any of a set of potential substrings.

null string will return -1.

static Stringleft(String str, int len)

Gets the leftmost n characters of a String.

If n characters are not available, or the String is null, the String will be returned without an exception.

static StringleftPad(String str, int size)

Left pad a String with spaces.

The String is padded to the size of n.

static StringleftPad(String str, int size, String delim)
Left pad a String with a specified string.
static StringlowercaseFirstLetter(String data)
static StringlowerCase(String str)

Convert a String to lower case, null String returns null.

static Stringmid(String str, int pos, int len)

Gets n characters from the middle of a String.

If n characters are not available, the remainder of the String will be returned without an exception.

static StringoverlayString(String text, String overlay, int start, int end)

Overlay a part of a String with another String.

static Stringprechomp(String str, String sep)

Remove the first value of a supplied String, and everything before it from a String.

static StringremoveAndHump(String data, String replaceThis)
static Stringrepeat(String str, int repeat)

Repeat a String n times to form a new string.

static Stringreplace(String text, char repl, char with)

Replace all occurances of a char within another char.

A null reference passed to this method is a no-op.

static Stringreplace(String text, char repl, char with, int max)

Replace a char with another char inside a larger String, for the first max values of the search char.

A null reference passed to this method is a no-op.

static Stringreplace(String text, String repl, String with)

Replace all occurances of a String within another String.

A null reference passed to this method is a no-op.

static Stringreplace(String text, String repl, String with, int max)

Replace a String with another String inside a larger String, for the first max values of the search String.

A null reference passed to this method is a no-op.

static StringreplaceOnce(String text, char repl, char with)

Replace a char with another char inside a larger String, once.

A null reference passed to this method is a no-op.

static StringreplaceOnce(String text, String repl, String with)

Replace a String with another String inside a larger String, once.

A null reference passed to this method is a no-op.

static Stringreverse(String str)

Reverse a String.

null String returns null.

static voidreverseArray(Object[] array)

Reverses an array.

TAKEN FROM CollectionsUtils.

static StringreverseDelimitedString(String str, String delimiter)

Reverses a String that is delimited by a specific character.

The Strings between the delimiters are not reversed.

static Stringright(String str, int len)

Gets the rightmost n characters of a String.

If n characters are not available, or the String is null, the String will be returned without an exception.

static StringrightPad(String str, int size)

Right pad a String with spaces.

The String is padded to the size of n.

static StringrightPad(String str, int size, String delim)

Right pad a String with a specified string.

The String is padded to the size of n.

static String[]split(String str)

Splits the provided text into a array, using whitespace as the separator.

The separator is not included in the returned String array.

static String[]split(String text, String separator)
static String[]split(String str, String separator, int max)

Splits the provided text into a array, based on a given separator.

The separator is not included in the returned String array.

static Stringstrip(String str)

Remove whitespace from the front and back of a String.

static Stringstrip(String str, String delim)

Remove a specified String from the front and back of a String.

If whitespace is wanted to be removed, used the StringUtils method.

static String[]stripAll(String[] strs)

Strip whitespace from the front and back of every String in the array.

static String[]stripAll(String[] strs, String delimiter)

Strip the specified delimiter from the front and back of every String in the array.

static StringstripEnd(String str, String strip)

Strip any of a supplied String from the end of a String.

If the strip String is null, whitespace is stripped.

static StringstripStart(String str, String strip)

Strip any of a supplied String from the start of a String.

If the strip String is null, whitespace is stripped.

static Stringsubstring(String str, int start)

Gets a substring from the specified string avoiding exceptions.

A negative start position can be used to start n characters from the end of the String.

static Stringsubstring(String str, int start, int end)

Gets a substring from the specified String avoiding exceptions.

A negative start position can be used to start/end n characters from the end of the String.

static StringswapCase(String str)

Swaps the case of String.

Properly looks after making sure the start of words are Titlecase and not Uppercase.

null is returned as null.

static Stringtrim(String str)

Removes control characters, including whitespace, from both ends of this String, handling null by returning null.

static Stringuncapitalise(String str)

Uncapitalise a String.

That is, convert the first character into lower-case.

static StringuncapitaliseAllWords(String str)

Uncapitalise all the words in a string.

Uses Character#isWhitespace(char) as a separator between words.

null will return null.

static StringupperCase(String str)

Convert a String to upper case, null String returns null.

Constructor Detail

StringUtils

public StringUtils()

StringUtils instances should NOT be constructed in standard programming. Instead, the class should be used as StringUtils.trim(" foo ");.

This constructor is public to permit tools that require a JavaBean manager to operate.

Method Detail

abbreviate

public static String abbreviate(String s, int maxWidth)
Turn "Now is the time for all good men" into "Now is the time for..."

Specifically:

If str is less than max characters long, return it. Else abbreviate it to (substring(str, 0, max-3) + "..."). If maxWidth is less than 3, throw an IllegalArgumentException. In no case will it return a string of length greater than maxWidth.

Parameters: maxWidth maximum length of result string

abbreviate

public static String abbreviate(String s, int offset, int maxWidth)
Turn "Now is the time for all good men" into "...is the time for..."

Works like abbreviate(String, int), but allows you to specify a "left edge" offset. Note that this left edge is not necessarily going to be the leftmost character in the result, or the first character following the ellipses, but it will appear somewhere in the result. In no case will it return a string of length greater than maxWidth.

Parameters: offset left edge of source string maxWidth maximum length of result string

addAndDeHump

public static String addAndDeHump(String view)

capitalise

public static String capitalise(String str)

Capitalise a String.

That is, convert the first character into title-case. null is returned as null.

Parameters: str the String to capitalise

Returns: capitalised String

capitaliseAllWords

public static String capitaliseAllWords(String str)

Capitalise all the words in a String.

Uses Character#isWhitespace(char) as a separator between words.

null will return null.

Parameters: str the String to capitalise

Returns: capitalised String

capitalizeFirstLetter

public static String capitalizeFirstLetter(String data)

center

public static String center(String str, int size)

Center a String in a larger String of size n.

Uses spaces as the value to buffer the String with. Equivalent to center(str, size, " ").

Parameters: str String to center size int size of new String

Returns: String containing centered String

Throws: NullPointerException if str is null

center

public static String center(String str, int size, String delim)

Center a String in a larger String of size n.

Uses a supplied String as the value to buffer the String with.

Parameters: str String to center size int size of new String delim String to buffer the new String with

Returns: String containing centered String

Throws: NullPointerException if str or delim is null ArithmeticException if delim is the empty String

chomp

public static String chomp(String str)

Remove the last newline, and everything after it from a String.

Parameters: str String to chomp the newline from

Returns: String without chomped newline

Throws: NullPointerException if str is null

chomp

public static String chomp(String str, String sep)

Remove the last value of a supplied String, and everything after it from a String.

Parameters: str String to chomp from sep String to chomp

Returns: String without chomped ending

Throws: NullPointerException if str or sep is null

chompLast

public static String chompLast(String str)

Remove a newline if and only if it is at the end of the supplied String.

Parameters: str String to chomp from

Returns: String without chomped ending

Throws: NullPointerException if str is null

chompLast

public static String chompLast(String str, String sep)

Remove a value if and only if the String ends with that value.

Parameters: str String to chomp from sep String to chomp

Returns: String without chomped ending

Throws: NullPointerException if str or sep is null

chop

public static String chop(String str)

Remove the last character from a String.

If the String ends in \r\n, then remove both of them.

Parameters: str String to chop last character from

Returns: String without last character

Throws: NullPointerException if str is null

chopNewline

public static String chopNewline(String str)

Remove \n from end of a String if it's there. If a \r precedes it, then remove that too.

Parameters: str String to chop a newline from

Returns: String without newline

Throws: NullPointerException if str is null

clean

public static String clean(String str)

Removes control characters, including whitespace, from both ends of this String, handling null by returning an empty String.

Parameters: str the String to check

Returns: the trimmed text (never null)

See Also: java.lang.String#trim()

concatenate

public static String concatenate(Object[] array)

Concatenates elements of an array into a single String.

The difference from join is that concatenate has no delimiter.

Parameters: array the array of values to concatenate.

Returns: the concatenated string.

countMatches

public static int countMatches(String str, String sub)

How many times is the substring in the larger String.

null returns 0.

Parameters: str the String to check sub the substring to count

Returns: the number of occurances, 0 if the String is null

Throws: NullPointerException if sub is null

defaultString

public static String defaultString(Object obj)

Returns either the passed in Object as a String, or, if the Object is null, an empty String.

Parameters: obj the Object to check

Returns: the passed in Object's toString, or blank if it was null

defaultString

public static String defaultString(Object obj, String defaultString)

Returns either the passed in Object as a String, or, if the Object is null, a passed in default String.

Parameters: obj the Object to check defaultString the default String to return if str is null

Returns: the passed in string, or the default if it was null

deleteWhitespace

public static String deleteWhitespace(String str)

Deletes all whitespaces from a String.

Whitespace is defined by Character#isWhitespace(char).

Parameters: str String target to delete whitespace from

Returns: the String without whitespaces

Throws: NullPointerException

difference

public static String difference(String s1, String s2)
Compare two strings, and return the portion where they differ. (More precisely, return the remainder of the second string, starting from where it's different from the first.)

E.g. strdiff("i am a machine", "i am a robot") -> "robot"

Returns: the portion of s2 where it differs from s1; returns the empty string ("") if they are equal

differenceAt

public static int differenceAt(String s1, String s2)
Compare two strings, and return the index at which the strings begin to differ.

E.g. strdiff("i am a machine", "i am a robot") -> 7

Returns: the index where s2 and s1 begin to differ; -1 if they are equal

equals

public static boolean equals(String str1, String str2)

Compares two Strings, returning true if they are equal.

nulls are handled without exceptions. Two null references are considered to be equal. The comparison is case sensitive.

Parameters: str1 the first string str2 the second string

Returns: true if the Strings are equal, case sensitive, or both null

See Also: java.lang.String#equals(Object)

equalsIgnoreCase

public static boolean equalsIgnoreCase(String str1, String str2)

Compares two Strings, returning true if they are equal ignoring the case.

Nulls are handled without exceptions. Two null references are considered equal. Comparison is case insensitive.

Parameters: str1 the first string str2 the second string

Returns: true if the Strings are equal, case insensitive, or both null

See Also: java.lang.String#equalsIgnoreCase(String)

escape

public static String escape(String str)

Escapes any values it finds into their String form.

So a tab becomes the characters '\\' and 't'.

Parameters: str String to escape values in

Returns: String with escaped values

Throws: NullPointerException if str is null

getChomp

public static String getChomp(String str, String sep)

Remove everything and return the last value of a supplied String, and everything after it from a String.

Parameters: str String to chomp from sep String to chomp

Returns: String chomped

Throws: NullPointerException if str or sep is null

getNestedString

public static String getNestedString(String str, String tag)

Get the String that is nested in between two instances of the same String.

If str is null, will return null.

Parameters: str the String containing nested-string tag the String before and after nested-string

Returns: the String that was nested, or null

Throws: NullPointerException if tag is null

getNestedString

public static String getNestedString(String str, String open, String close)

Get the String that is nested in between two Strings.

Parameters: str the String containing nested-string open the String before nested-string close the String after nested-string

Returns: the String that was nested, or null

Throws: NullPointerException if open or close is null

getPrechomp

public static String getPrechomp(String str, String sep)

Remove and return everything before the first value of a supplied String from another String.

Parameters: str String to chomp from sep String to chomp

Returns: String prechomped

Throws: NullPointerException if str or sep is null

indexOfAny

public static int indexOfAny(String str, String[] searchStrs)

Find the first index of any of a set of potential substrings.

null String will return -1.

Parameters: str the String to check searchStrs the Strings to search for

Returns: the first index of any of the searchStrs in str

Throws: NullPointerException if any of searchStrs[i] is null

interpolate

public static String interpolate(String text, Map namespace)

isAlpha

public static boolean isAlpha(String str)

Checks if the String contains only unicode letters.

null will return false. An empty String will return true.

Parameters: str the String to check

Returns: true if only contains letters, and is non-null

isAlphanumeric

public static boolean isAlphanumeric(String str)

Checks if the String contains only unicode letters or digits.

null will return false. An empty String will return true.

Parameters: str the String to check

Returns: true if only contains letters or digits, and is non-null

isAlphanumericSpace

public static boolean isAlphanumericSpace(String str)

Checks if the String contains only unicode letters, digits or space (' ').

null will return false. An empty String will return true.

Parameters: str the String to check

Returns: true if only contains letters, digits or space, and is non-null

isAlphaSpace

public static boolean isAlphaSpace(String str)

Checks if the String contains only unicode letters and space (' ').

null will return false. An empty String will return true.

Parameters: str the String to check

Returns: true if only contains letters and space, and is non-null

isEmpty

public static boolean isEmpty(String str)

Checks if a (trimmed) String is null or empty.

Parameters: str the String to check

Returns: true if the String is null, or length zero once trimmed

isNotEmpty

public static boolean isNotEmpty(String str)

Checks if a String is non null and is not empty (length > 0).

Parameters: str the String to check

Returns: true if the String is non-null, and not length zero

isNumeric

public static boolean isNumeric(String str)

Checks if the String contains only unicode digits.

null will return false. An empty String will return true.

Parameters: str the String to check

Returns: true if only contains digits, and is non-null

isNumericSpace

public static boolean isNumericSpace(String str)

Checks if the String contains only unicode digits or space (' ').

null will return false. An empty String will return true.

Parameters: str the String to check

Returns: true if only contains digits or space, and is non-null

isWhitespace

public static boolean isWhitespace(String str)

Checks if the String contains only whitespace.

null will return false. An empty String will return true.

Parameters: str the String to check

Returns: true if only contains whitespace, and is non-null

join

public static String join(Object[] array, String separator)

Joins the elements of the provided array into a single String containing the provided list of elements.

No delimiter is added before or after the list. A null separator is the same as a blank String.

Parameters: array the array of values to join together separator the separator character to use

Returns: the joined String

join

public static String join(Iterator iterator, String separator)

Joins the elements of the provided Iterator into a single String containing the provided elements.

No delimiter is added before or after the list. A null separator is the same as a blank String.

Parameters: iterator the Iterator of values to join together separator the separator character to use

Returns: the joined String

lastIndexOfAny

public static int lastIndexOfAny(String str, String[] searchStrs)

Find the latest index of any of a set of potential substrings.

null string will return -1.

Parameters: str the String to check searchStrs the Strings to search for

Returns: the last index of any of the Strings

Throws: NullPointerException if any of searchStrs[i] is null

left

public static String left(String str, int len)

Gets the leftmost n characters of a String.

If n characters are not available, or the String is null, the String will be returned without an exception.

Parameters: str the String to get the leftmost characters from len the length of the required String

Returns: the leftmost characters

Throws: IllegalArgumentException if len is less than zero

leftPad

public static String leftPad(String str, int size)

Left pad a String with spaces.

The String is padded to the size of n.

Parameters: str String to pad out size size to pad to

Returns: left padded String

Throws: NullPointerException if str or delim is null

leftPad

public static String leftPad(String str, int size, String delim)
Left pad a String with a specified string. Pad to a size of n.

Parameters: str String to pad out size size to pad to delim String to pad with

Returns: left padded String

Throws: NullPointerException if str or delim is null ArithmeticException if delim is the empty string

lowercaseFirstLetter

public static String lowercaseFirstLetter(String data)

lowerCase

public static String lowerCase(String str)

Convert a String to lower case, null String returns null.

Parameters: str the string to lowercase

Returns: the lower cased String

mid

public static String mid(String str, int pos, int len)

Gets n characters from the middle of a String.

If n characters are not available, the remainder of the String will be returned without an exception. If the String is null, null will be returned.

Parameters: str the String to get the characters from pos the position to start from len the length of the required String

Returns: the leftmost characters

Throws: IndexOutOfBoundsException if pos is out of bounds IllegalArgumentException if len is less than zero

overlayString

public static String overlayString(String text, String overlay, int start, int end)

Overlay a part of a String with another String.

Parameters: text String to do overlaying in overlay String to overlay start int to start overlaying at end int to stop overlaying before

Returns: String with overlayed text

Throws: NullPointerException if text or overlay is null

prechomp

public static String prechomp(String str, String sep)

Remove the first value of a supplied String, and everything before it from a String.

Parameters: str String to chomp from sep String to chomp

Returns: String without chomped beginning

Throws: NullPointerException if str or sep is null

removeAndHump

public static String removeAndHump(String data, String replaceThis)

repeat

public static String repeat(String str, int repeat)

Repeat a String n times to form a new string.

Parameters: str String to repeat repeat number of times to repeat str

Returns: String with repeated String

Throws: NegativeArraySizeException if repeat < 0 NullPointerException if str is null

replace

public static String replace(String text, char repl, char with)

Replace all occurances of a char within another char.

A null reference passed to this method is a no-op.

Parameters: text text to search and replace in repl char to search for with char to replace with

Returns: the text with any replacements processed

See Also: StringUtils

replace

public static String replace(String text, char repl, char with, int max)

Replace a char with another char inside a larger String, for the first max values of the search char.

A null reference passed to this method is a no-op.

Parameters: text text to search and replace in repl char to search for with char to replace with max maximum number of values to replace, or -1 if no maximum

Returns: the text with any replacements processed

replace

public static String replace(String text, String repl, String with)

Replace all occurances of a String within another String.

A null reference passed to this method is a no-op.

Parameters: text text to search and replace in repl String to search for with String to replace with

Returns: the text with any replacements processed

See Also: StringUtils

replace

public static String replace(String text, String repl, String with, int max)

Replace a String with another String inside a larger String, for the first max values of the search String.

A null reference passed to this method is a no-op.

Parameters: text text to search and replace in repl String to search for with String to replace with max maximum number of values to replace, or -1 if no maximum

Returns: the text with any replacements processed

replaceOnce

public static String replaceOnce(String text, char repl, char with)

Replace a char with another char inside a larger String, once.

A null reference passed to this method is a no-op.

Parameters: text text to search and replace in repl char to search for with char to replace with

Returns: the text with any replacements processed

See Also: StringUtils

replaceOnce

public static String replaceOnce(String text, String repl, String with)

Replace a String with another String inside a larger String, once.

A null reference passed to this method is a no-op.

Parameters: text text to search and replace in repl String to search for with String to replace with

Returns: the text with any replacements processed

See Also: StringUtils

reverse

public static String reverse(String str)

Reverse a String.

null String returns null.

Parameters: str the String to reverse

Returns: the reversed String

reverseArray

private static void reverseArray(Object[] array)

Reverses an array.

TAKEN FROM CollectionsUtils.

Parameters: array the array to reverse

reverseDelimitedString

public static String reverseDelimitedString(String str, String delimiter)

Reverses a String that is delimited by a specific character.

The Strings between the delimiters are not reversed. Thus java.lang.String becomes String.lang.java (if the delimiter is '.').

Parameters: str the String to reverse delimiter the delimiter to use

Returns: the reversed String

right

public static String right(String str, int len)

Gets the rightmost n characters of a String.

If n characters are not available, or the String is null, the String will be returned without an exception.

Parameters: str the String to get the rightmost characters from len the length of the required String

Returns: the leftmost characters

Throws: IllegalArgumentException if len is less than zero

rightPad

public static String rightPad(String str, int size)

Right pad a String with spaces.

The String is padded to the size of n.

Parameters: str String to repeat size number of times to repeat str

Returns: right padded String

Throws: NullPointerException if str is null

rightPad

public static String rightPad(String str, int size, String delim)

Right pad a String with a specified string.

The String is padded to the size of n.

Parameters: str String to pad out size size to pad to delim String to pad with

Returns: right padded String

Throws: NullPointerException if str or delim is null ArithmeticException if delim is the empty String

split

public static String[] split(String str)

Splits the provided text into a array, using whitespace as the separator.

The separator is not included in the returned String array.

Parameters: str the String to parse

Returns: an array of parsed Strings

split

public static String[] split(String text, String separator)

See Also: StringUtils

split

public static String[] split(String str, String separator, int max)

Splits the provided text into a array, based on a given separator.

The separator is not included in the returned String array. The maximum number of splits to perfom can be controlled. A null separator will cause parsing to be on whitespace.

This is useful for quickly splitting a String directly into an array of tokens, instead of an enumeration of tokens (as StringTokenizer does).

Parameters: str The string to parse. separator Characters used as the delimiters. If null, splits on whitespace. max The maximum number of elements to include in the array. A zero or negative value implies no limit.

Returns: an array of parsed Strings

strip

public static String strip(String str)

Remove whitespace from the front and back of a String.

Parameters: str the String to remove whitespace from

Returns: the stripped String

strip

public static String strip(String str, String delim)

Remove a specified String from the front and back of a String.

If whitespace is wanted to be removed, used the StringUtils method.

Parameters: str the String to remove a string from delim the String to remove at start and end

Returns: the stripped String

stripAll

public static String[] stripAll(String[] strs)

Strip whitespace from the front and back of every String in the array.

Parameters: strs the Strings to remove whitespace from

Returns: the stripped Strings

stripAll

public static String[] stripAll(String[] strs, String delimiter)

Strip the specified delimiter from the front and back of every String in the array.

Parameters: strs the Strings to remove a String from delimiter the String to remove at start and end

Returns: the stripped Strings

stripEnd

public static String stripEnd(String str, String strip)

Strip any of a supplied String from the end of a String.

If the strip String is null, whitespace is stripped.

Parameters: str the String to remove characters from strip the String to remove

Returns: the stripped String

stripStart

public static String stripStart(String str, String strip)

Strip any of a supplied String from the start of a String.

If the strip String is null, whitespace is stripped.

Parameters: str the String to remove characters from strip the String to remove

Returns: the stripped String

substring

public static String substring(String str, int start)

Gets a substring from the specified string avoiding exceptions.

A negative start position can be used to start n characters from the end of the String.

Parameters: str the String to get the substring from start the position to start from, negative means count back from the end of the String by this many characters

Returns: substring from start position

substring

public static String substring(String str, int start, int end)

Gets a substring from the specified String avoiding exceptions.

A negative start position can be used to start/end n characters from the end of the String.

Parameters: str the String to get the substring from start the position to start from, negative means count back from the end of the string by this many characters end the position to end at (exclusive), negative means count back from the end of the String by this many characters

Returns: substring from start position to end positon

swapCase

public static String swapCase(String str)

Swaps the case of String.

Properly looks after making sure the start of words are Titlecase and not Uppercase.

null is returned as null.

Parameters: str the String to swap the case of

Returns: the modified String

trim

public static String trim(String str)

Removes control characters, including whitespace, from both ends of this String, handling null by returning null.

Parameters: str the String to check

Returns: the trimmed text (or null)

See Also: java.lang.String#trim()

uncapitalise

public static String uncapitalise(String str)

Uncapitalise a String.

That is, convert the first character into lower-case. null is returned as null.

Parameters: str the String to uncapitalise

Returns: uncapitalised String

uncapitaliseAllWords

public static String uncapitaliseAllWords(String str)

Uncapitalise all the words in a string.

Uses Character#isWhitespace(char) as a separator between words.

null will return null.

Parameters: str the string to uncapitalise

Returns: uncapitalised string

upperCase

public static String upperCase(String str)

Convert a String to upper case, null String returns null.

Parameters: str the String to uppercase

Returns: the upper cased String