commons-lang3之StringUtils源码解析

源码版本

commons-lang3-3.1.jar

源码介绍

字符串的Trim处理

/**
* <pre>
* StringUtils.trim(null)          = null
* StringUtils.trim("")            = ""
* StringUtils.trim("     ")       = ""
* StringUtils.trim("abc")         = "abc"
* StringUtils.trim("    abc    ") = "abc"
* </pre>
*/
public static String trim(String str) {
    return str == null ? null : str.trim();
}

字符串的isBlank处理

java.lang.String实现了CharSequence接口

/**
 * <p>Checks if a CharSequence is whitespace, empty ("") or 
 * null.</p>
 * <pre>
 * StringUtils.isBlank(null)      = true
 * StringUtils.isBlank("")        = true
 * StringUtils.isBlank(" ")       = true
 * StringUtils.isBlank("bob")     = false
 * StringUtils.isBlank("  bob  ") = false
 * </pre>
 * @since 2.0
 * @since 3.0 Changed signature from isBlank(String) to isBlank(CharSequence)
 */
public static boolean isBlank(CharSequence cs) {
    int strLen;
    if (cs == null || (strLen = cs.length()) == 0) {
        return true;
    }
    for (int i = 0; i < strLen; i++) {
        if (Character.isWhitespace(cs.charAt(i)) == false) {
            return false;
        }
    }
    return true;
}

Empty checks

/**
* <p>Checks if a CharSequence is empty ("") or null.</p>
* <pre>
* StringUtils.isEmpty(null)      = true
* StringUtils.isEmpty("")        = true
* StringUtils.isEmpty(" ")       = false
* StringUtils.isEmpty("bob")     = false
* StringUtils.isEmpty("  bob  ") = false
* </pre>
*/
public static boolean isEmpty(CharSequence cs) {
   return cs == null || cs.length() == 0;
}

Reversing

/**
 * <p>Reverses a String as per {@link StringBuilder#reverse()}.
 * </p>
 * <p>A {@code null} String returns {@code null}.</p>
 *
 * <pre>
 * StringUtils.reverse(null)  = null
 * StringUtils.reverse("")    = ""
 * StringUtils.reverse("bat") = "tab"
 * </pre>
 */
public static String reverse(String str) {
    if (str == null) {
        return null;
    }
    return new StringBuilder(str).reverse().toString();
}

猜你喜欢

转载自blog.csdn.net/thebigdipperbdx/article/details/81487409