android.text.TextUtils不常用的方法筆記

TextUtils包含一些很有用的方法,除過一些經常用到的,還有一些不常用的記錄一下:

1.TextUtils.getChars

char[] chars = new char[5];
TextUtils.getChars("12345", 1, 3, chars, 2);
Look.e(Arrays.toString(chars));

結果:

[��, ��, 2, 3, ��]

此方法,是將”12345”中1->3位置的char(即’2’和’3’),拿出來放到chars[]中2位置開始的地方。

2.TextUtils.regionMatches

boolean b = TextUtils.regionMatches("abcd", 1, "obqs", 1, 2);
boolean b1 = TextUtils.regionMatches("abcd", 1, "opcs", 2, 1);
Look.e(b);
Look.e(b1);

結果:

false
true
regionMatches(CharSequence one, int toffset,
            CharSequence two, int ooffset,int len)

此方法,是one從toffset開始和two從ooffset開始,len位長度的字符是否匹配。

join

String join = TextUtils.join("123", new String[]{"3", "4", "5","6","7"});
Look.e(join);

結果:

//方便看一點,就是3|123|4|123|5|123|6|123|7
//就是在第二個參數(Object數組)中間插入第一個參數
31234123512361237

TextUtils.nullIfEmpty

字面理解,如果isEmpty是true,就返回null。

TextUtils.getTrimmedLength

字面理解,trim().length(),trim之後的長度

TextUtils.equals

和String的equals類似,唯一不同之處是,可以null和null做比較,並且是返回true

TextUtils.expandTemplate

CharSequence template= TextUtils.expandTemplate("你真^1是個^2好^3人", "a", "b", "c");
Look.e(template);

結果:

你真a是個b好c人

此方法,^n,就是將後邊參數第n(從1開始)個插入這裏

TextUtils.concat

拼接多個字符

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章