![](http://static.blog.csdn.net/images/category_icon.jpg)
版权声明:本文为博主原创文章,未经博主允许不得转载。
之前经常使用TextUtils.isEmpty(),用来判断字符串是否为空,也误认为空格也能返回true,造成代码错误。其实看看源码就知道
如果传入是空格,字符串的长度不会为0,因此返回时false。为了判断EditText输入的是否为空字符串,可以将字符串先trim(),再传入isEmpty,就能成功判断了。
同时发现其中也包含其它好用的函数:
/** * Returns the length that the specified CharSequence would have if * spaces and control characters were trimmed from the start and end, * as by {@link String#trim}. */ public static int getTrimmedLength(CharSequence s)
/** * Returns a CharSequence concatenating the specified CharSequences, * retaining their spans if any. */ public static CharSequence concat(CharSequence... text)
/** * Returns whether the given CharSequence contains only digits. */ public static boolean isDigitsOnly(CharSequence str)
/** * Html-encode the string. * @param s the string to be encoded * @return the encoded string */ public static String htmlEncode(String s)
/** * Converts a CharSequence of the comma-separated form "Andy, Bob, * Charles, David" that is too wide to fit into the specified width * into one like "Andy, Bob, 2 more". * * @param text the text to truncate * @param p the Paint with which to measure the text * @param avail the horizontal width available for the text * @param oneMore the string for "1 more" in the current locale * @param more the string for "%d more" in the current locale */ public static CharSequence commaEllipsize(CharSequence text,TextPaint p, float avail, String oneMore, String more)
/** * Returns the original text if it fits in the specified width * given the properties of the specified Paint, * or, if it does not fit, a truncated * copy with ellipsis character added at the specified edge or center. */ public static CharSequence ellipsize(CharSequence text, TextPaint p, float avail, TruncateAt where)
/** * Returns true if a and b are equal, including if they are both null. * <p><i>Note: In platform versions 1.1 and earlier, this method only worked well if * both the arguments were instances of String.</i></p> * @param a first CharSequence to check * @param b second CharSequence to check * @return true if a and b are equal */ public static boolean equals(CharSequence a, CharSequence b)
/** * 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)
/** * 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 */ public static String[] split(String text, String expression)
/** * 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)
/** * 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)
/** * 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. */ public static String substring(CharSequence source, int start, int end)
可以看出参数类型大多采用CharSequence,这是一个接口,很多Java类都实现了此接口,比如:String,StringBuffer、SpannedString,并提供以CharSequence为入参的构造函数或者转换方式。