TextUtils
open class TextUtils
kotlin.Any | |
↳ | android.text.TextUtils |
Summary
Nested classes | |
---|---|
abstract | |
open |
A simple string splitter. |
abstract |
An interface for splitting strings according to rules that are opaque to the user of this interface. |
Constants | |
---|---|
static Int |
Capitalization mode for |
static Int |
Capitalization mode for |
static Int |
Capitalization mode for |
static Int |
Return only first line of text (truncate at first newline). |
static Int |
Force entire string into single line of text (no newlines). |
static Int |
Remove |
Public methods | |
---|---|
open static CharSequence! |
commaEllipsize(text: CharSequence!, p: TextPaint!, avail: Float, oneMore: String!, more: String!) 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". |
open static CharSequence! |
concat(vararg text: CharSequence!) Returns a CharSequence concatenating the specified CharSequences, retaining their spans if any. |
open static Unit |
copySpansFrom(source: Spanned!, start: Int, end: Int, kind: Class<Any!>!, dest: Spannable!, destoff: Int) Copies the spans from the region |
open static Unit |
dumpSpans(cs: CharSequence!, printer: Printer!, prefix: String!) Debugging tool to print the spans in a CharSequence. |
open static CharSequence! |
ellipsize(text: CharSequence!, p: TextPaint!, avail: Float, where: TextUtils.TruncateAt!) 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. |
open static CharSequence! |
ellipsize(text: CharSequence!, paint: TextPaint!, avail: Float, where: TextUtils.TruncateAt!, preserveLength: Boolean, callback: TextUtils.EllipsizeCallback?) 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. |
open static Boolean |
equals(a: CharSequence!, b: CharSequence!) Returns true if a and b are equal, including if they are both null. |
open static CharSequence! |
expandTemplate(template: CharSequence!, vararg values: CharSequence!) Replace instances of "^1", "^2", etc. |
open static Int |
getCapsMode(cs: CharSequence!, off: Int, reqModes: Int) Determine what caps mode should be in effect at the current offset in the text. |
open static Unit | |
open static Int |
getLayoutDirectionFromLocale(locale: Locale!) Return the layout direction for a given Locale |
open static Int |
getOffsetAfter(text: CharSequence!, offset: Int) |
open static Int |
getOffsetBefore(text: CharSequence!, offset: Int) |
open static CharSequence! |
getReverse(source: CharSequence!, start: Int, end: Int) This function only reverses individual |
open static Int |
Returns the length that the specified CharSequence would have if spaces and ASCII control characters were trimmed from the start and end, as by |
open static String! |
htmlEncode(s: String!) Html-encode the string. |
open static Int |
indexOf(s: CharSequence!, ch: Char) |
open static Int |
indexOf(s: CharSequence!, ch: Char, start: Int) |
open static Int |
indexOf(s: CharSequence!, ch: Char, start: Int, end: Int) |
open static Int |
indexOf(s: CharSequence!, needle: CharSequence!) |
open static Int |
indexOf(s: CharSequence!, needle: CharSequence!, start: Int) |
open static Int |
indexOf(s: CharSequence!, needle: CharSequence!, start: Int, end: Int) |
open static Boolean |
isDigitsOnly(str: CharSequence!) Returns whether the given CharSequence contains only digits. |
open static Boolean |
isEmpty(str: CharSequence?) Returns true if the string is null or 0-length. |
open static Boolean |
isGraphic(str: CharSequence!) Returns whether the given CharSequence contains any printable characters. |
open static Boolean |
Returns whether this character is a printable character. |
open static String! |
join(delimiter: CharSequence, tokens: Array<Any!>) Returns a string containing the tokens joined by delimiters. |
open static String! |
join(delimiter: CharSequence, tokens: MutableIterable<Any?>) Returns a string containing the tokens joined by delimiters. |
open static Int |
lastIndexOf(s: CharSequence!, ch: Char) |
open static Int |
lastIndexOf(s: CharSequence!, ch: Char, last: Int) |
open static Int |
lastIndexOf(s: CharSequence!, ch: Char, start: Int, last: Int) |
open static CharSequence! |
listEllipsize(context: Context?, elements: MutableList<CharSequence!>?, separator: String, paint: TextPaint, avail: Float, moreId: Int) Formats a list of CharSequences by repeatedly inserting the separator between them, but stopping when the resulting sequence is too wide for the specified width. |
open static CharSequence |
makeSafeForPresentation(unclean: String, maxCharactersToConsider: Int, ellipsizeDip: Float, flags: Int) Remove html, remove bad characters, and truncate string. |
open static Boolean |
regionMatches(one: CharSequence!, toffset: Int, two: CharSequence!, ooffset: Int, len: Int) |
open static CharSequence! |
replace(template: CharSequence!, sources: Array<String!>!, destinations: Array<CharSequence!>!) Return a new CharSequence in which each of the source strings is replaced by the corresponding element of the destinations. |
open static Array<String!>! |
This method yields the same result as |
open static Array<String!>! |
Splits a string on a pattern. |
open static CharSequence! |
stringOrSpannedString(source: CharSequence!) |
open static String! |
substring(source: CharSequence!, start: Int, end: Int) Create a new String object containing the given range of characters from the source string. |
open static Unit |
writeToParcel(cs: CharSequence?, p: Parcel, parcelableFlags: Int) Flatten a CharSequence and whatever styles can be copied across processes into the parcel. |
Properties | |
---|---|
static Parcelable.Creator<CharSequence!>! |
Constants
CAP_MODE_CHARACTERS
static val CAP_MODE_CHARACTERS: Int
Capitalization mode for getCapsMode
: capitalize all characters. This value is explicitly defined to be the same as InputType#TYPE_TEXT_FLAG_CAP_CHARACTERS
.
Value: 4096
CAP_MODE_SENTENCES
static val CAP_MODE_SENTENCES: Int
Capitalization mode for getCapsMode
: capitalize the first character of each sentence. This value is explicitly defined to be the same as InputType#TYPE_TEXT_FLAG_CAP_SENTENCES
.
Value: 16384
CAP_MODE_WORDS
static val CAP_MODE_WORDS: Int
Capitalization mode for getCapsMode
: capitalize the first character of all words. This value is explicitly defined to be the same as InputType#TYPE_TEXT_FLAG_CAP_WORDS
.
Value: 8192
SAFE_STRING_FLAG_FIRST_LINE
static val SAFE_STRING_FLAG_FIRST_LINE: Int
Return only first line of text (truncate at first newline). Cannot be set at the same time as SAFE_STRING_FLAG_SINGLE_LINE
.
Value: 4
SAFE_STRING_FLAG_SINGLE_LINE
static val SAFE_STRING_FLAG_SINGLE_LINE: Int
Force entire string into single line of text (no newlines). Cannot be set at the same time as SAFE_STRING_FLAG_FIRST_LINE
.
Value: 2
SAFE_STRING_FLAG_TRIM
static val SAFE_STRING_FLAG_TRIM: Int
Remove whitespace
and non-breaking spaces from the edges of the label.
Value: 1
Public methods
commaEllipsize
open static funcommaEllipsize(
text: CharSequence!,
p: TextPaint!,
avail: Float,
oneMore: String!,
more: String!
): CharSequence!
Deprecated: Do not use. This is not internationalized, and has known issues with right-to-left text, languages that have more than one plural form, languages that use a different character as a comma-like separator, etc. Use listEllipsize
instead.
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".
Parameters | |
---|---|
text |
CharSequence!: the text to truncate |
p |
TextPaint!: the Paint with which to measure the text |
avail |
Float: the horizontal width available for the text (in pixels) |
oneMore |
String!: the string for "1 more" in the current locale |
more |
String!: the string for "%d more" in the current locale |
concat
open static fun concat(vararg text: CharSequence!): CharSequence!
Returns a CharSequence concatenating the specified CharSequences, retaining their spans if any. If there are no parameters, an empty string will be returned. If the number of parameters is exactly one, that parameter is returned as output, even if it is null. If the number of parameters is at least two, any null CharSequence among the parameters is treated as if it was the string "null"
. 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.
copySpansFrom
open static fun copySpansFrom(
source: Spanned!,
start: Int,
end: Int,
kind: Class<Any!>!,
dest: Spannable!,
destoff: Int
): Unit
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
.
Exceptions | |
---|---|
java.lang.IndexOutOfBoundsException |
if any of the copied spans are out of range in dest . |
dumpSpans
open static fun dumpSpans(
cs: CharSequence!,
printer: Printer!,
prefix: String!
): Unit
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.
ellipsize
open static fun ellipsize(
text: CharSequence!,
p: TextPaint!,
avail: Float,
where: TextUtils.TruncateAt!
): CharSequence!
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
open static fun ellipsize(
text: CharSequence!,
paint: TextPaint!,
avail: Float,
where: TextUtils.TruncateAt!,
preserveLength: Boolean,
callback: TextUtils.EllipsizeCallback?
): CharSequence!
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. If 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.
Parameters | |
---|---|
callback |
TextUtils.EllipsizeCallback?: This value may be null . |
equals
open static fun equals(
a: CharSequence!,
b: CharSequence!
): Boolean
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 |
CharSequence!: first CharSequence to check |
b |
CharSequence!: second CharSequence to check |
Return | |
---|---|
Boolean |
true if a and b are equal |
expandTemplate
open static fun expandTemplate(
template: CharSequence!,
vararg values: CharSequence!
): CharSequence!
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 |
CharSequence!: the input text containing "^1"-style placeholder values. This object is not modified; a copy is returned. |
values |
CharSequence!: CharSequences substituted into the template. The first is substituted for "^1", the second for "^2", and so on. |
Return | |
---|---|
CharSequence! |
the new CharSequence produced by doing the replacement |
Exceptions | |
---|---|
java.lang.IllegalArgumentException |
if the template requests a value that was not provided, or if more than 9 values are provided. |
getCapsMode
open static fun getCapsMode(
cs: CharSequence!,
off: Int,
reqModes: Int
): Int
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 |
CharSequence!: The text that should be checked for caps modes. |
off |
Int: Location in the text at which to check. |
reqModes |
Int: The modes to be checked: may be any combination of CAP_MODE_CHARACTERS , CAP_MODE_WORDS , and CAP_MODE_SENTENCES . |
Return | |
---|---|
Int |
Returns the actual capitalization modes that can be in effect at the current position, which is any combination of CAP_MODE_CHARACTERS , CAP_MODE_WORDS , and CAP_MODE_SENTENCES . |
getChars
open static fun getChars(
s: CharSequence!,
start: Int,
end: Int,
dest: CharArray!,
destoff: Int
): Unit
getLayoutDirectionFromLocale
open static fun getLayoutDirectionFromLocale(locale: Locale!): Int
Return the layout direction for a given Locale
Parameters | |
---|---|
locale |
Locale!: the Locale for which we want the layout direction. Can be null. |
Return | |
---|---|
Int |
the layout direction. This may be one of: android.view.View#LAYOUT_DIRECTION_LTR or android.view.View#LAYOUT_DIRECTION_RTL . Be careful: this code will need to be updated when vertical scripts will be supported |
getOffsetAfter
open static fun getOffsetAfter(
text: CharSequence!,
offset: Int
): Int
getOffsetBefore
open static fun getOffsetBefore(
text: CharSequence!,
offset: Int
): Int
getReverse
open static fungetReverse(
source: CharSequence!,
start: Int,
end: Int
): CharSequence!
Deprecated: Do not use.
This function only reverses individual 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.
getTrimmedLength
open static fun getTrimmedLength(s: CharSequence!): Int
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
.
htmlEncode
open static fun htmlEncode(s: String!): String!
Html-encode the string.
Parameters | |
---|---|
s |
String!: the string to be encoded |
Return | |
---|---|
String! |
the encoded string |
indexOf
open static fun indexOf(
s: CharSequence!,
ch: Char,
start: Int,
end: Int
): Int
indexOf
open static fun indexOf(
s: CharSequence!,
needle: CharSequence!,
start: Int
): Int
indexOf
open static fun indexOf(
s: CharSequence!,
needle: CharSequence!,
start: Int,
end: Int
): Int
isDigitsOnly
open static fun isDigitsOnly(str: CharSequence!): Boolean
Returns whether the given CharSequence contains only digits.
isEmpty
open static fun isEmpty(str: CharSequence?): Boolean
Returns true if the string is null or 0-length.
Parameters | |
---|---|
str |
CharSequence?: the string to be examined This value may be null . |
Return | |
---|---|
Boolean |
true if str is null or zero length |
isGraphic
open static fun isGraphic(str: CharSequence!): Boolean
Returns whether the given CharSequence contains any printable characters.
isGraphic
open static funisGraphic(c: Char): Boolean
Deprecated: Use isGraphic(java.lang.CharSequence)
instead.
Returns whether this character is a printable character. This does not support non-BMP characters and should not be used.
join
open static fun join(
delimiter: CharSequence,
tokens: Array<Any!>
): String!
Returns a string containing the tokens joined by delimiters.
Parameters | |
---|---|
delimiter |
CharSequence: a CharSequence that will be inserted between the tokens. If null, the string "null" will be used as the delimiter. |
tokens |
Array<Any!>: an array objects to be joined. Strings will be formed from the objects by calling object.toString(). If tokens is null, a NullPointerException will be thrown. If tokens is an empty array, an empty string will be returned. |
join
open static fun join(
delimiter: CharSequence,
tokens: MutableIterable<Any?>
): String!
Returns a string containing the tokens joined by delimiters.
Parameters | |
---|---|
delimiter |
CharSequence: a CharSequence that will be inserted between the tokens. If null, the string "null" will be used as the delimiter. |
tokens |
MutableIterable<Any?>: an array objects to be joined. Strings will be formed from the objects by calling object.toString(). If tokens is null, a NullPointerException will be thrown. If tokens is empty, an empty string will be returned. |
lastIndexOf
open static fun lastIndexOf(
s: CharSequence!,
ch: Char,
last: Int
): Int
lastIndexOf
open static fun lastIndexOf(
s: CharSequence!,
ch: Char,
start: Int,
last: Int
): Int
listEllipsize
open static fun listEllipsize(
context: Context?,
elements: MutableList<CharSequence!>?,
separator: String,
paint: TextPaint,
avail: Float,
moreId: Int
): CharSequence!
Formats a list of CharSequences by repeatedly inserting the separator between them, but stopping when the resulting sequence is too wide for the specified width. This method actually tries to fit the maximum number of elements. So if "A, 11 more" fits
, "A, B, 10 more"
doesn't fit, but "A, B, C, 9 more"
fits again (due to the glyphs for the digits being very wide, for example), it returns "A, B, C, 9 more"
. Because of this, this method may be inefficient for very long lists. Note that the elements of the returned value, as well as the string for moreId
, will be bidi-wrapped using android.text.BidiFormatter#unicodeWrap based on the locale of the input Context. If the input Context
is null, the default BidiFormatter from BidiFormatter#getInstance()
will be used.
Parameters | |
---|---|
context |
Context?: the Context to get the moreId resource from. If null , an ellipsis (U+2026) would be used for moreId . |
elements |
MutableList<CharSequence!>?: the list to format This value may be null . |
separator |
String: a separator, such as ", " This value cannot be null . |
paint |
TextPaint: the Paint with which to measure the text This value cannot be null . |
avail |
Float: the horizontal width available for the text (in pixels) Value is 0.0 or greater |
moreId |
Int: the resource ID for the pluralized string to insert at the end of sequence when some of the elements don't fit. |
Return | |
---|---|
CharSequence! |
the formatted CharSequence. If even the shortest sequence (e.g. "A, 11 more" ) doesn't fit, it will return an empty string. |
makeSafeForPresentation
open static fun makeSafeForPresentation(
unclean: String,
maxCharactersToConsider: Int,
ellipsizeDip: Float,
flags: Int
): CharSequence
Remove html, remove bad characters, and truncate string.
This method is meant to remove common mistakes and nefarious formatting from strings that were loaded from untrusted sources (such as other packages).
This method first android.text.Html#fromHtml and then ...
- Removes new lines or truncates at first new line
- Trims the white-space off the end
- Truncates the string
Parameters | |
---|---|
unclean |
String: The input string This value cannot be null . |
maxCharactersToConsider |
Int: The maximum number of characters of unclean to consider from the input string. 0 disables this feature. Value is 0 or greater |
ellipsizeDip |
Float: Assuming maximum length of the string (in dip), assuming font size 42. This is roughly 50 characters for ellipsizeDip == 1000 .Usually ellipsizing should be left to the view showing the string. If a string is used as an input to another string, it might be useful to control the length of the input string though. 0 disables this feature. Value is 0 or greater |
flags |
Int: Flags controlling cleaning behavior (Can be SAFE_STRING_FLAG_TRIM , SAFE_STRING_FLAG_SINGLE_LINE , and SAFE_STRING_FLAG_FIRST_LINE ) Value is either 0 or a combination of android.text.TextUtils#SAFE_STRING_FLAG_TRIM , android.text.TextUtils#SAFE_STRING_FLAG_SINGLE_LINE , and android.text.TextUtils#SAFE_STRING_FLAG_FIRST_LINE |
Return | |
---|---|
CharSequence |
The cleaned string This value cannot be null . |
regionMatches
open static fun regionMatches(
one: CharSequence!,
toffset: Int,
two: CharSequence!,
ooffset: Int,
len: Int
): Boolean
replace
open static fun replace(
template: CharSequence!,
sources: Array<String!>!,
destinations: Array<CharSequence!>!
): CharSequence!
Return a new CharSequence in which each of the source strings is replaced by the corresponding element of the destinations.
split
open static fun split(
text: String!,
expression: String!
): Array<String!>!
This method yields the same result as text.split(expression, -1)
except that if text.isEmpty()
then this method returns an empty array whereas "".split(expression, -1)
would have returned an array with a single ""
. The -1
means that trailing empty Strings are not removed from the result; for example split("a,", "," ) returns {"a", ""}. Note that whether a leading zero-width match can result in a leading ""
depends on whether your app targets an SDK version
<= 28
; see Pattern#split(CharSequence, int)
.
Parameters | |
---|---|
text |
String!: the string to split |
expression |
String!: the regular expression to match |
Return | |
---|---|
Array<String!>! |
an array of strings. The array will be empty if text is empty |
Exceptions | |
---|---|
java.lang.NullPointerException |
if expression or text is null |
split
open static fun split(
text: String!,
pattern: Pattern!
): Array<String!>!
Splits a string on a pattern. This method yields the same result as pattern.split(text, -1)
except that if text.isEmpty()
then this method returns an empty array whereas pattern.split("", -1)
would have returned an array with a single ""
. The -1
means that trailing empty Strings are not removed from the result; Note that whether a leading zero-width match can result in a leading ""
depends on whether your app targets
<= 28
; see Pattern#split(CharSequence, int)
.
Parameters | |
---|---|
text |
String!: the string to split |
pattern |
Pattern!: the regular expression to match |
Return | |
---|---|
Array<String!>! |
an array of strings. The array will be empty if text is empty |
Exceptions | |
---|---|
java.lang.NullPointerException |
if expression or text is null |
stringOrSpannedString
open static fun stringOrSpannedString(source: CharSequence!): CharSequence!
substring
open static fun substring(
source: CharSequence!,
start: Int,
end: Int
): String!
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.
writeToParcel
open static fun writeToParcel(
cs: CharSequence?,
p: Parcel,
parcelableFlags: Int
): Unit
Flatten a CharSequence and whatever styles can be copied across processes into the parcel.
Parameters | |
---|---|
cs |
CharSequence?: This value may be null . |
p |
Parcel: This value cannot be null . |
Properties
CHAR_SEQUENCE_CREATOR
static val CHAR_SEQUENCE_CREATOR: Parcelable.Creator<CharSequence!>!