TextUtils

public class TextUtils extends Object

Nested Class Summary

class TextUtils.SimpleStringSplitter A simple string splitter. 
interface TextUtils.StringSplitter An interface for splitting strings according to rules that are opaque to the user of this interface. 
enum TextUtils.TruncateAt  

Constant Summary

int CAP_MODE_CHARACTERS Capitalization mode for getCapsMode(CharSequence, int, int): capitalize all characters.
int CAP_MODE_SENTENCES Capitalization mode for getCapsMode(CharSequence, int, int): capitalize the first character of each sentence.
int CAP_MODE_WORDS Capitalization mode for getCapsMode(CharSequence, int, int): capitalize the first character of all words.

Public Method Summary

static CharSequence
concat(CharSequence... text)
Returns a CharSequence concatenating the specified CharSequences, retaining their spans if any.
static void
copySpansFrom(Spanned source, int start, int end, Class kind, Spannable dest, int destoff)
Copies the spans from the region start...end in source to the region destoff...destoff+end-start in dest.
static void
dumpSpans(CharSequence cs, Printer printer, String prefix)
Debugging tool to print the spans in a CharSequence.
static boolean
equals(CharSequence a, CharSequence b)
Returns true if a and b are equal, including if they are both null.
static CharSequence
expandTemplate(CharSequence template, CharSequence... values)
Return a new CharSequence in which each of the source strings is replaced by the corresponding element of the destinations.
static int
getCapsMode(CharSequence cs, int off, int reqModes)
Determine what caps mode should be in effect at the current offset in the text.
static void
getChars(CharSequence s, int start, int end, char[] dest, int destoff)
static int
getOffsetAfter(CharSequence text, int offset)
static int
getOffsetBefore(CharSequence text, int offset)
static int
getTrimmedLength(CharSequence s)
Returns the length that the specified CharSequence would have if spaces and control characters were trimmed from the start and end, as by String.trim().
static String
htmlEncode(String s)
Html-encode the string.
static int
indexOf(CharSequence s, char ch, int start, int end)
static int
indexOf(CharSequence s, CharSequence needle, int start, int end)
static int
indexOf(CharSequence s, CharSequence needle, int start)
static int
indexOf(CharSequence s, CharSequence needle)
static int
indexOf(CharSequence s, char ch, int start)
static int
indexOf(CharSequence s, char ch)
static boolean
isDigitsOnly(CharSequence str)
Returns whether the given CharSequence contains only digits.
static boolean
isEmpty(CharSequence str)
Returns true if the string is null or 0-length.
static boolean
isGraphic(CharSequence str)
Returns whether the given CharSequence contains any printable characters.
static boolean
isGraphic(char c)
Returns whether this character is a printable character.
static String
join(CharSequence delimiter, Iterable tokens)
Returns a string containing the tokens joined by delimiters.
static String
join(CharSequence delimiter, Object[] tokens)
Returns a string containing the tokens joined by delimiters.
static int
lastIndexOf(CharSequence s, char ch, int last)
static int
lastIndexOf(CharSequence s, char ch)
static int
lastIndexOf(CharSequence s, char ch, int start, int last)
static boolean
regionMatches(CharSequence one, int toffset, CharSequence two, int ooffset, int len)
static String[]
split(String text, Pattern pattern)
Splits a string on a pattern.
static String[]
split(String text, String expression)
String.split() returns [''] when the string to be split is empty.
static CharSequence
stringOrSpannedString(CharSequence source)
static String
substring(CharSequence source, int start, int end)
Create a new String object containing the given range of characters from the source string.

Inherited Method Summary

Constants

public static final int CAP_MODE_CHARACTERS

Capitalization mode for getCapsMode(CharSequence, int, int): capitalize all characters. This value is explicitly defined to be the same as InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS.

Constant Value: 4096

public static final int CAP_MODE_SENTENCES

Capitalization mode for getCapsMode(CharSequence, int, int): capitalize the first character of each sentence. This value is explicitly defined to be the same as InputType.TYPE_TEXT_FLAG_CAP_SENTENCES.

Constant Value: 16384

public static final int CAP_MODE_WORDS

Capitalization mode for getCapsMode(CharSequence, int, int): capitalize the first character of all words. This value is explicitly defined to be the same as InputType.TYPE_TEXT_FLAG_CAP_WORDS.

Constant Value: 8192

Public Methods

public static CharSequence concat (CharSequence... text)

Returns a CharSequence concatenating the specified CharSequences, retaining their spans if any.

Parameters
text

public static void copySpansFrom (Spanned source, int start, int end, Class kind, Spannable dest, int destoff)

Copies the spans from the region start...end in source to the region destoff...destoff+end-start in dest. Spans in source that begin before start or end after end but overlap this range are trimmed as if they began at start or ended at end.

Parameters
source
start
end
kind
dest
destoff
Throws
IndexOutOfBoundsException if any of the copied spans are out of range in dest.

public static void dumpSpans (CharSequence cs, Printer printer, String prefix)

Debugging tool to print the spans in a CharSequence. The output will be printed one span per line. If the CharSequence is not a Spanned, then the entire string will be printed on a single line.

Parameters
cs
printer
prefix

public static boolean equals (CharSequence a, CharSequence b)

Returns true if a and b are equal, including if they are both null.

Note: In platform versions 1.1 and earlier, this method only worked well if both the arguments were instances of String.

Parameters
a first CharSequence to check
b second CharSequence to check
Returns
  • true if a and b are equal

public static CharSequence expandTemplate (CharSequence template, CharSequence... values)

Return a new CharSequence in which each of the source strings is replaced by the corresponding element of the destinations. TODO(tball): enable if SpannableStringBuilder is ever implemented, which requires android.graphics support. public static CharSequence replace(CharSequence template, String[] sources, CharSequence[] destinations) { SpannableStringBuilder tb = new SpannableStringBuilder(template); for (int i = 0; i < sources.length; i++) { int where = indexOf(tb, sources[i]); if (where >= 0) tb.setSpan(sources[i], where, where + sources[i].length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } for (int i = 0; i < sources.length; i++) { int start = tb.getSpanStart(sources[i]); int end = tb.getSpanEnd(sources[i]); if (start >= 0) { tb.replace(start, end, destinations[i]); } } return tb; } /** Replace instances of "^1", "^2", etc. in the template CharSequence with the corresponding values. "^^" is used to produce a single caret in the output. Only up to 9 replacement values are supported, "^10" will be produce the first replacement value followed by a '0'.

Parameters
template the input text containing "^1"-style placeholder values. This object is not modified; a copy is returned.
values CharSequences substituted into the template. The first is substituted for "^1", the second for "^2", and so on.
Returns
  • the new CharSequence produced by doing the replacement
Throws
IllegalArgumentException if the template requests a value that was not provided, or if more than 9 values are provided.

public static int getCapsMode (CharSequence cs, int off, int reqModes)

Determine what caps mode should be in effect at the current offset in the text. Only the mode bits set in reqModes will be checked. Note that the caps mode flags here are explicitly defined to match those in InputType.

Parameters
cs The text that should be checked for caps modes.
off Location in the text at which to check.
reqModes The modes to be checked: may be any combination of CAP_MODE_CHARACTERS, CAP_MODE_WORDS, and CAP_MODE_SENTENCES.
Returns

public static void getChars (CharSequence s, int start, int end, char[] dest, int destoff)

Parameters
s
start
end
dest
destoff

public static int getOffsetAfter (CharSequence text, int offset)

Parameters
text
offset

public static int getOffsetBefore (CharSequence text, int offset)

Parameters
text
offset

public static int getTrimmedLength (CharSequence s)

Returns the length that the specified CharSequence would have if spaces and control characters were trimmed from the start and end, as by String.trim().

Parameters
s

public static String htmlEncode (String s)

Html-encode the string.

Parameters
s the string to be encoded
Returns
  • the encoded string

public static int indexOf (CharSequence s, char ch, int start, int end)

Parameters
s
ch
start
end

public static int indexOf (CharSequence s, CharSequence needle, int start, int end)

Parameters
s
needle
start
end

public static int indexOf (CharSequence s, CharSequence needle, int start)

Parameters
s
needle
start

public static int indexOf (CharSequence s, CharSequence needle)

Parameters
s
needle

public static int indexOf (CharSequence s, char ch, int start)

Parameters
s
ch
start

public static int indexOf (CharSequence s, char ch)

Parameters
s
ch

public static boolean isDigitsOnly (CharSequence str)

Returns whether the given CharSequence contains only digits.

Parameters
str

public static boolean isEmpty (CharSequence str)

Returns true if the string is null or 0-length.

Parameters
str the string to be examined
Returns
  • true if str is null or zero length

public static boolean isGraphic (CharSequence str)

Returns whether the given CharSequence contains any printable characters.

Parameters
str

public static boolean isGraphic (char c)

Returns whether this character is a printable character.

Parameters
c

public static String join (CharSequence delimiter, Iterable tokens)

Returns a string containing the tokens joined by delimiters.

Parameters
delimiter
tokens an array objects to be joined. Strings will be formed from the objects by calling object.toString().

public static String join (CharSequence delimiter, Object[] tokens)

Returns a string containing the tokens joined by delimiters.

Parameters
delimiter
tokens an array objects to be joined. Strings will be formed from the objects by calling object.toString().

public static int lastIndexOf (CharSequence s, char ch, int last)

Parameters
s
ch
last

public static int lastIndexOf (CharSequence s, char ch)

Parameters
s
ch

public static int lastIndexOf (CharSequence s, char ch, int start, int last)

Parameters
s
ch
start
last

public static boolean regionMatches (CharSequence one, int toffset, CharSequence two, int ooffset, int len)

Parameters
one
toffset
two
ooffset
len

public static String[] split (String text, Pattern pattern)

Splits a string on a pattern. String.split() returns [''] when the string to be split is empty. This returns []. This does not remove any empty strings from the result.

Parameters
text the string to split
pattern the regular expression to match
Returns
  • an array of strings. The array will be empty if text is empty
Throws
NullPointerException if expression or text is null

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

String.split() returns [''] when the string to be split is empty. This returns []. This does not remove any empty strings from the result. For example split("a,", "," ) returns {"a", ""}.

Parameters
text the string to split
expression the regular expression to match
Returns
  • an array of strings. The array will be empty if text is empty
Throws
NullPointerException if expression or text is null

public static CharSequence stringOrSpannedString (CharSequence source)

Parameters
source

public static String substring (CharSequence source, int start, int end)

Create a new String object containing the given range of characters from the source string. This is different than simply calling CharSequence.subSequence in that it does not preserve any style runs in the source sequence, allowing a more efficient implementation.

Parameters
source
start
end