当前位置:   article > 正文

StringUtils工具类的使用_stringutils.tocodepoints

stringutils.tocodepoints

StringUtils类是org.apache.commons.lang3包下提供的一个常用于处理字符串的工具类。

1. 使用需要引入以下依赖

  1. <dependency>
  2. <groupId>org.apache.commons</groupId>
  3. <artifactId>commons-lang3</artifactId>
  4. <version>3.9</version>
  5. </dependency>

2. 常用方法

  • 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,并且不只包含空格字符。

 3. 常用代码示例

        3.1  isEmpty()      
  • 检查字符串是否为空:
  1. StringUtils.isEmpty(null) = true
  2. StringUtils.isEmpty(“”) = true
  3. StringUtils.isEmpty(" “) = false
  4. StringUtils.isEmpty(” “) = false
  5. StringUtils.isEmpty(“aaa”) = false
  6. StringUtils.isEmpty(” aaa") = false

StringUtils中isEmpty 空格作非空处理

3.2  isBlank()
  • 检查字符串是否为空或只包含空格:
  1. StringUtils.isBlank(“\b”) = false
  2. StringUtils.isBlank(“aaa”) = false
  3. StringUtils.isBlank(” aaa") = false
  4. StringUtils.isBlank(null) = true
  5. StringUtils.isBlank(“”) = true
  6. StringUtils.isBlank(" “) = true
  7. StringUtils.isBlank(” “) = true
  8. StringUtils.isBlank(”\t \n \f \r") = true

制表符换行符、换页符和回车符StringUtils.isBlank()均识为空白符

 3.3  trim()
  • 去除字符串两端的空格:
  1. String str = " hello ";
  2. String trimmed = StringUtils.trim(str);
  3. System.out.println(trimmed); // 输出 "hello"
3.4  substring()
  • 截取字符串的子串:
  1. String str = "hello world";
  2. String sub = StringUtils.substring(str, 6, 11);
  3. System.out.println(sub); // 输出 "world"
3.5  join()
  • 将数组中的元素连接成一个字符串:
  1. String[] arr = {"apple", "banana", "orange"};
  2. String joinedStr = StringUtils.join(arr, ",");
  3. System.out.println(joinedStr); // 输出 "apple,banana,orange"
3.6  containsIgnoreCase()
  • 不区分大小写地检查字符串是否包含指定内容:
  1. String str = "Hello World";
  2. if(StringUtils.containsIgnoreCase(str, "hello")) {
  3. System.out.println("String contains 'hello' (case-insensitive)");
  4. } else {
  5. System.out.println("String does not contain 'hello'");
  6. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/1005556
推荐阅读
相关标签
  

闽ICP备14008679号