下面这个是android sdk自带的文本工具,比如提供EditText对象的内容是否为空判断,截取字符串啊等等
对外提供的方法都是以静态方法的方式提供
/*
* Copyright (C) 2006 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.text;
import android.content.res.Resources;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.SystemProperties;
import android.provider.Settings;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.AlignmentSpan;
import android.text.style.BackgroundColorSpan;
import android.text.style.BulletSpan;
import android.text.style.CharacterStyle;
import android.text.style.EasyEditSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.LeadingMarginSpan;
import android.text.style.LocaleSpan;
import android.text.style.MetricAffectingSpan;
import android.text.style.QuoteSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.ReplacementSpan;
import android.text.style.ScaleXSpan;
import android.text.style.SpellCheckSpan;
import android.text.style.StrikethroughSpan;
import android.text.style.StyleSpan;
import android.text.style.SubscriptSpan;
import android.text.style.SuggestionRangeSpan;
import android.text.style.SuggestionSpan;
import android.text.style.SuperscriptSpan;
import android.text.style.TextAppearanceSpan;
import android.text.style.TtsSpan;
import android.text.style.TypefaceSpan;
import android.text.style.URLSpan;
import android.text.style.UnderlineSpan;
import android.util.Log;
import android.util.Printer;
import android.view.View;
import com.android.internal.R;
import com.android.internal.util.ArrayUtils;
import libcore.icu.ICU;
import java.lang.reflect.Array;
import java.util.Iterator;
import java.util.Locale;
import java.util.regex.Pattern;
public class TextUtils {
private static final String TAG = "TextUtils";
//构造函数私有,无法实例化
private TextUtils() { /* cannot be instantiated */ }
public static void getChars(CharSequence s, int start, int end,
char[] dest, int destoff) {
Class<? extends CharSequence> c = s.getClass();
if (c == String.class)
((String) s).getChars(start, end, dest, destoff);
else if (c == StringBuffer.class)
((StringBuffer) s).getChars(start, end, dest, destoff);
else if (c == StringBuilder.class)
((StringBuilder) s).getChars(start, end, dest, destoff);
else if (s instanceof GetChars)
((GetChars) s).getChars(start, end, dest, destoff);
else {
for (int i = start; i < end; i++)
dest[destoff++] = s.charAt(i);
}
}
public static int indexOf(CharSequence s, char ch) {
return indexOf(s, ch, 0);
}
public static int indexOf(CharSequence s, char ch, int start) {
Class<? extends CharSequence> c = s.getClass();
if (c == String.class)
return ((String) s).indexOf(ch, start);
return indexOf(s, ch, start, s.length());
}
public static int indexOf(CharSequence s, char ch, int start, int end) {
Class<? extends CharSequence> c = s.getClass();
if (s instanceof GetChars || c == StringBuffer.class ||
c == StringBuilder.class || c == String.class) {
final int INDEX_INCREMENT = 500;
char[] temp = obtain(INDEX_INCREMENT);
while (start < end) {
int segend = start + INDEX_INCREMENT;
if (segend > end)
segend = end;
getChars(s, start, segend, temp, 0);
int count = segend - start;
for (int i = 0; i < count; i++) {
if (temp[i] == ch) {
recycle(temp);
return i + start;
}
}
start = segend;
}
recycle(temp);
return -1;
}
for (int i = start; i < end; i++)
if (s.charAt(i) == ch)
return i;
return -1;
}
public static int lastIndexOf(CharSequence s, char ch) {
return lastIndexOf(s, ch, s.length() - 1);
}
public static int lastIndexOf(CharSequence s, char ch, int last) {
Class<? extends CharSequence> c = s.getClass();
if (c == String.class)
return ((String) s).lastIndexOf(ch, last);
return lastIndexOf(s, ch, 0, last);
}
public static int lastIndexOf(CharSequence s, char ch,
int start, int last) {
if (last < 0)
return -1;
if (last >= s.length())
last = s.length() - 1;
int end = last + 1;
Class<? extends CharSequence> c = s.getClass();
if (s instanceof GetChars || c == StringBuffer.class ||
c == StringBuilder.class || c == String.class) {
final int INDEX_INCREMENT = 500;
char[] temp = obtain(INDEX_INCREMENT);
while (start < end) {
int segstart = end - INDEX_INCREMENT;
if (segstart < start)
segstart = start;
getChars(s, segstart, end, temp, 0);
int count = end - segstart;
for (int i = count - 1; i >= 0; i--) {
if (temp[i] == ch) {
recycle(temp);
return i + segstart;
}
}
end = segstart;
}
recycle(temp);
return -1;
}
for (int i = end - 1; i >= start; i--)
if (s.charAt(i) == ch)
return i;
return -1;
}
public static int indexOf(CharSequence s, CharSequence needle) {
return indexOf(s, needle, 0, s.length());
}
public static int indexOf(CharSequence s, CharSequence needle, int start) {
return indexOf(s, needle, start, s.length());
}
public static int indexOf(CharSequence s, CharSequence needle,
int start, int end) {
int nlen = needle.length();
if (nlen == 0)
return start;
char c = needle.charAt(0);
for (;;) {
start = indexOf(s, c, start);
if (start > end - nlen) {
break;
}
if (start < 0) {
return -1;
}
if (regionMatches(s, start, needle, 0, nlen)) {
return start;
}
start++;
}
return -1;
}
public static boolean regionMatches(CharSequence one, int toffset,
CharSequence two, int ooffset,
int len) {
int tempLen = 2 * len;
if (tempLen < len) {
// Integer overflow; len is unreasonably large
throw new IndexOutOfBoundsException();
}
char[] temp = obtain(tempLen);
getChars(one, toffset, toffset + len, temp, 0);
getChars(two, ooffset, ooffset + len, temp, len);
boolean match = true;
for (int i = 0; i < len; i++) {
if (temp[i] != temp[i + len]) {
match = false;
break;
}
}
recycle(temp);
return match;
}
/**
* Create a new String object containing the given range of characters
* from the source string. This is different than simply calling
* {@link CharSequence#subSequence(int, int) CharSequence.subSequence}
* in that it does not preserve any style runs in the source sequence,
* allowing a more efficient implementation.
*/
// 截取原字符串中的一部分返回 。字符串截取,类似于截肢,呃好可怕,还是不如截木头吧
// 内部实现如果是String、StringBuffer、StringBuilder则分别调用他们的substring()方法,
// 否则将原字符串内的需要截取的部分放入一个char[]中,然后转化为String
// source:原字符串
// start:起始偏移量
// end:结束偏移量
public static String substring(CharSequence source, int start, int end) {
if (source instanceof String)
return ((String) source).substring(start, end);
if (source instanceof StringBuilder)
return ((StringBuilder) source).substring(start, end);
if (source instanceof StringBuffer)
return ((StringBuffer) source).substring(start, end);
char[] temp = obtain(end - start);
getChars(source, start, end, temp, 0);
String ret = new String(temp, 0, end - start);
recycle(temp);
return ret;
}
/**
* Returns list of multiple {@link CharSequence} joined into a single
* {@link CharSequence} separated by localized delimiter such as ", ".
*
* @hide
*/
public static CharSequence join(Iterable<CharSequence> list) {
final CharSequence delimiter = Resources.getSystem().getText(R.string.list_delimeter);
return join(delimiter, list);
}
/**
* Returns a string containing the tokens joined by delimiters.
* @param tokens an array objects to be joined. Strings will be formed from
* the objects by calling object.toString().
*/
public static String join(CharSequence delimiter, Object[] tokens) {
StringBuilder sb = new StringBuilder();
boolean firstTime = true;
for (Object token: tokens) {
if (firstTime) {
firstTime = false;
} else {
sb.append(delimiter);
}
sb.append(token);
}
return sb.toString();
}
/**
* Returns a string containing the tokens joined by delimiters.
* @param tokens an array objects to be joined. Strings will be formed from
* the objects by calling object.toString().
*/
public static String join(CharSequence delimiter, Iterable tokens) {
StringBuilder sb = new StringBuilder();
boolean firstTime = true;
for (Object token: tokens) {
if (firstTime) {
firstTime = false;
} else {
sb.append(delimiter);
}
sb.append(token);
}
return sb.toString();
}
/**
* String.split() returns [''] when the string to be split is empty. This returns []. This does
* not remove any empty strings from the result. For example split("a,", "," ) returns {"a", ""}.
*
* @param text the string to split
* @param expression the regular expression to match
* @return an array of strings. The array will be empty if text is empty
*
* @throws NullPointerException if expression or text is null
*/
// 将字符串以另一个字符串为匹配分拆成字符串数组,比如把abcde,fghi,jklmn,opqrs,tuvwxyz以,逗号来分拆字符串组成字符串数组
// text:原字符串
// expression:匹配的字符串、正则
public static String[] split(String text, String expression) {
if (text.length() == 0) {
return EMPTY_STRING_ARRAY;
} else {
return text.split(expression, -1);
}
}
/**
* Splits a string on a pattern. String.split() returns [''] when the string to be
* split is empty. This returns []. This does not remove any empty strings from the result.
* @param text the string to split
* @param pattern the regular expression to match
* @return an array of strings. The array will be empty if text is empty
*
* @throws NullPointerException if expression or text is null
*/
public static String[] split(String text, Pattern pattern) {
if (text.length() == 0) {
retu