ix.util
Class Strings

java.lang.Object
  |
  +--ix.util.Strings

public final class Strings
extends java.lang.Object

A class containing useful static string methods.


Method Summary
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.util.List breakAt(java.lang.String separator, java.lang.String s)
          Returns a list of the substrings delimited by the given separator.
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 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 dashNameToFullJavaName(java.lang.String name)
          Like dashNameToJavaName but also handles parts separated by single "."s, as in a fully qualified 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 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 fullJavaNameToDashName(java.lang.String name)
          Like dashNameToJavaName but also handles parts separated by single "."s, as in a fully qualified class name.
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 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 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 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 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

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 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 reault is a one-element list containing the string as-is.
Parameters:
separator - the delimiter
s - the string that may contain it

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

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

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 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.

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.

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.

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 handles parts separated by single "."s, as in a fully qualified class name.

Note that it is not always possible to determine which Java name should be returned - not even under assumption that the result should be a class name - because it is not always possible to determine which part of a dash-name should refer to a package and which to a class. For example, the dash-name "a.b.c" might refer to class B.C in package "a" or to class C in package "a.b", and both of those classes and packages might exist.

Here are the rules used by this method:

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 dashNameToJavaName but also handles parts separated by single "."s, as in a fully qualified class name.

Note that it is not always possible to convert back to the same Java name, because it is not always possible to determine which part of a dash-name should refer to a package and which to a class. For example, the dash-name "a.b.c" might refer to class B.C in package "a" or to class C in package "a.b".

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