StringUtils

一:将数组中的字符转换为一个字符串

1、 将数组中的字符转换为一个字符串 

    String[3] s={"a","b","c"} 
    StringUtil.convString(s)="a,b,c" ;

2,static public String converString(String strToConv, String conv) 

   @param strToConv 要转换的字符串 
   @param conv 分隔符,默认以逗号分隔 

   String[3] s={"a","b","c"} 
   StringUtil.convString(s,"@")="a@b@c" 

二:空值处理

3,空值检测:

  public static boolean isEmpty(String str) 

  @param str the String to check, may be null 
  @return true if the String is empty or null 

  判断一个字符串是否为空,空格作非空处理。 

  StringUtils.isEmpty(null) = true ;StringUtils.isEmpty("") = true; StringUtils.isEmpty(" ") = false; 

  StringUtils.isEmpty("bob") = false ;StringUtils.isEmpty(" bob ") = false ;

4,非空处理: 

  public static boolean isNotEmpty(String str) 

  @param str the String to check, may be null 
  @return true if the String is not empty and not null 

  判断一个字符串是否非空,空格作非空处理. 

  StringUtils.isNotEmpty(null) = false; StringUtils.isNotEmpty("") = false ;StringUtils.isNotEmpty(" ") = true; 

  StringUtils.isNotEmpty("bob") = true ;StringUtils.isNotEmpty(" bob ") = true ;

5, 去空格处理 :返回去除空格后的对象,若是null,返回null

  public static String trim(String str) 

  @param str the String to be trimmed, may be null 
  @return the trimmed string, null if null String input 

  格式化一个字符串中的空格,有非空判断处理;

  StringUtils.trim(null) = null; StringUtils.trim("") = "" ;StringUtils.trim(" ") = ""; StringUtils.trim("abc") = "abc"; 

  StringUtils.trim(" abc ") = "abc" ;

6,去空格处理 :返回去除空格后的对象,若是null/""/" ",返回null

  public static String trimToNull(String str) 

  @param str the String to be trimmed, may be null 

  格式化一个字符串中的空格,有非空判断处理,如果为空返回null; 

  StringUtils.trimToNull(null) = null ;StringUtils.trimToNull("") = null ;StringUtils.trimToNull(" ") = null ;

  StringUtils.trimToNull("abc") = "abc";     StringUtils.trimToNull(" abc ") = "abc";

7,空格处理 :返回去除空格后的对象,若是null/""/" ",返回""

  public static String trimToEmpty(String str)  

  @param str the String to be trimmed, may be null 

  格式化一个字符串中的空格,有非空判断处理,如果为空返回"";

  StringUtils.trimToEmpty(null) = "" ;StringUtils.trimToEmpty("") = "" ;StringUtils.trimToEmpty(" ") = "" ;

  StringUtils.trimToEmpty("abc") = "abc" ;StringUtils.trimToEmpty(" abc ") = "abc"; 

三:字符串比较:

9,public static boolean equals(String str1, String str2) 

   判断两个字符串是否相等,有非空处理,关注大小写。 

   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 ;

10,public static boolean equalsIgnoreCase(String str1, String str2) 

    判断两个字符串是否相等,有非空处理。忽略大小写 

    StringUtils.equalsIgnoreCase(null, null) = true ;StringUtils.equalsIgnoreCase(null, "abc") = false ;

    StringUtils.equalsIgnoreCase("abc", null) = false ;StringUtils.equalsIgnoreCase("abc", "abc") = true ;

    StringUtils.equalsIgnoreCase("abc", "ABC") = true ;

四:IndexOf 处理 

11,public static int indexOf(String str, String searchStr) 

    @param str the String to check, may be null 
    @param searchStr the String to find, may be null 
    @return the first index of the search String

    返回要查找的字符串第一次所在位置,有非空处理 

    StringUtils.indexOf(null, *) = -1; StringUtils.indexOf(*, null) = -1 ;StringUtils.indexOf("", "") = 0 ;

    StringUtils.indexOf("aabaabaa", "a") = 0; StringUtils.indexOf("aabaabaa", "b") = 2 ;StringUtils.indexOf("aabaabaa", "ab") = 1;  

    StringUtils.indexOf("aabaabaa", "") = 0 ;

12,public static int indexOf(String str, String searchStr, int startPos) 

    @param str the String to check, may be null 
    @param searchStr the String to find, may be null 
    @param startPos the start position, negative treated as zero 
    @return the first index of the search String, -1 if no match or null string input 

    返回要由指定位置开始查找的字符串第一次所在位置,有非空处理 

    StringUtils.indexOf(null, *, *) = -1; StringUtils.indexOf(*, null, *) = -1 ;StringUtils.indexOf("", "", 0) = 0 ;

    StringUtils.indexOf("aabaabaa", "a", 0) = 0 ;StringUtils.indexOf("aabaabaa", "b", 0) = 2 ;StringUtils.indexOf("aabaabaa", "ab", 0) = 1;

    StringUtils.indexOf("aabaabaa", "b", 3) = 5; StringUtils.indexOf("aabaabaa", "b", 9) = -1; StringUtils.indexOf("aabaabaa", "b", -1) = 2;

    StringUtils.indexOf("aabaabaa", "", 2) = 2 ;StringUtils.indexOf("abc", "", 9) = 3 ;
五:子字符串处理

13,public static String substring(String str, int start) 

   @param str the String to get the substring from, may be null 
   @param start the position to start from, negative means 
      count back from the end of the String by this many characters 
   @return substring from start position, null if null String input 

   返回指定位置开始的字符串中的所有字符, start>0表示从左向右, start<0表示从右向左, start=0则从左第一位开始

   StringUtils.substring(null, *) = null ;StringUtils.substring("", *) = "" ;StringUtils.substring("abc", 0) = "abc";

   StringUtils.substring("abc", 2) = "c" ;StringUtils.substring("abc", 4) = "" ;StringUtils.substring("abc", -2) = "bc";

   StringUtils.substring("abc", -4) = "abc" ;

14,public static String substring(String str, int start, int end)

    @param str the String to get the substring from, may be null 
    @param start the position to start from, negative means 
      count back from the end of the String by this many characters 
    @param end the position to end at (exclusive), negative means 
       count back from the end of the String by this many characters 
    @return substring from start position to end positon, null if null String input 

    返回由开始位置到结束位置之间的子字符串, 

     start>0&&end>0从左开始(包括左)到右结束(不包括右),

     start<0&&end<0从右开始(包括右),再向左数到end结束(包括end)

    StringUtils.substring(null, *, *) = null ;StringUtils.substring("", * , *) = "";

    StringUtils.substring("abc", 0, 2) = "ab" ;StringUtils.substring("abc", 2, 0) = ""; StringUtils.substring("abc", 2, 4) = "c";

    StringUtils.substring("abc", 4, 6) = "" ;StringUtils.substring("abc", 2, 2) = ""; StringUtils.substring("abc", -2, -1) = "b";

    StringUtils.substring("abc", -4, 2) = "ab" ;
15,
public static String substringBefore(String str, String separator) 
    @param str the String to get a substring from, may be null 
    @param separator the String to search for, may be null 
    @return the substring before the first occurance of the separator, 
null if null String input 

    Gets the substring before the first occurance of a separator. 
    The separator is not returned. 
    A null string input will return null. 
    An empty ("") string input will return the empty string. 
    A null separator will return the input string. 
    返回指定字符串之前的所有字符 

   StringUtils.substringBefore(null, *) = null; StringUtils.substringBefore("", *) = ""; StringUtils.substringBefore("abc", "a") = "";

   StringUtils.substringBefore("abcba", "b") = "a"; StringUtils.substringBefore("abc", "c") = "ab";

   StringUtils.substringBefore("abc", "d") = "abc" ;StringUtils.substringBefore("abc", "") = "" ;

   StringUtils.substringBefore("abc", null) = "abc" ;

16,public static String substringAfter(String str, String separator) 

    @param str the String to get a substring from, may be null 
    @param separator the String to search for, may be null 
    @return the substring after the first occurance of the separator, null if null String input 

   Gets the substring after the first occurance of a separator. 
   The separator is not returned. 
   A null string input will return null. 
   An empty ("") string input will return the empty string. 
   A null separator will return the empty string if the input string is not null. 
   返回指定字符串之后的所有字符

   StringUtils.substringAfter(null, *) = null; StringUtils.substringAfter("", *) = "" ;StringUtils.substringAfter(*, null) = ""; 

   StringUtils.substringAfter("abc", "a") = "bc"; StringUtils.substringAfter("abcba", "b") = "cba";

   StringUtils.substringAfter("abc", "c") = ""; StringUtils.substringAfter("abc", "d") = "" ;StringUtils.substringAfter("abc", "") = "abc" ;

17,public static String substringBeforeLast(String str, String separator) 

   Gets the substring before the last occurance of a separator. 
   The separator is not returned. 
   A null string input will return null. 
   An empty ("") string input will return the empty string. 
   An empty or null separator will return the input string. 

   返回最后一个指定字符串之前的所有字符 

   StringUtils.substringBeforeLast(null, *) = null ;StringUtils.substringBeforeLast("", *) = "";

   StringUtils.substringBeforeLast("abcba", "b") = "abc" ;StringUtils.substringBeforeLast("abc", "c") = "ab";

   StringUtils.substringBeforeLast("a", "a") = ""; StringUtils.substringBeforeLast("a", "z") = "a";

   StringUtils.substringBeforeLast("a", null) = "a" ;StringUtils.substringBeforeLast("a", "") = "a"; 

18,public static String substringAfterLast(String str, String separator) 

    Gets the substring after the last occurance of a separator. The separator is not returned. 
    A null string input will return null. An empty ("") string input will return the empty string. 
    An empty or null separator will return the empty string if the input string is not null. 

    返回最后一个指定字符串之后的所有字符 

    StringUtils.substringAfterLast(null, *) = null; StringUtils.substringAfterLast("", *) = "" ;StringUtils.substringAfterLast(*, "") = "";  

    StringUtils.substringAfterLast(*, null) = "" ;StringUtils.substringAfterLast("abc", "a") = "bc" ;

    StringUtils.substringAfterLast("abcba", "b") = "a"; StringUtils.substringAfterLast("abc", "c") = "";

    StringUtils.substringAfterLast("a", "a") = "" ;StringUtils.substringAfterLast("a", "z") = "";

六: Replacing(字符串替换)

19,public static String replace(String text, String repl, String with) 

   @param text text to search and replace in, may be null 
   @param repl the String to search for, may be null 
   @param with the String to replace with, may be null 
   @return the text with any replacements processed, null if null String input 

   Replaces all occurances of a String within another String. A null reference passed to this method is a no-op. 

   以指定字符串替换原来字符串的的指定字符串 ,repl是所给字符串中存在的需要被替换的字符串,with是需要被替换成的新字符串;

   StringUtils.replace(null, *, *) = null; StringUtils.replace("", *, *) = ""; StringUtils.replace("aba", null, null) = "aba";

   StringUtils.replace("aba", null, null) = "aba"; StringUtils.replace("aba", "a", null) = "aba"; 

   StringUtils.replace("aba", "a", "") = "aba"; StringUtils.replace("aba", "a", "z") = "zbz" ;
20,public static String replace(String text, String repl, String with, int max) 

    @param text text to search and replace in, may be null 
    @param repl the String to search for, may be null 
    @param with the String to replace with, may be null 
    @param max maximum number of values to replace, or -1 if no maximum 
    @return the text with any replacements processed,null if null String input 

    Replaces a String with another String inside a larger String, for the first max values of the search String. 
    A null reference passed to this method is a no-op.

    以指定字符串最大替换原来字符串的的指定字符串,max>0从左向右到max处截止(包括max)替换0到max之间指定的repl为with 

    StringUtils.replace(null, *, *, *) = null; StringUtils.replace("", *, *, *) = ""; StringUtils.replace("abaa", null, null, 1) = "abaa";

    StringUtils.replace("abaa", null, null, 1) = "abaa"; StringUtils.replace("abaa", "a", null, 1) = "abaa" ;

    StringUtils.replace("abaa", "a", "", 1) = "abaa"; StringUtils.replace("abaa", "a", "z", 0) = "abaa" ;

    StringUtils.replace("abaa", "a", "z", 1) = "zbaa" ;StringUtils.replace("abaa", "a", "z", 2) = "zbza";

    StringUtils.replace("abaa", "a", "z", -1) = "zbzz" ;    

七,Case conversion(大小写转换) 

21,public static String upperCase(String str) 

    @param str the String to upper case, may be null 
    @return the upper cased String, null if null String input 

    Converts a String to upper case as per {@link String#toUpperCase()}. A null input String returns null. 

     将一个字符串变为大写

     StringUtils.upperCase(null) = null; StringUtils.upperCase("") = ""; StringUtils.upperCase("aBc") = "ABC" ;

22,public static String lowerCase(String str) 

    @param str the String to lower case, may be null 
    @return the lower cased String, null if null String input 

     A null input String returns null. 
     将一个字符串转换为小写 

     StringUtils.lowerCase(null) = null ;StringUtils.lowerCase("") = "" ;StringUtils.lowerCase("aBc") = "abc" ;

23,public static String capitalize(String str)

    将字符串中的首字母大写 

    StringUtils.capitalize(null) = null; StringUtils.capitalize("") = "" ;

    StringUtils.capitalize("cat") = "Cat" ;StringUtils.capitalize("cAt") = "CAt" ;
### StringUtils 类的功能概述 `StringUtils` 是 Apache Commons Library 中的一部分,属于 `org.apache.commons.lang3` 包下的工具类[^2]。该类主要用于简化字符串操作,提供了许多静态方法来处理常见的字符串问题。这些方法能够帮助开发者更高效地完成字符串的判断、转换和格式化等工作。 以下是 `StringUtils` 的主要功能分类及其描述: #### 1. 字符串判空 `StringUtils` 提供了一系列用于检测字符串是否为空或者仅包含空白字符的方法。例如: - `isEmpty(String str)`:如果字符串为 null 或长度为零,则返回 true。 - `isNotEmpty(String str)`:与 isEmpty 方法相反,当字符串不为空时返回 true。 - `isBlank(String str)`:如果字符串为 null 或者只包含空白字符(如空格),则返回 true。 - `isNotBlank(String str)`:与 isBlank 方法相反,当字符串既不是 null 又含有非空白字符时返回 true。 ```java System.out.println(StringUtils.isEmpty(null)); // 输出: true System.out.println(StringUtils.isBlank("")); // 输出: true System.out.println(StringUtils.isNotBlank("abc")); // 输出: true ``` #### 2. 去除多余字符 此类方法可以帮助去除字符串两端或内部多余的空白字符及其他指定字符。 - `trim(String str)`:移除字符串首尾的空白字符。 - `strip(String str, String stripChars)`:删除字符串开头和结尾处由参数定义的一组字符。 - `removeStart(String str, String remove)` 和 `removeEnd(String str, String remove)`:分别去掉字符串前缀或后缀部分匹配的内容。 ```java String result = StringUtils.strip(" Hello World! ", " H!"); // 结果:"ello World" ``` #### 3. 大小写转换 通过调用以下函数实现大小写的统一管理。 - `upperCase(String str)` - `lowerCase(String str)` ```java System.out.println(StringUtils.upperCase("hello")); // HELLO System.out.println(StringUtils.lowerCase("WORLD")); // world ``` #### 4. 子串查找与替换 支持灵活定位子序列位置以及批量替换单词等功能。 - `contains(CharSequence seq, CharSequence searchSeq)` - `replaceEachRepeatedly(String text, String[] searchList, String[] replacementList)` ```java boolean foundA = StringUtils.contains("ABCDEF", 'A'); // true String replacedText = StringUtils.replaceEachRepeatedly( "a_b_c_", new String[]{"_"}, new String[]{"/"}); // a/b/c/ ``` #### 5. 随机生成器 利用随机数种子创建固定长度的新密码或者其他形式的数据片段。 - `RandomStringUtils.random(int count)` - `RandomStringUtils.randomAlphabetic(int count)` ```java String randomStr = RandomStringUtils.random(8); System.out.println(randomStr.length()); // 8 ``` --- ### 使用场景举例 假设我们需要开发一款应用程序,在其中验证用户输入邮箱地址的有效性并清理数据。我们可以借助于 `StringUtils` 来快速构建逻辑框架如下所示: ```java public boolean isValidEmail(String email){ if (StringUtils.isBlank(email)) { return false; } String trimmedEmail = StringUtils.trim(email); int atIndex = trimmedEmail.indexOf("@"); ... } ``` 上述代码展示了如何运用 `StringUtils.isBlank()` 判断字段是否存在有效值;再经过去掉冗余空间之后进一步分析结构特性等等。 --- ### 总结 综上所述,Apache Commons Lang 库内的 `StringUtils` 已成为众多 Java 开发人员日常工作中不可或缺的好帮手之一。凭借其丰富的预设算法集合,极大地减少了重复编码量的同时提高了程序可读性和稳定性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

智能体格

你的鼓将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值