Android TextUtils.isEmpty()的误解

之前经常使用TextUtils.isEmpty(),用来判断字符串是否为空,也误认为空格也能返回true,造成代码错误尴尬。其实看看源码就知道

    /**
     * Returns true if the string is null or 0-length.
     * @param str the string to be examined
     * @return true if str is null or zero length
     */
    public static boolean isEmpty(CharSequence str) {
        if (str == null || str.length() == 0)
            return true;
        else
            return false;
    }

如果传入是空格,字符串的长度不会为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为入参的构造函数或者转换方式。






  • 7
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值