Java StringUtils工具类常用方法详解

Java StringUtils工具类常用方法详解

StringUtils是Apache Commons Lang库中一个极其常用的工具类,它提供了大量处理字符串的静态方法,能够简化我们的日常开发工作,提高代码的可读性和健壮性。下面我将详细介绍StringUtils类中最常用的方法及其使用场景。

一、StringUtils的基本介绍

Apache Commons Lang库中的StringUtils类提供了比JDK原生String类更丰富的字符串操作方法,所有方法都是null安全的,这意味着它们可以优雅地处理null值而不会抛出NullPointerException。

引入依赖创建springboot工程会自动引入该依赖

org.apache.commons

commons-lang3

3.12.0

二、空校验与空白校验

1. isEmpty() - 空字符串校验

StringUtils.isEmpty(null) // true

StringUtils.isEmpty("") // true

StringUtils.isEmpty(" ") // false

StringUtils.isEmpty("test") // false

2. isNotEmpty() - 非空校验

StringUtils.isNotEmpty(null) // false

StringUtils.isNotEmpty("") // false

StringUtils.isNotEmpty(" ") // true

StringUtils.isNotEmpty("test") // true

3. isBlank() - 空白字符串校验

StringUtils.isBlank(null) // true

StringUtils.isBlank("") // true

StringUtils.isBlank(" ") // true

StringUtils.isBlank("\t\n") // true

StringUtils.isBlank("test") // false

4. isNotBlank() - 非空白字符串校验

StringUtils.isNotBlank(null) // false

StringUtils.isNotBlank("") // false

StringUtils.isNotBlank(" ") // false

StringUtils.isNotBlank("test") // true

三、字符串截取与分割

5. substring() - 安全截取字符串

StringUtils.substring(null, 2) // null

StringUtils.substring("", 2) // ""

StringUtils.substring("abc", 0) // "abc"

StringUtils.substring("abc", 2) // "c"

StringUtils.substring("abc", 4) // ""

6. split() - 字符串分割

String[] result = StringUtils.split("a..b.c", '.');

// result = ["a", "b", "c"]

7. join() - 字符串连接

StringUtils.join(new String[] {"a", "b", "c"}, ",") // "a,b,c"

四、字符串比较

8. equals() - 安全字符串比较

StringUtils.equals(null, null) // true

StringUtils.equals(null, "abc") // false

StringUtils.equals("abc", null) // false

StringUtils.equals("abc", "abc") // true

StringUtils.equals("abc", "ABC") // false

9. equalsIgnoreCase() - 忽略大小写比较

StringUtils.equalsIgnoreCase("abc", "ABC") // true

10. compare() - 安全字符串比较(可处理null)

StringUtils.compare(null, null) // 0

StringUtils.compare(null , "a") // -1

StringUtils.compare("a", null) // 1

StringUtils.compare("a", "a") // 0

StringUtils.compare("a", "b") // -1

五、字符串填充与去除

11. trim() - 去除两端空白

StringUtils.trim(null) // null

StringUtils.trim("") // ""

StringUtils.trim(" ") // ""

StringUtils.trim("abc ") // "abc"

StringUtils.trim(" abc ") // "abc"

12. strip() - 扩展去除空白

StringUtils.strip(" ab c ", null) // "ab c"

StringUtils.strip(" ab c ", " ") // "ab c"

StringUtils.strip("abcab", "ab") // "c"

13. leftPad() / rightPad() - 字符串填充

StringUtils.leftPad("abc", 5, ' ') // " abc"

StringUtils.rightPad("abc", 5, ' ') // "abc "

六、字符串查找与替换

14. contains() - 包含检查

StringUtils.contains(null, *) // false

StringUtils.contains(*, null) // false

StringUtils.contains("abc", 'a') // true

StringUtils.contains("abc", "z") // false

15. countMatches() - 统计出现次数

StringUtils.countMatches("abba", "a") // 2

StringUtils.countMatches("abba", "b") // 2

StringUtils.countMatches("abba", 'a') // 2

16. replace() - 字符串替换

StringUtils.replace("aba", "a", "z") // "zbz"

StringUtils.replace("abc", "d", "z") // "abc"

七、字符串转换

17. capitalize() / uncapitalize() - 首字母大小写转换

StringUtils.capitalize("cat") // "Cat"

StringUtils.uncapitalize("Cat") // "cat"

18. swapCase() - 大小写互换

StringUtils.swapCase("The Dog has a BONE") // "tHE dOG HAS A bone"

19. reverse() - 字符串反转

StringUtils.reverse("bat") // "tab"

八、其他实用方法

20. defaultString() - null值默认字符串

StringUtils.defaultString(null) // ""

StringUtils.defaultString("") // ""

StringUtils.defaultString("bat") // "bat"

StringUtils.defaultString(null, "default") // "default"

21. abbreviate() - 字符串缩写

StringUtils.abbreviate("abcdefg", 6) // "abc..."

StringUtils.abbreviate("abcdefg", "...", 4) // "a..."

22. difference() - 查找不同部分

StringUtils.difference("abcde", "abxyz") // "xyz"

总结

StringUtils提供的方法远比原生String类丰富且安全,可以显著提高开发效率和代码质量。这里介绍的只是最常用的部分方法,实际使用中还有更多实用功能如normalizeSpace()、isNumeric()、wrap()等方法,都值得在项目中尝试使用。

正确处理字符串是每个Java开发者必备的技能,掌握StringUtils能让这项工作变得更加轻松和高效。希望这篇博客能帮助你更好地理解并使用这个强大的工具类!

相关风雨

《酷狗唱唱》耳返开关设置方法
beat365亚洲体育在线

《酷狗唱唱》耳返开关设置方法

🌊 08-23 💨 阅读 3428
脆性材料有哪些(韧性材料和脆性材料)
beat365亚洲体育在线

脆性材料有哪些(韧性材料和脆性材料)

🌊 09-15 💨 阅读 5734
娃娃皇后》
beat365亚洲体育在线

娃娃皇后》

🌊 08-24 💨 阅读 9344