StringUtils API(字符串工具類)

Maven依賴:

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
	<groupId>org.apache.commons</groupId>
	<artifactId>commons-lang3</artifactId>
	<version>3.10</version>
</dependency>

API

  1. IsEmpty/IsBlank - checks if a String contains text
    檢查字符串是否有內容
    blank:
    空白的;(文件等的)空白處,空格;
    empty:
    空的;空的東西

    String s = "This is a test statement";
    String blank = " ";
    String empty = "";
    
    System.out.println(StringUtils.isBlank(s));         //false
    System.out.println(StringUtils.isBlank(blank));     //true
    System.out.println(StringUtils.isBlank(empty));     //true
    System.out.println(StringUtils.isNotBlank(s));      //true
    System.out.println(StringUtils.isNotBlank(blank));  //false
    System.out.println(StringUtils.isNotBlank(empty));  //false
    System.out.println(StringUtils.isEmpty(s));         //flase
    System.out.println(StringUtils.isEmpty(blank));     //false
    System.out.println(StringUtils.isEmpty(empty));     //true
    System.out.println(StringUtils.isNotEmpty(s));      //true
    System.out.println(StringUtils.isNotEmpty(blank));  //true
    System.out.println(StringUtils.isNotEmpty(empty));  //false
    
  2. Trim/Strip - removes leading and trailing whitespace
    刪除字符串開頭和結尾的空白符。
    trim:
    修剪;除去(不必要部分)
    strip:
    剝去(外皮);從(某處)拿走所有東西

    String s = "  This is a test statement  ";
    String empty = "";
    System.out.println(s.length());//28
    System.out.println(StringUtils.trim(s)+"-"+s.length());//This is a test statement-28
    System.out.println(StringUtils.strip(s)+"-"+s.length());//This is a test statement-28
    System.out.println(StringUtils.stripToEmpty(empty));    //
    System.out.println(StringUtils.stripToNull(null));      //null
    System.out.println(StringUtils.trimToEmpty(empty));     //
    System.out.println(StringUtils.trimToNull(null));   //null
    
  3. Equals/Compare - compares two strings null-safe
    比較兩個字符串null安全。
    quals()返回boolean compare()返回相差的字符數

    String s = "This is a test statement";
    String s1 = "This is a test statement";
    String empty = "";
    String n = null;
    System.out.println(StringUtils.equals(s,s1));       //true
    System.out.println(StringUtils.compare(s,s1));      //0
    System.out.println(StringUtils.equals(s,empty));    //false
    System.out.println(StringUtils.compare(s,empty));   //24
    System.out.println(StringUtils.equals(s,n));        //false
    System.out.println(StringUtils.compare(s,n));       //1
    System.out.println(StringUtils.equals(empty,n));    //false
    System.out.println(StringUtils.compare(empty,n));   //1
    
  4. startsWith - check if a String starts with a prefix null-safe
    檢查字符串是否以xx前綴開始 空安全

    String s = "This is a test statement";
    System.out.println(StringUtils.startsWith(s,"t"));     //false
    
  5. endsWith - check if a String ends with a suffix null-safe
    檢查字符串是否以xx後綴開始 空安全

    String s = "This is a test statement";
    System.out.println(StringUtils.endsWith(s,"t"));       //true
    
  6. IndexOf/LastIndexOf/Contains - null-safe index-of checks
    null安全的索引檢查。

    String s = "This is a test statement";
    System.out.println(StringUtils.indexOf(s,"i"));      //2
    System.out.println(StringUtils.lastIndexOf(s,"i"));  //5
    System.out.println(StringUtils.contains(s,"i"));     //true
    
  7. IndexOfAny/LastIndexOfAny/IndexOfAnyBut - index-of any of a set of Strings
    字符串集合索引檢查。
    返回數組中的元素最早出現的下標。

    String s = "This is a test statement";
    System.out.println(StringUtils.indexOfAny(s,"e","i"));      //2
    System.out.println(StringUtils.lastIndexOfAny(s,"i","e"));  //21
    System.out.println(StringUtils.indexOfAnyBut(s,"T"));       //1
    
  8. ContainsOnly/ContainsNone/ContainsAny - does String contains only/none/any of these characters
    字符在字符串中出現一次或一次也沒有出現。

    String s = "This is a test statement";
    System.out.println(StringUtils.containsOnly(s,"is"));      //false
    System.out.println(StringUtils.containsNone(s,"hello"));  //fasle
    System.out.println(StringUtils.containsAny(s,"men"));     //true
    
  9. Substring/Left/Right/Mid - null-safe substring extractions
    null安全 子串的提取。

    String s = "Thisisateststatement";
    System.out.println(StringUtils.substring(s,1,6));//[1,6) hisis
    System.out.println(StringUtils.left(s,3));       //Thi
    System.out.println(StringUtils.right(s,3));      //ent
    System.out.println(StringUtils.mid(s,6,3));      //ate
    
  10. SubstringBefore/SubstringAfter/SubstringBetween - substring extraction relative to other strings
    子串提取依賴其它字符串。

    String s = "Thisisateststatement";
    System.out.println(StringUtils.substringBefore(s,"is"));  //Th
    System.out.println(StringUtils.substringAfter(s,"is"));   //isateststatement
    System.out.println(StringUtils.substringBetween(s,"te")); //ststa
    
  11. Split/Join - splits a String into an array of substrings and vice versa
    將字符串分割成子字符串數組,反之亦然

    String s = "This is a";
    String s1 = "";
    String [] str = new String[]{"1","2","3"};
    String[] split = StringUtils.split(s, " ");
    for (int i = 0; i < split.length; i++) {
    	System.out.println(split[i]);//This is a
    }
    System.out.println(s1);
    System.out.println(StringUtils.join(str,"-"));//1-2-3
    
  12. Remove - removes part of a String
    刪除字符串的一部分

    String s = "This is a";
    System.out.println(StringUtils.remove(s,"is"));//Th  a
    System.out.println(s);//This is a
    
  13. Replace/Overlay - Searches a String and replaces one String with another
    替換字符串的部分字符。

    String s = "This is a";
    System.out.println(StringUtils.replace(s,"is","qaz"));//Thqaz qaz a  a
    System.out.println(s);//This is a
    System.out.println(StringUtils.overlay(s,"is",0,4));//[0,4) is is a
    System.out.println(s);//This is a
    
  14. Chop - removes the last part of a String
    刪除字符串最後的字符。

    String s = "This isa";
    System.out.println(StringUtils.chop(s));//This is
    System.out.println(s);                  //This isa
    
  15. AppendIfMissing - appends a suffix to the end of the String if not present
    在字符串末尾追加後綴(如果不存在)

    String s = "This isa";
    System.out.println(StringUtils.appendIfMissing(s,"a"));//This isa
    System.out.println(s);                  //This isa
    System.out.println(StringUtils.appendIfMissing(s,"s"));//This isas
    System.out.println(s);                  //This isa
    
  16. PrependIfMissing - prepends a prefix to the start of the String if not present
    在字符串開頭(如果不存在)前加上一個前綴

    String s = "This isa";
    System.out.println(StringUtils.prependIfMissing(s,"T"));//This isa
    System.out.println(s);                                        //This isa
    System.out.println(StringUtils.prependIfMissing(s,"s"));//sThis isa
    System.out.println(s);                                        //This isa
    
  17. LeftPad/RightPad/Center/Repeat - pads a String
    填補一個字符串

    String s = "101";
    System.out.println(StringUtils.leftPad(s,8,"0"));//00000101
    System.out.println(StringUtils.rightPad(s,8,"0"));//10100000
    System.out.println(StringUtils.center(s,8,"@@@"));//@@101@@@
    System.out.println(StringUtils.repeat(s,"---",3));//101---101---101
    
  18. UpperCase/LowerCase/SwapCase/Capitalize/Uncapitalize - changes the case of a String
    改變字符串的大小寫

    String s = "daW sWSV VfeFRE";
    System.out.println(StringUtils.upperCase(s));//DAW SWSV VFEFRE
    System.out.println(StringUtils.lowerCase(s));//daw swsv vfefre
    System.out.println(StringUtils.swapCase(s));//DAw Swsv vFEfre
    System.out.println(StringUtils.capitalize(s));//首字母大寫 DaW sWSV VfeFRE
    System.out.println(StringUtils.uncapitalize(s));//取消首字母大寫 daW sWSV VfeFRE
    
  19. CountMatches - counts the number of occurrences of one String in another
    計算一個字符串在另一個字符串中的出現次數

    String s = "this is a test statement";
    System.out.println(StringUtils.countMatches(s,"s"));//4
    
  20. IsAlpha/IsNumeric/IsWhitespace/IsAsciiPrintable - checks the characters in a String
    檢查字符串中的字符

    String s = "asds哈哈";
    String s1 = " 2134";
    String s2 = " ";
    String s3 = "\u007F";
    //檢查字符串是否只包含Unicode字母(所有漢字+英文字母,數字和符號大多不包括在內)
    System.out.println(StringUtils.isAlpha(s));//true
    //檢查字符串是否只包含Unicode數字
    System.out.println(StringUtils.isNumeric(s1));//false
    //檢查字符串是否只包含空格
    System.out.println(StringUtils.isWhitespace(s2));//true
    //檢查字符串是否只包含ASCII可打印字符(95個可打印字符(0x20-0x7E))
    System.out.println(StringUtils.isAsciiPrintable(s3));//false
    
  21. DefaultString - protects against a null input String
    防止null輸入字符串

    String s =null;
    System.out.println(StringUtils.defaultString(s,"default"));//default
    
  22. Rotate - rotate (circular shift) a String
    旋轉(循環移位)字符串

    String s ="abcdefg";
    System.out.println(StringUtils.rotate(s,0));//abcdefg
    System.out.println(StringUtils.rotate(s,2));//fgabcde
    System.out.println(StringUtils.rotate(s,-2));//cdefgab
    System.out.println(StringUtils.rotate(s,7));//abcdefg
    System.out.println(StringUtils.rotate(s,-7));//abcdefg
    System.out.println(StringUtils.rotate(s,9));//fgabcde
    System.out.println(StringUtils.rotate(s,-9));//cdefgab
    
  23. Reverse/ReverseDelimited - reverses a String
    反轉字符串

    String s ="abcdefg";
    System.out.println(StringUtils.reverse(s));//gfedcba
    System.out.println(StringUtils.reverseDelimited(s,'d'));//efgdabc
    
  24. Abbreviate - abbreviates a string using ellipsis or another given String
    使用省略號或其他給定字符串縮寫字符串

String s ="據香港特區政府新聞網站消息";
System.out.println(StringUtils.abbreviate(s,"...",8));//據香港特區...
  1. Difference - compares Strings and reports on their differences
    比較字符串和報告的差異
String s = "ab";
System.out.println(StringUtils.difference(s,"abxyz"));//xyz
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章