ix.util
Class Strings

java.lang.Object
  extended by ix.util.Strings

public final class Strings
extends java.lang.Object

A class containing useful static string methods.


Method Summary
static java.lang.String afterFirst(java.lang.String separator, java.lang.String s)
          Returns the substring that begins directly after the first occurrence of the separator.
static java.lang.String afterLast(java.lang.String separator, java.lang.String s)
          Returns the substring starting directly after the last occurrence of the separator.
static java.lang.String beforeFirst(java.lang.String separator, java.lang.String s)
          Returns the substring that ends directly before the first occurrence of the separator.
static java.lang.String beforeLast(java.lang.String separator, java.lang.String s)
          Returns the substring that ends directly before the last occurrence of the separator.
static java.util.List<java.lang.String> breakAt(java.lang.String separator, java.lang.String s)
          Returns a list of the substrings delimited by the given separator.
static java.util.List<java.lang.String> breakAtAny(java.lang.String separatorChars, java.lang.String s)
          Returns a list of substrings, breaking the string at every occurrence of a separator charater.
static java.lang.String[] breakAtFirst(java.lang.String separator, java.lang.String s)
          breakAtFirst takes a string containing fields separated by a (string) delimiter and returns a two-element string array containing the substring before the first occurrence of the separator and the substring after.
static java.lang.String[] breakAtLast(java.lang.String separator, java.lang.String s)
          breakAtLast takes a string containing fields separated by a (string) delimiter and returns a two-element string array containing the substring before the last occurrence of the separator and the substring after.
static java.util.List<java.lang.String> breakIntoLines(java.lang.String text)
          Returns a List of the lines in a string.
static java.lang.String capitalize(java.lang.String s)
          Returns a copy of the string in which the first character is in upper case and the rest of the string is left as it was.
static java.lang.String conjunction(java.util.Collection items)
          Returns a conjunction formed from the elements of a collection.
static java.lang.String dashNameToFullJavaName(java.lang.String name)
          Like dashNameToJavaName but also allows package names, separated by dots, to prefix the class name.
static java.lang.String dashNameToJavaName(java.lang.String name)
          Converts a name in which words are separate by dashes into one that uses Java-style capitalisation instead.
static java.lang.String disjunction(java.util.Collection items)
          Returns a disjunction formed from the elements of a collection.
static java.lang.String firstLine(java.lang.String s)
          Returns the first line of a string.
static java.lang.String foldLongLine(java.lang.String text)
          Replaces some spaces with line separators to make a long string more readable in contexts where it would have been displayed as a single line.
static java.lang.String foldLongLine(java.lang.String text, int maxLen, java.lang.String prefix)
          Replaces some spaces with line separators to make a long string more readable in contexts where it would have been displayed as a single line.
static java.lang.String[] foldToArray(java.lang.String text)
          Makes a long string more readable by breaking it into lines.
static java.lang.String fullJavaNameToDashName(java.lang.String name)
          Like javaNameToDashName but also allows package names, separated by dots, to prefix the class name.
static java.lang.String indefinite(java.lang.String noun)
          Returns a string prefixed by "an " or "a " depending on whether or not it begins with an English vowel.
static int indexOfAny(java.lang.String chars, int start, java.lang.String s)
          Returns the index of the first occurrence of any of the specified characters, or -1 if none are found.
static int indexOfAny(java.lang.String chars, java.lang.String s)
          Returns the index of the first occurrence of any of the specified characters, or -1 if none are found.
static boolean isAllLowerCase(java.lang.String s)
          Determines whether there are no upper-case characters in a string.
static boolean isVowel(char c)
          Returns true if the character is an English vowel (a, e, i, o, or u) and false if it is not.
static java.lang.String javaNameToDashName(java.lang.String name)
          Converts a name that shows word boundaries using Java-style capitalization to a name in which words are (almost) all lower case and separated by dashes.
static java.lang.String joinLines(java.util.List lines)
          Returns a String formed by appending a list of strings with a line-separator between adjacent elements.
static java.lang.String joinLines(java.lang.String[] lines)
          Returns a String formed by appending an array of strings with a line-separator between adjacent elements.
static java.lang.String joinWith(java.lang.String separator, java.util.List substrings)
          Returns a String formed by appending a List of strings with a separator between adjacent elements.
static java.lang.String joinWith(java.lang.String separator, java.lang.String[] substrings)
          Returns a String formed by appending an array of strings with a separator between adjacent elements.
static java.lang.String quote(java.lang.String text)
          Puts double quotes around a string.
static java.lang.String repeat(int count, java.lang.String s)
          Returns a String made by appending a specified string count times.
static java.lang.String replace(java.lang.String from, java.lang.String to, java.lang.String source)
          Returns a string formed by replacing every occurrence of from with to in source.
static java.lang.String replaceChars(java.lang.String from, java.lang.String to, java.lang.String source)
          Returns a copy of source in any character that appears in from has been replaced by the the corresponding char in to.
static java.lang.String[] toArray(java.util.Collection strings)
          Converts a collection of Strings to a new String array of the same size.
static java.lang.String uncapitalize(java.lang.String s)
          Returns a copy of the string in which the first character is in lower case and the rest of the string is left as it was.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

toArray

public static java.lang.String[] toArray(java.util.Collection strings)
Converts a collection of Strings to a new String array of the same size.


indexOfAny

public static int indexOfAny(java.lang.String chars,
                             java.lang.String s)
Returns the index of the first occurrence of any of the specified characters, or -1 if none are found.

Parameters:
chars - the characters to look for
s - the string to look in

indexOfAny

public static int indexOfAny(java.lang.String chars,
                             int start,
                             java.lang.String s)
Returns the index of the first occurrence of any of the specified characters, or -1 if none are found.

Parameters:
chars - the characters to look for
start - index to start looking
s - the string to look in

breakAtFirst

public static java.lang.String[] breakAtFirst(java.lang.String separator,
                                              java.lang.String s)
breakAtFirst takes a string containing fields separated by a (string) delimiter and returns a two-element string array containing the substring before the first occurrence of the separator and the substring after. Neither substring contains the delimiter. If the delimiter does not appear in the string at all, the values are the string and "".

Parameters:
separator - the delimiter
s - the string that may contain it

breakAtLast

public static java.lang.String[] breakAtLast(java.lang.String separator,
                                             java.lang.String s)
breakAtLast takes a string containing fields separated by a (string) delimiter and returns a two-element string array containing the substring before the last occurrence of the separator and the substring after. Neither substring contains the delimiter. If the delimiter does not appear in the string at all, the values are the string and "".

Parameters:
separator - the delimiter
s - the string that may contain it

breakAt

public static java.util.List<java.lang.String> breakAt(java.lang.String separator,
                                                       java.lang.String s)
Returns a list of the substrings delimited by the given separator. The separator itself does not appear in any element of the list, and if the separator does not occur, the result is a one-element list containing the string as-is.

Parameters:
separator - the delimiter
s - the string that may contain it

breakAtAny

public static java.util.List<java.lang.String> breakAtAny(java.lang.String separatorChars,
                                                          java.lang.String s)
Returns a list of substrings, breaking the string at every occurrence of a separator charater. The separators are given in a string so that more than one may be specified. None of the separators will appear in any of the result substrings, and if no separator occurs, the result is a one-element list containing the original string as-is.

Parameters:
separatorChars - the separators
s - the string to break into parts
See Also:
breakAt(String, String)

joinWith

public static java.lang.String joinWith(java.lang.String separator,
                                        java.util.List substrings)
Returns a String formed by appending a List of strings with a separator between adjacent elements. If none of the strings contain the separator, this is the inverse of the operatrion performed by the breakAt method.

Parameters:
separator - the delimiter that will separate substrings
substrings - a list of the Strings to join

joinWith

public static java.lang.String joinWith(java.lang.String separator,
                                        java.lang.String[] substrings)
Returns a String formed by appending an array of strings with a separator between adjacent elements.

Parameters:
separator - the delimiter that will separate substrings
substrings - an array of the Strings to join

joinLines

public static java.lang.String joinLines(java.util.List lines)
Returns a String formed by appending a list of strings with a line-separator between adjacent elements.

Parameters:
lines - a list of the Strings to join

joinLines

public static java.lang.String joinLines(java.lang.String[] lines)
Returns a String formed by appending an array of strings with a line-separator between adjacent elements.

Parameters:
lines - an array of the Strings to join

beforeFirst

public static java.lang.String beforeFirst(java.lang.String separator,
                                           java.lang.String s)
Returns the substring that ends directly before the first occurrence of the separator. If the separator does not occur, the entire string is returned as-is.

Parameters:
separator - the delimiter
s - the string that may contain it

afterFirst

public static java.lang.String afterFirst(java.lang.String separator,
                                          java.lang.String s)
Returns the substring that begins directly after the first occurrence of the separator. If the separator does not occur, the entire string is returned as-is.

Parameters:
separator - the delimiter
s - the string that may contain it

beforeLast

public static java.lang.String beforeLast(java.lang.String separator,
                                          java.lang.String s)
Returns the substring that ends directly before the last occurrence of the separator. If the separator does not occur, the entire string is returned as-is.

Parameters:
separator - the delimiter
s - the string that may contain it

afterLast

public static java.lang.String afterLast(java.lang.String separator,
                                         java.lang.String s)
Returns the substring starting directly after the last occurrence of the separator. If the separator does not occur, the entire string is returned as-is.

Parameters:
separator - the delimiter
s - the string that may contain it

firstLine

public static java.lang.String firstLine(java.lang.String s)
Returns the first line of a string.


breakIntoLines

public static java.util.List<java.lang.String> breakIntoLines(java.lang.String text)
Returns a List of the lines in a string.


foldLongLine

public static java.lang.String foldLongLine(java.lang.String text)
Replaces some spaces with line separators to make a long string more readable in contexts where it would have been displayed as a single line. Any line separators already in the text are retained and are taken into account when calculating line lengths.


foldLongLine

public static java.lang.String foldLongLine(java.lang.String text,
                                            int maxLen,
                                            java.lang.String prefix)
Replaces some spaces with line separators to make a long string more readable in contexts where it would have been displayed as a single line. Any line separators already in the text are retained and are taken into account when calculating line lengths.

The prefix, if not null, is put at the front of every resulting line except the first.


foldToArray

public static java.lang.String[] foldToArray(java.lang.String text)
Makes a long string more readable by breaking it into lines. This is a variation on foldLongLine(String) that returns an array of lines rather than a string containing containing newline sequences.


replace

public static java.lang.String replace(java.lang.String from,
                                       java.lang.String to,
                                       java.lang.String source)
Returns a string formed by replacing every occurrence of from with to in source. If any replacements are made, the result is a new string without any modifications to the original. If, on the other hand, from does not occur, the original string is returned rather than a copy.

Parameters:
from - the text to replace
to - the text to replace it with
source - the text in which to do the replacing
Returns:
a new string if any replacements were needed; otherwise the original source string.

replaceChars

public static java.lang.String replaceChars(java.lang.String from,
                                            java.lang.String to,
                                            java.lang.String source)
Returns a copy of source in any character that appears in from has been replaced by the the corresponding char in to. If any replacements are made, the result is a new string without any modifications to the original. Otherwise, the original string is returned rather than a copy.

Parameters:
from - chars to replace
to - the chars to replace them with
source - the text in which to do the replacing
Returns:
a new string if any replacements were needed; otherwise the original source string.

quote

public static java.lang.String quote(java.lang.String text)
Puts double quotes around a string. This is useful to indicate the boundaries of the string when it's included in other text such as in an error or warning message.


isVowel

public static boolean isVowel(char c)
Returns true if the character is an English vowel (a, e, i, o, or u) and false if it is not.


indefinite

public static java.lang.String indefinite(java.lang.String noun)
Returns a string prefixed by "an " or "a " depending on whether or not it begins with an English vowel.

See Also:
isVowel(char)

disjunction

public static java.lang.String disjunction(java.util.Collection items)
Returns a disjunction formed from the elements of a collection. The result contains the elements converted to strings, spearated by commans, and with an " or " before the final element.


conjunction

public static java.lang.String conjunction(java.util.Collection items)
Returns a conjunction formed from the elements of a collection. The result contains the elements converted to strings, spearated by commans, and with an " and " before the final element.


isAllLowerCase

public static boolean isAllLowerCase(java.lang.String s)
Determines whether there are no upper-case characters in a string.


capitalize

public static java.lang.String capitalize(java.lang.String s)
Returns a copy of the string in which the first character is in upper case and the rest of the string is left as it was.


uncapitalize

public static java.lang.String uncapitalize(java.lang.String s)
Returns a copy of the string in which the first character is in lower case and the rest of the string is left as it was.


repeat

public static java.lang.String repeat(int count,
                                      java.lang.String s)
Returns a String made by appending a specified string count times.


dashNameToJavaName

public static java.lang.String dashNameToJavaName(java.lang.String name)
Converts a name in which words are separate by dashes into one that uses Java-style capitalisation instead. The first character of the result will be upper-case, as if it were following the convention for class names.

See Also:
javaNameToDashName(String), dashNameToFullJavaName(String)

dashNameToFullJavaName

public static java.lang.String dashNameToFullJavaName(java.lang.String name)
Like dashNameToJavaName but also allows package names, separated by dots, to prefix the class name. The package names are used unchanged, because we expect them to follow the convention of being all in lower case.

The names of inner classes (which contain dollar-signs) are handled by converting the sequence of class names.

See Also:
dashNameToJavaName(String), fullJavaNameToDashName(String)

javaNameToDashName

public static java.lang.String javaNameToDashName(java.lang.String name)
Converts a name that shows word boundaries using Java-style capitalization to a name in which words are (almost) all lower case and separated by dashes. The "almost" is because in some cases upper case is preserved so that conversion back to a Java name will be correct. The division into words is performed by a JavaNameWordIterator; the resulting words are converted to all lower case unless they begin with 2 upper-case characters, and then they are left as-is.

See Also:
JavaNameWordIterator, dashNameToJavaName(String), fullJavaNameToDashName(String)

fullJavaNameToDashName

public static java.lang.String fullJavaNameToDashName(java.lang.String name)
Like javaNameToDashName but also allows package names, separated by dots, to prefix the class name. The package names are used unchanged, because we expect them to follow the convention of being all in lower case.

The names of inner classes (which contain dollar-signs) are handled by converting the sequence of class names.

See Also:
javaNameToDashName(String), dashNameToFullJavaName(String)