美文网首页
Apache commons-lang3 工具类

Apache commons-lang3 工具类

作者: Tinyspot | 来源:发表于2023-01-06 09:47 被阅读0次

1. 依赖包

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.12.0</version>
</dependency>

2. StringUtils

  • 字符串比较 equals() / equalsIgnoreCase()
  • 字符串匹配 startsWith() / endsWith()
  • 大小写转换 upperCase() / lowerCase()

2.1 .isEmpty() 与 isBlank()

  • StringUtils.isBlank() 增加了一个 for 循环
/**
 * StringUtils.isEmpty(null)      = true
 * StringUtils.isEmpty("")        = true
 * StringUtils.isEmpty(" ")       = false
 */
public static boolean isEmpty(final CharSequence cs) {
    return cs == null || cs.length() == 0;
}

/**
 * StringUtils.isBlank(null)      = true
 * StringUtils.isBlank("")        = true
 * StringUtils.isBlank(" ")       = true
 */
public static boolean isBlank(final CharSequence cs) {
    final int strLen = length(cs);
    if (strLen == 0) {
        return true;
    }
    for (int i = 0; i < strLen; i++) {
        if (!Character.isWhitespace(cs.charAt(i))) {
            return false;
        }
    }
    return true;
}

2.2 trim() 与 deleteWhitespace()

  • trim() 删除两端的空字符串
  • deleteWhitespace() 删除空格
StringUtils.trim(" 12 34 "); // 12 34
StringUtils.deleteWhitespace(" 12 34 "); // 1234

2.3 split() 与 join()

public static String[] split(final String str, final char separatorChar) {
    return splitWorker(str, separatorChar, false);
}
String[] splits = StringUtils.split("we,er,34,y5", ",");

List<Integer> values = Arrays.asList(12, 23);
String join = StringUtils.join(values, ",");

2.4 StringUtils.isNumeric()

/**
 * commons-lang3:
 *  StringUtils.isNumeric(null)   = false
 *  StringUtils.isNumeric("")     = false
 *  StringUtils.isNumeric("  ")   = false
 */
public static boolean isNumeric(final CharSequence cs) {
    
}

/**
 * commons-lang2: 
 *  StringUtils.isNumeric(null)   = false
 *  StringUtils.isNumeric("")     = true (注意)
 *  StringUtils.isNumeric("  ")   = false
 */
public static boolean isNumeric(String str) {
    
}

3. 随机字符串 RandomUtils

public static void main(String[] args) {
    System.out.println(RandomStringUtils.random(10));
    System.out.println(RandomStringUtils.random(10, false, true));
    System.out.println(RandomStringUtils.random(10, true, true));

    // 拉丁字母(a-z、A-Z)
    System.out.println(RandomStringUtils.randomAlphabetic(10));
    // 数字0-9
    System.out.println(RandomStringUtils.randomNumeric(10));
    // 拉丁字母(a-z、A-Z) 和数字
    System.out.println(RandomStringUtils.randomAlphanumeric(10));
}

RandomStringUtils 源码

public static String random(final int count) {
    return random(count, false, false);
}

/**
 * @param count  字符串长度
 * @param letters 是否包含字母
 * @param numbers 是否包含数字
 * @return
 */
public static String random(final int count, final boolean letters, final boolean numbers) {
    return random(count, 0, 0, letters, numbers);
}

4. 去除转义字符

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.12.0</version>
</dependency>
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-text</artifactId>
    <version>1.10.0</version>
</dependency>

依赖使用到的是apache的commons-text包(因为commons-lang3中的StringEscapeUtils.unescapeJava已废弃,需要使用commons-text)

String s = StringEscapeUtils.unescapeJava(str);

5. commons-lang3 与 commons-lang 区别

  • lang3 废除了一些旧API
  • StringUtils 方法参数由 String 变为 CharSequence

相关文章

网友评论

      本文标题:Apache commons-lang3 工具类

      本文链接:https://www.haomeiwen.com/subject/bkdscdtx.html