org.apache.maven.doxia.util

Class StringUtil


public class StringUtil
extends java.lang.Object

A collection of utility functions (static methods) operating on Strings and on lists of Strings. (What is called a list of Strings here is simply an array of Strings.)

Field Summary

static String[]
EMPTY_LIST
The empty list of Strings.

Method Summary

static String[]
append(String[] strings, String string)
Inserts a String as last item of a list of Strings.
static String
capitalize(String string)
Returns the specified string with its first character converted to upper case.
static boolean
contains(String[] strings, String string)
Tests if list strings contains String string.
static String
escape(String string)
Returns the specified string with all non-ASCII characters and non-printable ASCII characters replaced by the corresponding Java escape sequences (that is '\n', '\u00E9', etc).
static int
indexOf(String[] strings, String string)
Searches String string within list strings.
static String[]
insertAt(String[] strings, String string, int index)
Inserts a String inside a list of Strings.
static String
join(String[] strings, String separator)
Joins the items of the specified list of Strings using specified separator String.
static String
join(String[] strings, char separatorChar)
Joins the items of the specified list of Strings using specified separator char.
static void
main(String[] args)
A simple test for escape(String) and unescape(String).
static String[]
prepend(String[] strings, String string)
Inserts a String as first item of a list of Strings.
static String
protect(String string)
Like escape(String) but puts a double quote character ('\"') around the escaped string.
static String[]
remove(String[] strings, String string)
Removes a String from a list of Strings.
static String[]
removeAt(String[] strings, int index)
Removes an item specified by its position from a list of Strings.
static String
replaceAll(String string, String oldSub, String newSub)
Replaces substring oldSub by substring newSub inside String string.
static String[]
split(String string, char separatorChar)
Splits String string at occurences of char separatorChar.
static String
uncapitalize(String string)
Returns the specified string with its first character converted to lower case.
static String
unescape(String string)
Returns the specified string with Java escape sequences (that is '\n', '\u00E9', etc) replaced by the corresponding character.
static String
unprotect(String string)
Like unescape(String) but removes the double quote characters ('\"'), if any, before unescaping the string.

Field Details

EMPTY_LIST

public static final String[] EMPTY_LIST
The empty list of Strings.

Method Details

append

public static String[] append(String[] strings,
                              String string)
Inserts a String as last item of a list of Strings.
Parameters:
strings - the list where a String is to be inserted
string - the String to insert
Returns:
a list containing all the items of list strings plus String string inserted at its end

capitalize

public static String capitalize(String string)
Returns the specified string with its first character converted to upper case.
Parameters:
string - the String to be processed
Returns:
the specified string with its first character converted to upper case

contains

public static boolean contains(String[] strings,
                               String string)
Tests if list strings contains String string.
Parameters:
strings - the list to be searched
string - the String to search for
Returns:
true the string is found and false otherwise

escape

public static String escape(String string)
Returns the specified string with all non-ASCII characters and non-printable ASCII characters replaced by the corresponding Java escape sequences (that is '\n', '\u00E9', etc).
Parameters:
string - the String to be escaped
Returns:
the specified string with all non-ASCII characters and non-printable ASCII characters replaced by the corresponding Java escape sequences

indexOf

public static int indexOf(String[] strings,
                          String string)
Searches String string within list strings.
Parameters:
strings - the list to be searched
string - the String to search for
Returns:
the index of the searched string within list or -1 if not found

insertAt

public static String[] insertAt(String[] strings,
                                String string,
                                int index)
Inserts a String inside a list of Strings.
Parameters:
strings - the list where a String is to be inserted
string - the String to insert
index - the insertion index
Returns:
a list containing all the items of list strings plus String string inserted at position index

join

public static String join(String[] strings,
                          String separator)
Joins the items of the specified list of Strings using specified separator String.
Parameters:
strings - the list where items are to be joined
separator - the String used to join items
Returns:
a string where all list items have been joined

join

public static String join(String[] strings,
                          char separatorChar)
Joins the items of the specified list of Strings using specified separator char.
Parameters:
strings - the list where items are to be joined
separatorChar - the char used to join items
Returns:
a string where all list items have been joined

main

public static final void main(String[] args)

prepend

public static String[] prepend(String[] strings,
                               String string)
Inserts a String as first item of a list of Strings.
Parameters:
strings - the list where a String is to be inserted
string - the String to insert
Returns:
a list containing all the items of list strings plus String string inserted at its beginning

protect

public static String protect(String string)
Like escape(String) but puts a double quote character ('\"') around the escaped string.

remove

public static String[] remove(String[] strings,
                              String string)
Removes a String from a list of Strings.
Parameters:
strings - the list where a String is to be removed
string - the String to remove
Returns:
a list containing all the items of list strings less String string if such String is contained in the list; the original list otherwise.

removeAt

public static String[] removeAt(String[] strings,
                                int index)
Removes an item specified by its position from a list of Strings.
Parameters:
strings - the list where an item is to be removed
index - the position of the item to remove
Returns:
a list containing all the items of list strings less the item at position index.

replaceAll

public static String replaceAll(String string,
                                String oldSub,
                                String newSub)
Replaces substring oldSub by substring newSub inside String string.
Parameters:
string - the String where replacements are to be performed
oldSub - the substring to replace
newSub - the replacement substring
Returns:
a string where all replacements have been performed
See Also:
String.replace

split

public static String[] split(String string,
                             char separatorChar)
Splits String string at occurences of char separatorChar.
Parameters:
string - the String to be split
separatorChar - the char where to split
Returns:
the list of substrings resulting from splitting String string at occurences of char separatorChar

Note that each occurence of separatorChar specifies the end of a substring. Therefore, the returned list may contain empty substrings if consecutive separatorChars are found in String string.


uncapitalize

public static String uncapitalize(String string)
Returns the specified string with its first character converted to lower case.
Parameters:
string - the String to be processed
Returns:
the specified string with its first character converted to lower case

unescape

public static String unescape(String string)
Returns the specified string with Java escape sequences (that is '\n', '\u00E9', etc) replaced by the corresponding character.
Parameters:
string - the String to be unescaped
Returns:
the specified string with Java escape sequences replaced by the corresponding character

unprotect

public static String unprotect(String string)
Like unescape(String) but removes the double quote characters ('\"'), if any, before unescaping the string.