public class TextUtils extends Object
Modifier and Type | Class and Description |
---|---|
static interface |
TextUtils.EllipsizeCallback |
static class |
TextUtils.SimpleStringSplitter
A simple string splitter.
|
static interface |
TextUtils.StringSplitter
An interface for splitting strings according to rules that are opaque to the user of this
interface.
|
static class |
TextUtils.TruncateAt |
Modifier and Type | Method and Description |
---|---|
static CharSequence |
commaEllipsize(CharSequence text,
TextPaint p,
float avail,
String oneMore,
String more)
Converts a CharSequence of the comma-separated form "Andy, Bob,
Charles, David" that is too wide to fit into the specified width
into one like "Andy, Bob, 2 more".
|
static CharSequence |
commaEllipsize(CharSequence text,
TextPaint p,
float avail,
String oneMore,
String more,
TextDirectionHeuristic textDir) |
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 boolean |
delimitedStringContains(String delimitedString,
char delimiter,
String item)
Does a comma-delimited list 'delimitedString' contain a certain item?
|
static void |
dumpSpans(CharSequence cs,
Printer printer,
String prefix)
Debugging tool to print the spans in a CharSequence.
|
static CharSequence |
ellipsize(CharSequence text,
TextPaint p,
float avail,
TextUtils.TruncateAt where)
Returns the original text if it fits in the specified width
given the properties of the specified Paint,
or, if it does not fit, a truncated
copy with ellipsis character added at the specified edge or center.
|
static CharSequence |
ellipsize(CharSequence text,
TextPaint paint,
float avail,
TextUtils.TruncateAt where,
boolean preserveLength,
TextUtils.EllipsizeCallback callback)
Returns the original text if it fits in the specified width
given the properties of the specified Paint,
or, if it does not fit, a copy with ellipsis character added
at the specified edge or center.
|
static CharSequence |
ellipsize(CharSequence text,
TextPaint paint,
float avail,
TextUtils.TruncateAt where,
boolean preserveLength,
TextUtils.EllipsizeCallback callback,
TextDirectionHeuristic textDir,
String ellipsis)
Returns the original text if it fits in the specified width
given the properties of the specified Paint,
or, if it does not fit, a copy with ellipsis character added
at the specified edge or center.
|
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)
Replace instances of "^1", "^2", etc. in the
template CharSequence with the corresponding
values . "^^" is used to produce a single caret in
the output. |
static CharSequence |
formatSelectedCount(int count)
Return localized string representing the given number of selected items.
|
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 |
getLayoutDirectionFromLocale(Locale locale)
Return the layout direction for a given Locale
|
static int |
getOffsetAfter(CharSequence text,
int offset) |
static int |
getOffsetBefore(CharSequence text,
int offset) |
static CharSequence |
getReverse(CharSequence source,
int start,
int end)
Deprecated.
Do not use.
|
static int |
getTrimmedLength(CharSequence s)
Returns the length that the specified CharSequence would have if
spaces and ASCII 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) |
static int |
indexOf(CharSequence s,
char ch,
int start) |
static int |
indexOf(CharSequence s,
char ch,
int start,
int end) |
static int |
indexOf(CharSequence s,
CharSequence needle) |
static int |
indexOf(CharSequence s,
CharSequence needle,
int start) |
static int |
indexOf(CharSequence s,
CharSequence needle,
int start,
int end) |
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(char c)
Deprecated.
Use
isGraphic(CharSequence) instead. |
static boolean |
isGraphic(CharSequence str)
Returns whether the given CharSequence contains any printable characters.
|
static boolean |
isPrintableAscii(char c) |
static boolean |
isPrintableAsciiOnly(CharSequence str) |
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) |
static int |
lastIndexOf(CharSequence s,
char ch,
int last) |
static int |
lastIndexOf(CharSequence s,
char ch,
int start,
int last) |
static String |
nullIfEmpty(String str) |
static long |
packRangeInLong(int start,
int end)
Pack 2 int values into a long, useful as a return value for a range
|
static boolean |
regionMatches(CharSequence one,
int toffset,
CharSequence two,
int ooffset,
int len) |
static <T> T[] |
removeEmptySpans(T[] spans,
Spanned spanned,
Class<T> klass)
Removes empty spans from the
spans array. |
static CharSequence |
replace(CharSequence template,
String[] sources,
CharSequence[] destinations)
Return a new CharSequence in which each of the source strings is
replaced by the corresponding element of the destinations.
|
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.
|
static int |
unpackRangeEndFromLong(long range)
Get the end value from a range packed in a long by
packRangeInLong(int, int) |
static int |
unpackRangeStartFromLong(long range)
Get the start value from a range packed in a long by
packRangeInLong(int, int) |
static void |
writeToParcel(CharSequence cs,
Parcel p,
int parcelableFlags)
Flatten a CharSequence and whatever styles can be copied across processes
into the parcel.
|
public static final String ELLIPSIS_STRING
public static final int ALIGNMENT_SPAN
public static final int FIRST_SPAN
public static final int FOREGROUND_COLOR_SPAN
public static final int RELATIVE_SIZE_SPAN
public static final int SCALE_X_SPAN
public static final int STRIKETHROUGH_SPAN
public static final int UNDERLINE_SPAN
public static final int STYLE_SPAN
public static final int BULLET_SPAN
public static final int QUOTE_SPAN
public static final int LEADING_MARGIN_SPAN
public static final int URL_SPAN
public static final int BACKGROUND_COLOR_SPAN
public static final int TYPEFACE_SPAN
public static final int SUPERSCRIPT_SPAN
public static final int SUBSCRIPT_SPAN
public static final int ABSOLUTE_SIZE_SPAN
public static final int TEXT_APPEARANCE_SPAN
public static final int ANNOTATION
public static final int SUGGESTION_SPAN
public static final int SPELL_CHECK_SPAN
public static final int SUGGESTION_RANGE_SPAN
public static final int EASY_EDIT_SPAN
public static final int LOCALE_SPAN
public static final int TTS_SPAN
public static final int LAST_SPAN
public static final Parcelable.Creator<CharSequence> CHAR_SEQUENCE_CREATOR
public static final int CAP_MODE_CHARACTERS
getCapsMode(java.lang.CharSequence, int, int)
: capitalize all
characters. This value is explicitly defined to be the same as
InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS
.public static final int CAP_MODE_WORDS
getCapsMode(java.lang.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
.public static final int CAP_MODE_SENTENCES
getCapsMode(java.lang.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
.public static void getChars(CharSequence s, int start, int end, char[] dest, int destoff)
public static int indexOf(CharSequence s, char ch)
public static int indexOf(CharSequence s, char ch, int start)
public static int indexOf(CharSequence s, char ch, int start, int end)
public static int lastIndexOf(CharSequence s, char ch)
public static int lastIndexOf(CharSequence s, char ch, int last)
public static int lastIndexOf(CharSequence s, char ch, int start, int last)
public static int indexOf(CharSequence s, CharSequence needle)
public static int indexOf(CharSequence s, CharSequence needle, int start)
public static int indexOf(CharSequence s, CharSequence needle, int start, int end)
public static boolean regionMatches(CharSequence one, int toffset, CharSequence two, int ooffset, int len)
public static String substring(CharSequence source, int start, int end)
CharSequence.subSequence
in that it does not preserve any style runs in the source sequence,
allowing a more efficient implementation.public static String join(CharSequence delimiter, Object[] tokens)
tokens
- an array objects to be joined. Strings will be formed from
the objects by calling object.toString().public static String join(CharSequence delimiter, Iterable tokens)
tokens
- an array objects to be joined. Strings will be formed from
the objects by calling object.toString().public static String[] split(String text, String expression)
text
- the string to splitexpression
- the regular expression to matchNullPointerException
- if expression or text is nullpublic static String[] split(String text, Pattern pattern)
text
- the string to splitpattern
- the regular expression to matchNullPointerException
- if expression or text is nullpublic static CharSequence stringOrSpannedString(CharSequence source)
public static boolean isEmpty(CharSequence str)
str
- the string to be examinedpublic static int getTrimmedLength(CharSequence s)
String.trim()
.public static boolean equals(CharSequence a, CharSequence b)
Note: In platform versions 1.1 and earlier, this method only worked well if both the arguments were instances of String.
a
- first CharSequence to checkb
- second CharSequence to check@Deprecated public static CharSequence getReverse(CharSequence source, int start, int end)
char
s and not their associated
spans. It doesn't support surrogate pairs (that correspond to non-BMP code points), combining
sequences or conjuncts either.public static void writeToParcel(CharSequence cs, Parcel p, int parcelableFlags)
public static void dumpSpans(CharSequence cs, Printer printer, String prefix)
public static CharSequence replace(CharSequence template, String[] sources, CharSequence[] destinations)
public static CharSequence expandTemplate(CharSequence template, CharSequence... values)
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'.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.IllegalArgumentException
- if the template requests a
value that was not provided, or if more than 9 values are
provided.public static int getOffsetBefore(CharSequence text, int offset)
public static int getOffsetAfter(CharSequence text, int offset)
public static void copySpansFrom(Spanned source, int start, int end, Class kind, Spannable dest, int destoff)
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
.IndexOutOfBoundsException
- if any of the copied spans
are out of range in dest
.public static CharSequence ellipsize(CharSequence text, TextPaint p, float avail, TextUtils.TruncateAt where)
public static CharSequence ellipsize(CharSequence text, TextPaint paint, float avail, TextUtils.TruncateAt where, boolean preserveLength, TextUtils.EllipsizeCallback callback)
preserveLength
is specified, the returned copy
will be padded with zero-width spaces to preserve the original
length and offsets instead of truncating.
If callback
is non-null, it will be called to
report the start and end of the ellipsized range. TextDirection
is determined by the first strong directional character.public static CharSequence ellipsize(CharSequence text, TextPaint paint, float avail, TextUtils.TruncateAt where, boolean preserveLength, TextUtils.EllipsizeCallback callback, TextDirectionHeuristic textDir, String ellipsis)
preserveLength
is specified, the returned copy
will be padded with zero-width spaces to preserve the original
length and offsets instead of truncating.
If callback
is non-null, it will be called to
report the start and end of the ellipsized range.public static CharSequence commaEllipsize(CharSequence text, TextPaint p, float avail, String oneMore, String more)
text
- the text to truncatep
- the Paint with which to measure the textavail
- the horizontal width available for the textoneMore
- the string for "1 more" in the current localemore
- the string for "%d more" in the current localepublic static CharSequence commaEllipsize(CharSequence text, TextPaint p, float avail, String oneMore, String more, TextDirectionHeuristic textDir)
public static String htmlEncode(String s)
s
- the string to be encodedpublic static CharSequence concat(CharSequence... text)
public static boolean isGraphic(CharSequence str)
@Deprecated public static boolean isGraphic(char c)
isGraphic(CharSequence)
instead.public static boolean isDigitsOnly(CharSequence str)
public static boolean isPrintableAscii(char c)
public static boolean isPrintableAsciiOnly(CharSequence str)
public static int getCapsMode(CharSequence cs, int off, int reqModes)
InputType
.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
.CAP_MODE_CHARACTERS
, CAP_MODE_WORDS
, and
CAP_MODE_SENTENCES
.public static boolean delimitedStringContains(String delimitedString, char delimiter, String item)
public static <T> T[] removeEmptySpans(T[] spans, Spanned spanned, Class<T> klass)
spans
array.
When parsing a Spanned using Spanned.nextSpanTransition(int, int, Class)
, empty spans
will (correctly) create span transitions, and calling getSpans on a slice of text bounded by
one of these transitions will (correctly) include the empty overlapping span.
However, these empty spans should not be taken into account when layouting or rendering the
string and this method provides a way to filter getSpans' results accordingly.spans
- A list of spans retrieved using Spanned.getSpans(int, int, Class)
from
the spanned
spanned
- The Spanned from which spans were extractedSpanned.getSpanStart(Object)
==
Spanned.getSpanEnd(Object)
have been removed. The initial order is preservedpublic static long packRangeInLong(int start, int end)
public static int unpackRangeStartFromLong(long range)
packRangeInLong(int, int)
public static int unpackRangeEndFromLong(long range)
packRangeInLong(int, int)
public static int getLayoutDirectionFromLocale(Locale locale)
locale
- the Locale for which we want the layout direction. Can be null.View.LAYOUT_DIRECTION_LTR
or
View.LAYOUT_DIRECTION_RTL
.
Be careful: this code will need to be updated when vertical scripts will be supportedpublic static CharSequence formatSelectedCount(int count)