TextUtils

本文详细介绍了 Android 中 TextUtils 类的多个实用方法,包括字符串连接、反转、匹配等功能,并提供了具体的使用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://developer.android.com/reference/android/text/TextUtils.html


之前没有关注过 TextUtils 这个类,主要用到的就是很简单的方法, 比如 empty   equals 简单的方法

后面有时间了会对一些特殊的方法,源码解读


1.TextUtils.join("|" ,List<String>)   -----   string x =  " a | b | c "

  Spanny  mSpanny1 = new Spanny(mWords1).findAndSpan("关键的", new Spanny.GetSpan() {
            @Override
            public Object getSpan() {
                return new BackgroundColorSpan(Color.RED);
            }
        });



3. commaEllipsize

TextUtils.commaEllipsize("a b c d e f g h i g", mTextView.getPaint(), mScreenWidth, "1 more", "%d more");
"a b c d 1 more"



4.concat
TextUtils.concat(mSpanny1, mSpanny2);



5.copySpansFrom
TextUtils.copySpansFrom(mSpanny1 , start , end , Object.class, ss,  destoff);


6.dumpSpans
TextUtils.dumpSpans(mSpanny1, new LogPrinter(Log.INFO, "kenneth"), "yo");
打印cs中的Span,prefix会在打印每个Span开始出现。printer,可以用LogPrinter,这样就可以打印在 log 里。


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

相当于 TextView 的 xml 中ellipsize,这里可以回调省略范围的 index,同时也可用通过preserveLength,设置返回的 CharSequence 的长度为原始长度还是省略后的长度,这里利用的”零宽不换行空格符”来占位,where参数可以用TextUtils.TruncateAt.START,TextUtils.TruncateAt.END,TextUtils.TruncateAt.MIDDLE和TextUtils.TruncateAt.MARQUEE,分别是开始,结尾,中间和跑马灯。

CharSequence ellipsize = TextUtils.ellipsize(mWords2, mTextView.getPaint(), mScreenWidth, TextUtils.TruncateAt.START, true, new TextUtils.EllipsizeCallback() {
                    @Override
                    public void ellipsized(int i, int i1) {
                        Log.i(TAG, "被省略范围 " + i + " " + i1);
                    }
                });


8.expandTemplate

public static CharSequence expandTemplate(CharSequence template, CharSequence... values) 
替换template当中的^1`` ^2 等为values中相应的值,注意:不能超过9个,9个以上会抛出异常。

String template = "This is a ^1 of the ^2 broadcast ^3.";
                mTextView.setText(template);
                CharSequence expandTemplate = TextUtils.expandTemplate(template, "test", "emergency", "system");



9.getCapsMode

public static int getCapsMode(CharSequence cs, int off, int reqModes)
这个方法不知道怎么用,求留言赐教。


10. getChars

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

char[] buffer = new char[5];

TextUtils.getChars(mWords1, 2, 4, buffer, 0);


11.
getLayoutDirectionFromLocale

public static int getLayoutDirectionFromLocale(Locale locale)

根据传入的 local获取当前的阅读习惯(例如,汉语习惯是左到右,希伯来语是右到左),
具体可以看这篇文章,http://droidyue.com/blog/2014/07/07/support-rtl-in-android/index.html



12. getReverse  翻转start到end的字符串。

public static CharSequence getReverse(CharSequence source, int start, int end)



13.getTrimmedLength

public static int getTrimmedLength(CharSequence s)
trim 后的字符串长度。


14. htmlEncode

public static String htmlEncode(String s)
encode html ,注意是 html 。


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


16.

isDigitsOnly

public static boolean isDigitsOnly(CharSequence str)

判断是否仅有数字。

17.lastIndexOf

public static int lastIndexOf(CharSequence s, char ch)


18. regionMatches  比对两个字符串 offset到 len 的长度的字符串是否相同
public static boolean regionMatches(CharSequence one, int toffset,
                                        CharSequence two, int ooffset,
                                        int len) {


19.replace

public static CharSequence replace(CharSequence template,
                                       String[] sources,
                                       CharSequence[] destinations)
替换功能,注意:只会替换第一个出现的。

例子:

mOutputView.setText(TextUtils.replace(mWords1,new String[]{"很长","的","."},new String[]{"very long","of","。"}));



20. split

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

分割字符串。
例子:

mOutputView.setText(TextUtils.join("--",TextUtils.split(mWords2,",")));






<think>好的,用户想查找TextUtils的Maven包并在项目中添加依赖。首先,我需要确认用户提到的TextUtils具体是哪个库,因为可能有多个同名的库。接下来,应该检查常见的仓库如Maven Central或JitPack是否有这个库。 根据引用[3],用户对Maven有一定的了解,但可能不太熟悉具体操作步骤。需要指导用户如何在pom.xml中添加依赖,类似于引用[2]中的示例,那里展示了添加Spring Boot依赖的方式。用户可能需要类似的格式,但需要正确的groupId、artifactId和版本号。 另外,引用[4]提到了Gradle配置,但用户明确询问的是Maven,所以应专注于Maven的配置方法。如果TextUtils不在Maven Central,可能需要添加其他仓库,但通常建议优先使用主流仓库。 需要提醒用户确认正确的依赖信息,避免拼写错误,并建议通过Maven仓库网站搜索确认。如果找不到,可能需要考虑是否有其他名称或自行实现功能。最后,确保用户理解如何验证依赖是否成功添加,比如构建项目并检查是否出现错误。</think>要在Maven项目中添加TextUtils依赖,请按以下步骤操作: 1. **确认依赖信息**: - 访问[Maven中央仓库](https://mvnrepository.com/) - 搜索"TextUtils"找到正确的坐标信息 - 常见组合可能是: $$ groupId:org.apache.commons \\ artifactId:commons-text \\ version:1.9 $$ 2. **修改pom.xml**: 参考引用[2]中的依赖配置模式,在`<dependencies>`节点内添加: ```xml <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-text</artifactId> <version>1.9</version> </dependency> ``` 3. **验证依赖**: 执行`mvn clean install`后,检查是否出现依赖下载日志: ``` Downloading from central: https://repo.maven.apache.org/maven2/org/apache/commons/commons-text/1.9/commons-text-1.9.jar ``` 4. **注意事项**: - 如果使用自定义仓库(如引用[4]中的私有仓库),需在`<repositories>`节点添加仓库配置 - 实际坐标可能因具体实现库不同而变化,建议通过仓库搜索确认最新版本[^3]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李磊LL

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值