TextUtils类常用方法介绍

作者: i小灰 | 来源:发表于2020-12-19 20:34 被阅读0次

简介:
TextUtils类是系统自带的一个工具类,里面包含了一些静态方法,是处理一些常见的有关Text的工具的集合方法类。

它的构造方法是私有的,不能通过 new来创建。它的方法都是 static 类型,可以直接调用。

CharSequence 类
这个类的实现类有我们常用的String、StringBuffer、StringBuilder等,因此在TextUtils类中Text的表示形式一般都是使用CharSequence。

    1. TextUtils判断字符串是否为空
 String str = "";
 TextUtils.isEmpty(str);

注意:str=”null“,不能判断为空。

源码

//判断字符串是否为空
   public static boolean isEmpty(@Nullable CharSequence str) {
        return str == null || str.length() == 0;
    }

    1. TextUtils判断字符串是否相等
 String str1 = "hfhsfhhh";
 String str2 = "hfhsfhhhww";
 TextUtils.equals(str1,str2);

注意:str1和str2如果都是 null 或者都是“”,返回的是 true。

源码:

 public static boolean equals(CharSequence a, CharSequence b) {
        if (a == b) return true;
        int length;
        if (a != null && b != null && (length = a.length()) == b.length()) {
            if (a instanceof String && b instanceof String) {
                return a.equals(b);
            } else {
                for (int i = 0; i < length; i++) {
                    if (a.charAt(i) != b.charAt(i)) return false;
                }
                return true;
            }
        }
        return false;
    }

    1. TextUtils判断字符串的截取
      substring(CharSequence source, int start, int end)
    1. TextUtils判断字符串是否只是数字
      // 判断是否仅有数字
      boolean isDigitsOnly(CharSequence str)

源码:

 /**
     * Returns whether the given CharSequence contains only digits.
     */
    public static boolean isDigitsOnly(CharSequence str) {
        final int len = str.length();
        for (int cp, i = 0; i < len; i += Character.charCount(cp)) {
            cp = Character.codePointAt(str, i);
            if (!Character.isDigit(cp)) {
                return false;
            }
        }
        return true;
    }

  • 5.其它不常见用法:

// 判断是否有可以显示出来的字符
boolean isGraphic(CharSequence str)

// 将字符串以另一个字符串为匹配分拆成字符串数组
// text:原字符串
// expression:匹配的字符串、正则
String[] split(String text, String expression)
String[] split(String text, Pattern pattern)

// 这是一个系列的方法
// 该方法用于获取当前char在指定的CharSequence的位置坐标,并可以设置区间和前后顺序
indexOf(CharSequence s, char ch)
indexOf(CharSequence s, char ch, int start)
indexOf(CharSequence s, char ch, int start, int end)
lastIndexOf(CharSequence s, char ch)
lastIndexOf(CharSequence s, char ch, int last)
lastIndexOf(CharSequence s, char ch, int start, int last)
indexOf(CharSequence s, CharSequence needle)
indexOf(CharSequence s, CharSequence needle, int start)
indexOf(CharSequence s, CharSequence needle, int start, int end)

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

拼接字符串 
String android.text.TextUtils.join(CharSequence delimiter, Object[] tokens)

使用HTML编码这个字符串String android.text.TextUtils.htmlEncode(String s)

相关文章

  • TextUtils类常用方法介绍

    简介:TextUtils类是系统自带的一个工具类,里面包含了一些静态方法,是处理一些常见的有关Text的工具的集合...

  • TextUtils类方法介绍

    使用TextUtils类对字符串进行简单的处理 (1)拼接字符串: TextUtils.conact(CharSe...

  • 你不可错过的android工具类

    1、android.text.TextUtils:字符串处理类 常用方法如下: 1、public static b...

  • Android TextUtils类介绍

    对于字符串处理Android为我们提供了一个简单实用的TextUtils类,如果处理比较简单的内容不用去思考正则表...

  • Matcher类

    Matcher类 常用方法及介绍 boolean matches() 最常用方法:尝试对整个目标字符展开匹配检测,...

  • 聚类常用方法介绍

    聚类步骤 1.数据清洗2.选择模型细节:避免loop(for循环),影响速度;密度聚类,不需要给出具体几类,k-m...

  • TextUtils类学习

    前言: TextUtils类是系统自带的一个工具类,里面包含了一些静态方法,是处理一些常见的有关Text的工具的集...

  • runtime常用方法

    类 类结构 类实例结构 常用函数 方法 结构 类方法的常用函数 方法的常用函数 方法选择器 动态创建类 示例: 动...

  • Java I/O三.InputStream常用方法

    本文主要介绍 InputStream常用方法罗列 InputStream类部分源码 InputStream 为字节...

  • Object类

    1、Object类中常用的方法,并简单介绍 toString()、clone()、hashCode()、equal...

网友评论

    本文标题:TextUtils类常用方法介绍

    本文链接:https://www.haomeiwen.com/subject/sjnoiktx.html