Spring 和 common-lang3 怎样判断空字符串

本文将针对 Sprting 和 commons-lang3 的 SpringUtils 工具库中如何判断字符串是否为空字符串,即字符串为 null、 长度为 0 或者全部是空格。

本文将针对 Sprting 和 commons-lang3 的 SpringUtils 工具库中如何判断字符串是否为空字符串,即字符串为 null、 长度为 0 或者全部是空格。

Spring 代码

/**
 * Check whether the given {@code String} contains actual <em>text</em>.
 * <p>More specifically, this method returns {@code true} if the
 * {@code String} is not {@code null}, its length is greater than 0,
 * and it contains at least one non-whitespace character.
 * @param str the {@code String} to check (may be {@code null})
 * @return {@code true} if the {@code String} is not {@code null}, its
 * length is greater than 0, and it does not contain whitespace only
 * @see #hasText(CharSequence)
 */
public static boolean hasText(String str) {
    // 在正式判断是否为空串前,先判断是否为 null 或长度 < 0,
    // 可避免空指针和 containsText() 函数调用
    return (hasLength(str) && containsText(str));
}

private static boolean containsText(CharSequence str) {
    int strLen = str.length();
    for (int i = 0; i < strLen; i++) {
        // 不是空白字符就直接返回,后续字符不需要在判断
        if (!Character.isWhitespace(str.charAt(i))) {
            return true;
        }
    }
    return false;
}

commons-lang3 代码

/**
 * <p>Checks if a CharSequence is not empty (""), not null and not whitespace only.</p>
 *
 * <p>Whitespace is defined by {@link Character#isWhitespace(char)}.</p>
 *
 * <pre>
 * StringUtils.isNotBlank(null)      = false
 * StringUtils.isNotBlank("")        = false
 * StringUtils.isNotBlank(" ")       = false
 * StringUtils.isNotBlank("bob")     = true
 * StringUtils.isNotBlank("  bob  ") = true
 * </pre>
 *
 * @param cs  the CharSequence to check, may be null
 * @return {@code true} if the CharSequence is
 *  not empty and not null and not whitespace only
 * @since 2.0
 * @since 3.0 Changed signature from isNotBlank(String) to isNotBlank(CharSequence)
 */
public static boolean isNotBlank(final CharSequence cs) {
    // 与 Spring 的代码比较,多做了一次取反
    return !isBlank(cs);
}

/**
 * <p>Checks if a CharSequence is empty (""), null or whitespace only.</p>
 *
 * <p>Whitespace is defined by {@link Character#isWhitespace(char)}.</p>
 *
 * <pre>
 * StringUtils.isBlank(null)      = true
 * StringUtils.isBlank("")        = true
 * StringUtils.isBlank(" ")       = true
 * StringUtils.isBlank("bob")     = false
 * StringUtils.isBlank("  bob  ") = false
 * </pre>
 *
 * @param cs  the CharSequence to check, may be null
 * @return {@code true} if the CharSequence is null, empty or whitespace only
 * @since 2.0
 * @since 3.0 Changed signature from isBlank(String) to isBlank(CharSequence)
 */
public static boolean isBlank(final CharSequence cs) {
    int strLen;
    // 这里将 cs.length() 写在 cs == null 后面同样可以
    // 避免空指针,并且在为空的情况下少调用一次 cs.length() 方法
    if (cs == null || (strLen = cs.length()) == 0) {
        return true;
    }
    for (int i = 0; i < strLen; i++) {
        // 不是空白字符就直接返回,后续字符不需要在判断
        if (!Character.isWhitespace(cs.charAt(i))) {
            return false;
        }
    }
    return true;
}
少壮不努力,老大徒悲伤。——汉乐府古辞《长歌行》
0 不喜欢
说说我的看法 -
全部评论(
没有评论
关于
本网站专注于 Java、数据库(MySQL、Oracle)、Linux、软件架构及大数据等多领域技术知识分享。涵盖丰富的原创与精选技术文章,助力技术传播与交流。无论是技术新手渴望入门,还是资深开发者寻求进阶,这里都能为您提供深度见解与实用经验,让复杂编码变得轻松易懂,携手共赴技术提升新高度。如有侵权,请来信告知:hxstrive@outlook.com
公众号