StringUtils
是 Apache Commons Lang (org.apache.commons.lang3.StringUtils
) 提供的一个工具类,主要用于字符串处理,可以替代 String
的许多常见操作,让代码更简洁。
Apache Commons Lang3
在 pom.xml
添加依赖(如果是 Maven 项目):
org.apache.commons
commons-lang3
3.12.0
StringUtils
方法以下是 StringUtils
的常见用法:
方法 | 作用 |
---|---|
isEmpty(str) |
null 或 "" 返回 true ,否则返回 false |
isNotEmpty(str) |
null 或 "" 返回 false ,否则返回 true |
isBlank(str) |
null 、"" 、" " 返回 true |
isNotBlank(str) |
null 、"" 、" " 返回 false |
System.out.println(StringUtils.isEmpty(null)); // true
System.out.println(StringUtils.isEmpty("")); // true
System.out.println(StringUtils.isEmpty(" ")); // false
System.out.println(StringUtils.isBlank(" ")); // true
System.out.println(StringUtils.isNotBlank("abc")); // true
方法 | 作用 |
---|---|
join(array, ",") |
数组或列表转为 String ,用 , 分隔 |
joinWith(",", "a", "b", "c") |
类似 join ,可直接传多个参数 |
List list = Arrays.asList("Java", "Python", "C++");
System.out.println(StringUtils.join(list, ",")); // Java,Python,C++
System.out.println(StringUtils.joinWith(" | ", "A", "B", "C")); // A | B | C
方法 | 作用 |
---|---|
trim(str) |
去掉首尾空格 |
trimToNull(str) |
trim() 后如果是 "" ,返回 null |
trimToEmpty(str) |
trim() 后如果是 null ,返回 "" |
System.out.println(StringUtils.trim(" abc ")); // "abc"
System.out.println(StringUtils.trimToNull(" ")); // null
System.out.println(StringUtils.trimToEmpty(null)); // ""
方法 | 作用 |
---|---|
upperCase(str) |
转大写 |
lowerCase(str) |
转小写 |
capitalize(str) |
首字母大写 |
uncapitalize(str) |
首字母小写 |
System.out.println(StringUtils.upperCase("hello")); // HELLO
System.out.println(StringUtils.lowerCase("WORLD")); // world
System.out.println(StringUtils.capitalize("java")); // Java
System.out.println(StringUtils.uncapitalize("Java")); // java
方法 | 作用 |
---|---|
contains(str, searchStr) |
是否包含子串 |
containsIgnoreCase(str, searchStr) |
忽略大小写查找 |
startsWith(str, prefix) |
是否以 prefix 开头 |
endsWith(str, suffix) |
是否以 suffix 结尾 |
System.out.println(StringUtils.contains("hello world", "world")); // true
System.out.println(StringUtils.containsIgnoreCase("Hello World", "hello"));// true
System.out.println(StringUtils.startsWith("java.lang.String", "java")); // true
System.out.println(StringUtils.endsWith("myfile.txt", ".txt")); // true
方法 | 作用 |
---|---|
replace(str, "a", "b") |
替换所有 a 为 b |
replaceOnce(str, "a", "b") |
只替换第一个 a |
replaceIgnoreCase(str, "a", "b") |
忽略大小写替换 |
remove(str, "a") |
删除 a |
System.out.println(StringUtils.replace("abcabc", "a", "X")); // XbcXbc
System.out.println(StringUtils.replaceOnce("abcabc", "a", "X")); // Xbcabc
System.out.println(StringUtils.remove("abcabc", "a")); // bcbc
方法 | 作用 |
---|---|
leftPad(str, size, "0") |
左填充至 size 长度 |
rightPad(str, size, " ") |
右填充至 size 长度 |
System.out.println(StringUtils.leftPad("7", 3, "0")); // 007
System.out.println(StringUtils.rightPad("abc", 5, "-")); // abc--
方法 | 作用 |
---|---|
split(str, ",") |
按 , 拆分 |
splitByWholeSeparator(str, "--") |
按 |
String[] arr = StringUtils.split("a,b,c", ",");
System.out.println(Arrays.toString(arr)); // [a, b, c]
String[] arr2 = StringUtils.splitByWholeSeparator("a--b--c", "--");
System.out.println(Arrays.toString(arr2)); // [a, b, c]
方法 | 作用 |
---|---|
isNumeric(str) |
是否全是数字 |
isAlpha(str) |
是否全是字母 |
isAlphanumeric(str) |
是否全是字母或数字 |
System.out.println(StringUtils.isNumeric("12345")); // true
System.out.println(StringUtils.isNumeric("123a")); // false
System.out.println(StringUtils.isAlpha("abc")); // true
System.out.println(StringUtils.isAlphanumeric("abc123"));// true
分类 | 方法 | 作用 |
---|---|---|
判断空 | isEmpty() / isBlank() |
判断是否 null 或 "" |
拼接 | join() / joinWith() |
List 转 String |
去空格 | trim() / trimToNull() |
去掉空格并转换 |
大小写 | upperCase() / capitalize() |
字母大小写转换 |
查找 | contains() / startsWith() |
是否包含/开头/结尾 |
替换 | replace() / remove() |
字符串替换或删除 |
填充 | leftPad() / rightPad() |
字符填充 |
拆分 | split() |
拆分字符串 |
判断类型 | isNumeric() / isAlpha() |
是否数字/字母 |