Most visited

Recently visited

Added in API level 1

TextUtils

public class TextUtils
extends Object

java.lang.Object
   ↳ android.text.TextUtils


Summary

Nested classes

interface TextUtils.EllipsizeCallback

 

class TextUtils.SimpleStringSplitter

一个简单的字符串拆分器。

interface TextUtils.StringSplitter

根据对此接口的用户不透明的规则分割字符串的接口。

枚举 TextUtils.TruncateAt

 

Constants

int CAP_MODE_CHARACTERS

大写模式为 getCapsMode(CharSequence, int, int) :大写所有字符。

int CAP_MODE_SENTENCES

大写模式 getCapsMode(CharSequence, int, int) :大写每个句子的第一个字符。

int CAP_MODE_WORDS

大写模式为 getCapsMode(CharSequence, int, int) :将所有单词的首字母大写。

Fields

public static final Creator<CharSequence> CHAR_SEQUENCE_CREATOR

Public methods

static CharSequence commaEllipsize(CharSequence text, TextPaint p, float avail, String oneMore, String more)

将以逗号分隔的格式“Andy,Bob,Charles,David”的CharSequence转换为宽度不足以符合指定宽度的CharSequence,如“Andy,Bob,2 more”。

static CharSequence concat(CharSequence... text)

返回一个CharSequence连接指定的CharSequence,如果有的话保留它们的跨度。

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

复制从区域跨度 start...endsource的区域 destoff...destoff+end-startdest

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

调试工具在CharSequence中打印跨度。

static CharSequence ellipsize(CharSequence text, TextPaint p, float avail, TextUtils.TruncateAt where)

返回原始文本,如果指定的Paint的属性符合指定的宽度,或者如果不适合,则在指定的边或中心处添加一个带有省略号字符的截断副本。

static CharSequence ellipsize(CharSequence text, TextPaint paint, float avail, TextUtils.TruncateAt where, boolean preserveLength, TextUtils.EllipsizeCallback callback)

如果给定指定Paint的属性,则返回原始文本;如果不匹配,则返回在指定边或中心处添加省略号字符的副本。

static boolean equals(CharSequence a, CharSequence b)

如果a和b相等,则返回true,包括它们都为null。

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

替换“^ 1”,“^ 2”等的实例

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

确定文本中当前偏移量应该生效的上限模式。

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

返回给定语言环境的布局方向

static int getOffsetAfter(CharSequence text, int offset)
static int getOffsetBefore(CharSequence text, int offset)
static CharSequence getReverse(CharSequence source, int start, int end)

此方法在API级别24中已弃用。请勿使用。

static int getTrimmedLength(CharSequence s)

如果空格和ASCII控制字符从开始和结束被修剪,返回指定CharSequence的长度,如 trim()

static String htmlEncode(String s)

Html编码字符串。

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)

返回给定的CharSequence是否只包含数字。

static boolean isEmpty(CharSequence str)

如果字符串为null或0长度,则返回true。

static boolean isGraphic(CharSequence str)

返回给定的CharSequence是否包含任何可打印的字符。

static boolean isGraphic(char c)

此方法在API级别24中已弃用。请改为使用isGraphic(CharSequence)

static String join(CharSequence delimiter, Iterable tokens)

返回包含由分隔符连接的令牌的字符串。

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

返回包含由分隔符连接的令牌的字符串。

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 CharSequence replace(CharSequence template, String[] sources, CharSequence[] destinations)

返回一个新的CharSequence,其中每个源字符串都由目标的相应元素替换。

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

在模式上分割一个字符串。

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

当要拆分的字符串为空时,String.split()返回['']。

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

创建一个新的String对象,其中包含来自源字符串的给定范围的字符。

static void writeToParcel(CharSequence cs, Parcel p, int parcelableFlags)

拼合一个CharSequence,可以将任何样式跨进程复制到宗地中。

Inherited methods

From class java.lang.Object

Constants

CAP_MODE_CHARACTERS

Added in API level 3
int CAP_MODE_CHARACTERS

大写模式为getCapsMode(CharSequence, int, int) :大写所有字符。 该值明确定义为与TYPE_TEXT_FLAG_CAP_CHARACTERS相同。

常量值:4096(0x00001000)

CAP_MODE_SENTENCES

Added in API level 3
int CAP_MODE_SENTENCES

大写模式为getCapsMode(CharSequence, int, int) :大写每个句子的第一个字符。 该值明确定义为与TYPE_TEXT_FLAG_CAP_SENTENCES相同。

常量值:16384(0x00004000)

CAP_MODE_WORDS

Added in API level 3
int CAP_MODE_WORDS

大写模式为getCapsMode(CharSequence, int, int) :将所有单词的首字母大写。 该值明确定义为与TYPE_TEXT_FLAG_CAP_WORDS相同。

常量值:8192(0x00002000)

Fields

CHAR_SEQUENCE_CREATOR

Added in API level 1
Creator<CharSequence> CHAR_SEQUENCE_CREATOR

Public methods

commaEllipsize

Added in API level 1
CharSequence commaEllipsize (CharSequence text, 
                TextPaint p, 
                float avail, 
                String oneMore, 
                String more)

将以逗号分隔的格式“Andy,Bob,Charles,David”的CharSequence转换为宽度不足以符合指定宽度的CharSequence,如“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
oneMore String: the string for "1 more" in the current locale
more String: the string for "%d more" in the current locale
Returns
CharSequence

concat

Added in API level 1
CharSequence concat (CharSequence... text)

返回一个CharSequence连接指定的CharSequence,如果有的话保留它们的跨度。

Parameters
text CharSequence
Returns
CharSequence

copySpansFrom

Added in API level 1
void copySpansFrom (Spanned source, 
                int start, 
                int end, 
                Class kind, 
                Spannable dest, 
                int destoff)

复制从区域跨度start...endsource的区域destoff...destoff+end-startdest 跨越在source之前开始start或之后结束end但重叠的这个范围,就好像他们开始在修剪start或结束end

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

dumpSpans

Added in API level 3
void dumpSpans (CharSequence cs, 
                Printer printer, 
                String prefix)

调试工具在CharSequence中打印跨度。 输出将打印每行一个跨度。 如果CharSequence不是一个Spanned,那么整个字符串将被打印在一行上。

Parameters
cs CharSequence
printer Printer
prefix String

ellipsize

Added in API level 1
CharSequence ellipsize (CharSequence text, 
                TextPaint p, 
                float avail, 
                TextUtils.TruncateAt where)

返回原始文本,如果指定的Paint的属性符合指定的宽度,或者如果不适合,则在指定的边或中心处添加一个带有省略号字符的截断副本。

Parameters
text CharSequence
p TextPaint
avail float
where TextUtils.TruncateAt
Returns
CharSequence

ellipsize

Added in API level 1
CharSequence ellipsize (CharSequence text, 
                TextPaint paint, 
                float avail, 
                TextUtils.TruncateAt where, 
                boolean preserveLength, 
                TextUtils.EllipsizeCallback callback)

如果给定指定Paint的属性,则返回原始文本;如果不匹配,则返回在指定边或中心处添加省略号字符的副本。 如果指定了preserveLength ,则返回的副本将填充零宽度空格以保留原始长度和偏移量而不是截断。 如果callback非空,它将被调用来报告椭圆化范围的开始和结束。 TextDirection由第一个强方向字符决定。

Parameters
text CharSequence
paint TextPaint
avail float
where TextUtils.TruncateAt
preserveLength boolean
callback TextUtils.EllipsizeCallback
Returns
CharSequence

equals

Added in API level 1
boolean equals (CharSequence a, 
                CharSequence b)

如果a和b相等,则返回true,包括它们都为null。

注意:在平台版本1.1和更低版本中,如果两个参数都是String实例,则此方法只能运行良好。

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

expandTemplate

Added in API level 1
CharSequence expandTemplate (CharSequence template, 
                CharSequence... values)

template CharSequence中的“^ 1”,“^ 2”等实例替换为相应的values “^^”用于在输出中生成一个脱字符号。 只支持最多9个替换值,“^ 10”将产生第一个替换值,后面是“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.
Returns
CharSequence 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.

getCapsMode

Added in API level 3
int getCapsMode (CharSequence cs, 
                int off, 
                int reqModes)

确定文本中当前偏移量应该生效的上限模式。 只有在reqModes中设置的模式位才会被检查。 请注意,这里的cap模式标志明确定义为与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.
Returns
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

Added in API level 1
void getChars (CharSequence s, 
                int start, 
                int end, 
                char[] dest, 
                int destoff)

Parameters
s CharSequence
start int
end int
dest char
destoff int

getLayoutDirectionFromLocale

Added in API level 17
int getLayoutDirectionFromLocale (Locale locale)

返回给定语言环境的布局方向

Parameters
locale Locale: the Locale for which we want the layout direction. Can be null.
Returns
int the layout direction. This may be one of: LAYOUT_DIRECTION_LTR or LAYOUT_DIRECTION_RTL. Be careful: this code will need to be updated when vertical scripts will be supported

getOffsetAfter

Added in API level 1
int getOffsetAfter (CharSequence text, 
                int offset)

Parameters
text CharSequence
offset int
Returns
int

getOffsetBefore

Added in API level 1
int getOffsetBefore (CharSequence text, 
                int offset)

Parameters
text CharSequence
offset int
Returns
int

getReverse

Added in API level 1
CharSequence getReverse (CharSequence source, 
                int start, 
                int end)

此方法在API级别24中已弃用。
不使用。

此功能仅反转单个char ,而不反转它们的相关跨度。 它不支持代理对(对应于非BMP代码点),组合序列或连接。

Parameters
source CharSequence
start int
end int
Returns
CharSequence

getTrimmedLength

Added in API level 1
int getTrimmedLength (CharSequence s)

如果空格和ASCII控制字符从开始和结束被修剪,返回指定CharSequence的长度,如 trim()

Parameters
s CharSequence
Returns
int

htmlEncode

Added in API level 1
String htmlEncode (String s)

Html编码字符串。

Parameters
s String: the string to be encoded
Returns
String the encoded string

indexOf

Added in API level 1
int indexOf (CharSequence s, 
                char ch, 
                int start, 
                int end)

Parameters
s CharSequence
ch char
start int
end int
Returns
int

indexOf

Added in API level 1
int indexOf (CharSequence s, 
                CharSequence needle, 
                int start, 
                int end)

Parameters
s CharSequence
needle CharSequence
start int
end int
Returns
int

indexOf

Added in API level 1
int indexOf (CharSequence s, 
                CharSequence needle, 
                int start)

Parameters
s CharSequence
needle CharSequence
start int
Returns
int

indexOf

Added in API level 1
int indexOf (CharSequence s, 
                CharSequence needle)

Parameters
s CharSequence
needle CharSequence
Returns
int

indexOf

Added in API level 1
int indexOf (CharSequence s, 
                char ch, 
                int start)

Parameters
s CharSequence
ch char
start int
Returns
int

indexOf

Added in API level 1
int indexOf (CharSequence s, 
                char ch)

Parameters
s CharSequence
ch char
Returns
int

isDigitsOnly

Added in API level 1
boolean isDigitsOnly (CharSequence str)

返回给定的CharSequence是否只包含数字。

Parameters
str CharSequence
Returns
boolean

isEmpty

Added in API level 1
boolean isEmpty (CharSequence str)

如果字符串为null或0长度,则返回true。

Parameters
str CharSequence: the string to be examined
Returns
boolean true if str is null or zero length

isGraphic

Added in API level 1
boolean isGraphic (CharSequence str)

返回给定的CharSequence是否包含任何可打印的字符。

Parameters
str CharSequence
Returns
boolean

isGraphic

Added in API level 1
boolean isGraphic (char c)

此方法在API级别24中已弃用。
改为使用isGraphic(CharSequence)

返回此字符是否为可打印字符。 这不支持非BMP字符,不应使用。

Parameters
c char
Returns
boolean

join

Added in API level 1
String join (CharSequence delimiter, 
                Iterable tokens)

返回包含由分隔符连接的令牌的字符串。

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

join

Added in API level 1
String join (CharSequence delimiter, 
                Object[] tokens)

返回包含由分隔符连接的令牌的字符串。

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

lastIndexOf

Added in API level 1
int lastIndexOf (CharSequence s, 
                char ch, 
                int last)

Parameters
s CharSequence
ch char
last int
Returns
int

lastIndexOf

Added in API level 1
int lastIndexOf (CharSequence s, 
                char ch)

Parameters
s CharSequence
ch char
Returns
int

lastIndexOf

Added in API level 1
int lastIndexOf (CharSequence s, 
                char ch, 
                int start, 
                int last)

Parameters
s CharSequence
ch char
start int
last int
Returns
int

regionMatches

Added in API level 1
boolean regionMatches (CharSequence one, 
                int toffset, 
                CharSequence two, 
                int ooffset, 
                int len)

Parameters
one CharSequence
toffset int
two CharSequence
ooffset int
len int
Returns
boolean

replace

Added in API level 1
CharSequence replace (CharSequence template, 
                String[] sources, 
                CharSequence[] destinations)

返回一个新的CharSequence,其中每个源字符串都由目标的相应元素替换。

Parameters
template CharSequence
sources String
destinations CharSequence
Returns
CharSequence

split

Added in API level 1
String[] split (String text, 
                Pattern pattern)

在模式上分割一个字符串。 当要拆分的字符串为空时,String.split()返回['']。 这返回[]。 这不会从结果中删除任何空字符串。

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

split

Added in API level 1
String[] split (String text, 
                String expression)

当要拆分的字符串为空时,String.split()返回['']。 这返回[]。 这不会从结果中删除任何空字符串。 例如split(“a”,“,”)返回{“a”,“”}。

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

stringOrSpannedString

Added in API level 1
CharSequence stringOrSpannedString (CharSequence source)

Parameters
source CharSequence
Returns
CharSequence

substring

Added in API level 1
String substring (CharSequence source, 
                int start, 
                int end)

创建一个新的String对象,其中包含来自源字符串的给定范围的字符。 这与简单地调用CharSequence.subSequence不同,因为它不保留源序列中的任何样式运行,从而实现更高效的实现。

Parameters
source CharSequence
start int
end int
Returns
String

writeToParcel

Added in API level 1
void writeToParcel (CharSequence cs, 
                Parcel p, 
                int parcelableFlags)

拼合一个CharSequence,可以将任何样式跨进程复制到宗地中。

Parameters
cs CharSequence
p Parcel
parcelableFlags int

Hooray!