Class TextUtils
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
static enum
Where to truncate. -
Field Summary
Modifier and TypeFieldDescriptionstatic final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
-
Method Summary
Modifier and TypeMethodDescriptionstatic String
binaryCompact
(long num) static void
binaryCompact
(Appendable a, long num) static CharSequence
concat
(CharSequence... elements) Returns a CharSequence concatenating the specified CharSequences, retaining their spans if any.static CharSequence
concat
(Iterable<? extends CharSequence> elements) Returns a CharSequence concatenating the specified CharSequences, retaining their spans if any.static boolean
static void
copySpansFrom
(Spanned source, int start, int end, Class<?> type, Spannable dest, int destoff) Copies the spans from the regionstart...end
insource
to the regiondestoff...destoff+end-start
indest
.static CharSequence
static int
distance
(CharSequence a, CharSequence b) Find the Levenshtein distance between a and b.static void
drawTextRun
(Canvas canvas, char[] text, int start, int end, int contextStart, int contextEnd, float x, float y, boolean isRtl, TextPaint paint) Draw a run of text, all in a single direction, with optional context for complex text shaping.static void
drawTextRun
(Canvas canvas, CharSequence text, int start, int end, int contextStart, int contextEnd, float x, float y, boolean isRtl, TextPaint paint) Draw a run of text, all in a single direction, with optional context for complex text shaping.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 void
getChars
(CharSequence s, int srcBegin, int srcEnd, char[] dst, int dstBegin) Copies a block of characters efficiently.static char[]
getEllipsisChars
(TextUtils.TruncateAt method) static String
static int
getLayoutDirectionFromLocale
(Locale locale) Return the layout direction for a given Localestatic 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 boolean
isEmpty
(CharSequence csq) Returns true if the char sequence is null or 0-length.static CharSequence
join
(CharSequence delimiter, CharSequence... elements) Returns a CharSequence composed of copies of the elements joined together with the specified delimiter.static CharSequence
join
(CharSequence delimiter, Iterable<? extends CharSequence> elements) Returns a CharSequence composed of copies of the elements joined together with the specified delimiter.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 <T> List
<T> removeEmptySpans
(List<T> spans, Spanned spanned) Removes empty spans from thespans
list.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 String
validateSurrogatePairs
(String text) Replace all invalid surrogate pairs with 'U+FFFD' for the given UTF-16 string.static void
writeToParcel
(CharSequence cs, Parcel dest, int flags) Flatten aCharSequence
and whatever styles can be copied across processes into the output.
-
Field Details
-
FIRST_SPAN
@Internal public static final int FIRST_SPAN- See Also:
-
ALIGNMENT_SPAN
@Internal public static final int ALIGNMENT_SPAN- See Also:
-
FOREGROUND_COLOR_SPAN
@Internal public static final int FOREGROUND_COLOR_SPAN- See Also:
-
RELATIVE_SIZE_SPAN
@Internal public static final int RELATIVE_SIZE_SPAN- See Also:
-
SCALE_X_SPAN
@Internal public static final int SCALE_X_SPAN- See Also:
-
STRIKETHROUGH_SPAN
@Internal public static final int STRIKETHROUGH_SPAN- See Also:
-
UNDERLINE_SPAN
@Internal public static final int UNDERLINE_SPAN- See Also:
-
STYLE_SPAN
@Internal public static final int STYLE_SPAN- See Also:
-
BULLET_SPAN
@Internal public static final int BULLET_SPAN- See Also:
-
QUOTE_SPAN
@Internal public static final int QUOTE_SPAN- See Also:
-
LEADING_MARGIN_SPAN
@Internal public static final int LEADING_MARGIN_SPAN- See Also:
-
URL_SPAN
@Internal public static final int URL_SPAN- See Also:
-
BACKGROUND_COLOR_SPAN
@Internal public static final int BACKGROUND_COLOR_SPAN- See Also:
-
TYPEFACE_SPAN
@Internal public static final int TYPEFACE_SPAN- See Also:
-
SUPERSCRIPT_SPAN
@Internal public static final int SUPERSCRIPT_SPAN- See Also:
-
SUBSCRIPT_SPAN
@Internal public static final int SUBSCRIPT_SPAN- See Also:
-
ABSOLUTE_SIZE_SPAN
@Internal public static final int ABSOLUTE_SIZE_SPAN- See Also:
-
TEXT_APPEARANCE_SPAN
@Internal public static final int TEXT_APPEARANCE_SPAN- See Also:
-
ANNOTATION
@Internal public static final int ANNOTATION- See Also:
-
SUGGESTION_SPAN
@Internal public static final int SUGGESTION_SPAN- See Also:
-
SPELL_CHECK_SPAN
@Internal public static final int SPELL_CHECK_SPAN- See Also:
-
SUGGESTION_RANGE_SPAN
@Internal public static final int SUGGESTION_RANGE_SPAN- See Also:
-
EASY_EDIT_SPAN
@Internal public static final int EASY_EDIT_SPAN- See Also:
-
LOCALE_SPAN
@Internal public static final int LOCALE_SPAN- See Also:
-
TTS_SPAN
@Internal public static final int TTS_SPAN- See Also:
-
ACCESSIBILITY_CLICKABLE_SPAN
@Internal public static final int ACCESSIBILITY_CLICKABLE_SPAN- See Also:
-
ACCESSIBILITY_URL_SPAN
@Internal public static final int ACCESSIBILITY_URL_SPAN- See Also:
-
LINE_BACKGROUND_SPAN
@Internal public static final int LINE_BACKGROUND_SPAN- See Also:
-
LINE_HEIGHT_SPAN
@Internal public static final int LINE_HEIGHT_SPAN- See Also:
-
ACCESSIBILITY_REPLACEMENT_SPAN
@Internal public static final int ACCESSIBILITY_REPLACEMENT_SPAN- See Also:
-
TRAILING_MARGIN_SPAN
@Internal public static final int TRAILING_MARGIN_SPAN- See Also:
-
LAST_SPAN
@Internal public static final int LAST_SPAN- See Also:
-
-
Method Details
-
getEllipsisString
-
getEllipsisChars
-
stringOrSpannedString
-
isEmpty
Returns true if the char sequence is null or 0-length.- Parameters:
csq
- the char sequence to be examined- Returns:
- true if csq is null or zero length
-
contentEquals
-
getChars
public static void getChars(@NonNull CharSequence s, int srcBegin, int srcEnd, @NonNull char[] dst, int dstBegin) Copies a block of characters efficiently. -
removeEmptySpans
@NonNull public static <T> List<T> removeEmptySpans(@NonNull List<T> spans, @NonNull Spanned spanned) Removes empty spans from thespans
list.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 laying-out or rendering the string and this method provides a way to filter getSpans' results accordingly.
- Parameters:
spans
- A list of spans retrieved usingSpanned.getSpans(int, int, Class)
from thespanned
spanned
- The Spanned from which spans were extracted- Returns:
- A subset of spans where empty spans (
Spanned.getSpanStart(Object)
==Spanned.getSpanEnd(Object)
have been removed. The initial order is preserved
-
substring
Create a new String object containing the given range of characters from the source string. This is different than simply callingCharSequence.subSequence
in that it does not preserve any style runs in the source sequence, allowing a more efficient implementation. -
indexOf
-
indexOf
-
indexOf
-
lastIndexOf
-
lastIndexOf
-
lastIndexOf
-
writeToParcel
Flatten aCharSequence
and whatever styles can be copied across processes into the output. -
createFromParcel
-
drawTextRun
public static void drawTextRun(@NonNull Canvas canvas, @NonNull char[] text, int start, int end, int contextStart, int contextEnd, float x, float y, boolean isRtl, @NonNull TextPaint paint) Draw a run of text, all in a single direction, with optional context for complex text shaping.See
drawTextRun(Canvas, CharSequence, int, int, int, int, float, float, boolean, TextPaint)
for more details. This method uses a character array rather than CharSequence to represent the string.- Parameters:
canvas
- the canvastext
- the text to renderstart
- the start of the text to render. Data before this position can be used for shaping context.end
- the end of the text to render. Data at or after this position can be used for shaping context.contextStart
- the index of the start of the shaping contextcontextEnd
- the index of the end of the shaping contextx
- the x position at which to draw the texty
- the y position at which to draw the textisRtl
- whether the run is in RTL directionpaint
- the paint
-
drawTextRun
public static void drawTextRun(@NonNull Canvas canvas, @NonNull CharSequence text, int start, int end, int contextStart, int contextEnd, float x, float y, boolean isRtl, @NonNull TextPaint paint) Draw a run of text, all in a single direction, with optional context for complex text shaping.The run of text includes the characters from
start
toend
in the text. In addition, the rangecontextStart
tocontextEnd
is used as context for the purpose of complex text shaping, such as Arabic text potentially shaped differently based on the text next to it.All text outside the range
contextStart..contextEnd
is ignored. The text betweenstart
andend
will be laid out and drawn. The context range is useful for contextual shaping, e.g. Kerning, Arabic contextual form.The direction of the run is explicitly specified by
isRtl
. Thus, this method is suitable only for runs of a single direction. Alignment of the text is as determined by the Paint's TextAlign value. Further,0 <= contextStart <= start <= end <= contextEnd <= text.length
must hold on entry.- Parameters:
canvas
- the canvastext
- the text to renderstart
- the start of the text to render. Data before this position can be used for shaping context.end
- the end of the text to render. Data at or after this position can be used for shaping context.contextStart
- the index of the start of the shaping contextcontextEnd
- the index of the end of the shaping contextx
- the x position at which to draw the texty
- the y position at which to draw the textisRtl
- whether the run is in RTL directionpaint
- the paint- See Also:
-
ellipsize
@NonNull public static CharSequence ellipsize(@NonNull CharSequence text, @NonNull TextPaint p, float avail, @NonNull 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. -
ellipsize
@NonNull public static CharSequence ellipsize(@NonNull CharSequence text, @NonNull TextPaint paint, float avail, @NonNull TextUtils.TruncateAt where, boolean preserveLength, @Nullable 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. IfpreserveLength
is specified, the returned copy will be padded with zero-width spaces to preserve the original length and offsets instead of truncating. Ifcallback
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. -
concat
Returns a CharSequence concatenating the specified CharSequences, retaining their spans if any.If there are no elements, an empty string will be returned.
If the number of elements is exactly one, that element is returned, even if it is null or mutable.
If the number of elements is at least two, any null CharSequence among the elements is treated as if it was the string
"null"
, and a new String or SpannedString is returned.If there are paragraph spans in the source CharSequences that satisfy paragraph boundary requirements in the sources but would no longer satisfy them in the concatenated CharSequence, they may get extended in the resulting CharSequence or not retained.
- Since:
- 3.10.1
-
concat
Returns a CharSequence concatenating the specified CharSequences, retaining their spans if any.If there are no elements, an empty string will be returned.
If the number of elements is exactly one, that element is returned, even if it is null or mutable.
If the number of elements is at least two, any null CharSequence among the elements is treated as if it was the string
"null"
, and a new String or SpannedString is returned.If there are paragraph spans in the source CharSequences that satisfy paragraph boundary requirements in the sources but would no longer satisfy them in the concatenated CharSequence, they may get extended in the resulting CharSequence or not retained.
- Since:
- 3.10.1
-
join
@NonNull public static CharSequence join(@NonNull CharSequence delimiter, @NonNull CharSequence... elements) Returns a CharSequence composed of copies of the elements joined together with the specified delimiter.If there are no elements, an empty string will be returned. Otherwise, returns a new CharSequence. Any null value will be replaced with the string
"null"
.Unlike Android, this method retains their spans if any. If you want to ignore all the spans, use
String.join(CharSequence, CharSequence...)
instead.If there are paragraph spans in the source CharSequences that satisfy paragraph boundary requirements in the sources but would no longer satisfy them in the concatenated CharSequence, they may get extended in the resulting CharSequence or not retained.
-
join
@NonNull public static CharSequence join(@NonNull CharSequence delimiter, @NonNull Iterable<? extends CharSequence> elements) Returns a CharSequence composed of copies of the elements joined together with the specified delimiter.If there are no elements, an empty string will be returned. Otherwise, returns a new CharSequence. Any null value will be replaced with the string
"null"
.Unlike Android, this method retains their spans if any. If you want to ignore all the spans, use
String.join(CharSequence, Iterable)
instead.If there are paragraph spans in the source CharSequences that satisfy paragraph boundary requirements in the sources but would no longer satisfy them in the concatenated CharSequence, they may get extended in the resulting CharSequence or not retained.
-
binaryCompact
-
binaryCompact
-
copySpansFrom
public static void copySpansFrom(@NonNull Spanned source, int start, int end, @Nullable Class<?> type, @NonNull Spannable dest, int destoff) Copies the spans from the regionstart...end
insource
to the regiondestoff...destoff+end-start
indest
. Spans insource
that begin beforestart
or end afterend
but overlap this range are trimmed as if they began atstart
or ended atend
.- Throws:
IndexOutOfBoundsException
- if any of the copied spans are out of range indest
.
-
getLayoutDirectionFromLocale
Return the layout direction for a given Locale- Parameters:
locale
- the Locale for which we want the layout direction. Can be null.- Returns:
- the layout direction. This may be one of:
View.LAYOUT_DIRECTION_LTR
orView.LAYOUT_DIRECTION_RTL
.Be careful: this code will need to be updated when vertical scripts will be supported
-
validateSurrogatePairs
Replace all invalid surrogate pairs with 'U+FFFD' for the given UTF-16 string. Return the given string as-is if it was validated, or a new string. -
distance
Find the Levenshtein distance between a and b. This algorithm has a time complexity of O(m*n) and a space complexity of O(n), where m is the length of a and n is the length of b.This method only works for Unicode BMP characters without taking into account grapheme clusters.
- Returns:
- the Levenshtein distance in chars (u16)
- Since:
- 3.7
-