赞
踩
StringUtils类是org.apache.commons.lang3包下提供的一个常用于处理字符串的工具类。
- <dependency>
- <groupId>org.apache.commons</groupId>
- <artifactId>commons-lang3</artifactId>
- <version>3.9</version>
- </dependency>
- abbreviateMiddle:缩写字符串的中间部分。
- appendIfMissing:如果字符串不以指定后缀结尾,则追加后缀。
- capitalize:将字符串的第一个字符大写。
- center:将字符串居中对齐。
- chomp:删除字符串末尾的换行符。
- compare:比较两个字符串的大小。
- contains:检查字符串是否包含指定内容。
- countMatches:计算字符串中指定子串出现的次数。
- defaultIfBlank:如果字符串为空或只包含空格,则返回默认值。
- deleteWhitespace:删除字符串中的所有空格字符。
- endsWith:检查字符串是否以指定后缀结尾。
- getCommonPrefix:获取多个字符串的共同前缀。
- indexOf:获取子串在字符串中第一次出现的位置索引。
- isBlank:检查字符串是否为空或只包含空格。
- join:将数组中的元素连接成一个字符串。
- lastIndexOf:获取子串在字符串中最后一次出现的位置索引。
- replace:替换字符串中的指定内容。
- split:根据指定分隔符拆分字符串。
- startsWith:检查字符串是否以指定前缀开头。
- substring:截取字符串的子串。
- toUpperCase:将字符串转换为大写。
- toLowerCase:将字符串转换为小写。
- trim:删除字符串两端的空格。
- containsAny:检查字符串是否包含任何指定字符。
- containsIgnoreCase:不区分大小写地检查字符串是否包含指定内容。
- containsNone:检查字符串是否不包含指定字符。
- containsOnly:检查字符串是否只包含指定字符。
- containsWhitespace:检查字符串是否包含空格字符。
- defaultString:如果字符串为null,则返回空字符串。
- endsWithAny:检查字符串是否以任何指定后缀结尾。
- equalsIgnoreCase:忽略大小写比较字符串是否相等。
- getBytes:获取字符串的字节数组。
- indexOfAny:获取字符串中任何指定字符第一次出现的位置索引。
- isAlpha:检查字符串是否只包含字母字符。
- isNumeric:检查字符串是否只包含数字字符。
- joinWith:使用指定分隔符连接字符串数组。
- leftPad:在字符串左侧填充指定字符至指定长度。
- length:获取字符串的长度。
- matches:使用正则表达式检查字符串是否匹配。
- reverse:反转字符串。
- splitByWholeSeparator:根据指定分隔符拆分字符串,保留所有分隔符。
- startsWithAny:检查字符串是否以任何指定前缀开头。
- stripAccents:去除字符串中的重音符号。
- substringAfter:获取指定子串之后的部分字符串。
- toCodePoints:将字符串转换为Unicode代码点数组。
- upperCase:将字符串转换为大写。
- isNotEmpty:检查字符串是否不为null且长度大于0,即字符串不为空。
- isNotBlank:检查字符串是否不为null且长度大于0,并且不只包含空格字符。
- StringUtils.isEmpty(null) = true
- StringUtils.isEmpty(“”) = true
- StringUtils.isEmpty(" “) = false
- StringUtils.isEmpty(” “) = false
- StringUtils.isEmpty(“aaa”) = false
- StringUtils.isEmpty(” aaa") = false
StringUtils中isEmpty 空格作非空处理
- StringUtils.isBlank(“\b”) = false
- StringUtils.isBlank(“aaa”) = false
- StringUtils.isBlank(” aaa") = false
- StringUtils.isBlank(null) = true
- StringUtils.isBlank(“”) = true
- StringUtils.isBlank(" “) = true
- StringUtils.isBlank(” “) = true
- StringUtils.isBlank(”\t \n \f \r") = true
- String str = " hello ";
- String trimmed = StringUtils.trim(str);
- System.out.println(trimmed); // 输出 "hello"
- String str = "hello world";
- String sub = StringUtils.substring(str, 6, 11);
- System.out.println(sub); // 输出 "world"
- String[] arr = {"apple", "banana", "orange"};
- String joinedStr = StringUtils.join(arr, ",");
- System.out.println(joinedStr); // 输出 "apple,banana,orange"
- String str = "Hello World";
- if(StringUtils.containsIgnoreCase(str, "hello")) {
- System.out.println("String contains 'hello' (case-insensitive)");
- } else {
- System.out.println("String does not contain 'hello'");
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。