package com.hanchao.test; import org.apache.commons.lang.StringUtils; /** * @author liweihan (liweihan@sohu-inc.com) * @version 1.0 (2016年1月15日 上午11:01:31) */ public class StringsUtilsTest { public static void main(String[] args) { /** * org.apache.commons.lang.StringUtils中方法的操做對象是java.lang.String類型的對象, * 是JDK提供的String類型操做方法的補充,而且是null安全的 * 即若是輸入參數String爲null則不會拋出NullPointerException,而是作了相應處理。 * * 都是靜態方法。經常使用的有如下一些: */ /** * 1.public static boolean isEmpty(String str) * 判斷某字符串是否爲空,爲空的標準是str == null 或 str.length() == 0 */ System.out.println(StringUtils.isEmpty(null));//true System.out.println(StringUtils.isEmpty(""));//true System.out.println(StringUtils.isEmpty(" "));//false System.out.println(StringUtils.isEmpty("aa"));//false System.out.println(StringUtils.isEmpty(" aa "));//false System.out.println("1=========================="); /** * 2.public static boolean isNotEmpty(String str) * 判斷某字符串是否非空,等於!isEmpty(String str) */ System.out.println(StringUtils.isNotEmpty(null));//false System.out.println(StringUtils.isNotEmpty(""));//false System.out.println(StringUtils.isNotEmpty(" "));//true System.out.println(StringUtils.isNotEmpty("aa"));//true System.out.println(StringUtils.isNotEmpty(" aa "));//true System.out.println("2=========================="); /** * 3.public static boolean isBlank(String str) * 判斷某字符串是否爲空或長度爲0或由空白符(whitespace)構成 * 即 str = null or str.length = 0 or str.trim().length = 0 * 它和isEmpty的區別是都是空字符串時,它認爲是isBlank(即true),isEmpty返回false! */ System.out.println(StringUtils.isBlank(null));//true System.out.println(StringUtils.isBlank(""));//true System.out.println(StringUtils.isBlank(" "));//true [注意isEmpty()是false] System.out.println(StringUtils.isBlank("aa"));//false System.out.println(StringUtils.isBlank(" aa "));//false System.out.println("3=========================="); /** * 4. public static boolean isNotBlank(String str) * 判斷某字符串是否不爲空且長度不爲0且不禁空白符(whitespace)構成, * 等於!isBlank(String str) */ System.out.println(StringUtils.isNotBlank(null));//false System.out.println(StringUtils.isNotBlank(""));//false System.out.println(StringUtils.isNotBlank(" "));//false [注意isNotEmpty()是true] System.out.println(StringUtils.isNotBlank("aa"));//true System.out.println(StringUtils.isNotBlank(" aa "));//true System.out.println("4=========================="); /** * 5.public static String trim(String str) * 去掉字符串兩端的控制符(control characters, char <= 32) * 若是輸入爲null則返回null */ System.out.println(StringUtils.trim(null)); //null System.out.println(StringUtils.trim(""));// System.out.println(StringUtils.trim(" "));// System.out.println(StringUtils.trim(" \b \t \n \f \r "));// System.out.println(StringUtils.trim(" \n\tss"));//ss System.out.println(StringUtils.trim(" ss "));//ss System.out.println("5=========================="); /** * 6.public static String trimToNull(String str) * 去掉字符串兩端的控制符(control characters, char <= 32) * 若是變爲null或"",則返回null */ System.out.println(StringUtils.trimToNull(null)); //null System.out.println(StringUtils.trimToNull(""));//null System.out.println(StringUtils.trimToNull(" "));//null System.out.println(StringUtils.trimToNull(" \b \t \n \f \r "));//null System.out.println(StringUtils.trimToNull(" \n\tss \b"));//ss System.out.println(StringUtils.trimToNull(" ss "));//ss System.out.println("6=========================="); /** * 7.public static String trimToEmpty(String str) * 去掉字符串兩端的控制符(control characters, char <= 32) * 若是變爲null或"",則返回"" */ System.out.println(StringUtils.trimToEmpty(null)); // System.out.println(StringUtils.trimToEmpty(""));// System.out.println(StringUtils.trimToEmpty(" "));// System.out.println(StringUtils.trimToEmpty(" \b \t \n \f \r "));// System.out.println(StringUtils.trimToEmpty(" \n\tss \b"));//ss System.out.println(StringUtils.trimToEmpty(" ss "));//ss System.out.println("7=========================="); /** * 8.public static boolean equals(String str1, String str2) * 比較兩個字符串是否相等,若是兩個均爲空則也認爲相等 */ System.out.println(StringUtils.equals("", ""));//true System.out.println(StringUtils.equals(null, null));//true System.out.println(StringUtils.equals("ss", null));//false System.out.println(StringUtils.equals("AA", "aa"));//false System.out.println("8=========================="); /** * 9.public static boolean equalsIgnoreCase(String str1, String str2) * 比較兩個字符串是否相等,不區分大小寫,若是兩個均爲空則也認爲相等 */ System.out.println(StringUtils.equalsIgnoreCase("", ""));//true System.out.println(StringUtils.equalsIgnoreCase(null, null));//true System.out.println(StringUtils.equalsIgnoreCase("ss", null));//false System.out.println(StringUtils.equalsIgnoreCase("AA", "aa"));//true System.out.println("9=========================="); /** * 10.public static int indexOf(String str, char searchChar) * 返回字符searchChar在字符串str中第一次出現的位置。 * 若是searchChar沒有在str中出現則返回-1, * 若是str爲null或"",則也返回-1 * * 說明:同理! public static int lastIndexOf(String str, char searchChar) */ System.out.println(StringUtils.indexOf("abcdefg", 'b'));//1 System.out.println(StringUtils.indexOf(null, 'b'));//-1 System.out.println(StringUtils.indexOf("", 'a'));//-1 System.out.println("10=========================="); /** * 11.public static int indexOf(String str, char searchChar, int startPos) * 返回字符searchChar從startPos開始在字符串str中第一次出現的位置。 * 若是從startPos開始searchChar沒有在str中出現則返回-1, * 若是str爲null或"",則也返回-1 * * 說明:同理! * public static int lastIndexOf(String str, char searchChar, int startPos) */ System.out.println(StringUtils.indexOf("abcdefbg", 'b',2));//6 System.out.println(StringUtils.indexOf(null, 'b',2));//-1 System.out.println(StringUtils.indexOf("", 'a',2));//-1 System.out.println("11=========================="); /** * 12.public static int indexOf(String str, String searchStr) * 返回字符串searchStr在字符串str中第一次出現的位置。 若是str爲null或searchStr爲null則返回-1, 若是searchStr爲"",且str爲不爲null,則返回0,☆ 若是searchStr不在str中,則返回 說明:同理! public static int lastIndexOf(String str, String searchStr) */ System.out.println(StringUtils.indexOf("abcdefg", "b"));//1 System.out.println(StringUtils.indexOf("abcdefg", "cde"));//2 System.out.println(StringUtils.indexOf("abcdefg", null));//-1 System.out.println(StringUtils.indexOf(null, "aa"));//-1 System.out.println(StringUtils.indexOf("abcdefg", ""));//0 System.out.println("12=========================="); /** * 13.public static int indexOf(String str, String searchStr, int startPos) * 返回字符串searchStr從startPos開始在字符串str中第一次出現的位置。 * 若是searchStr爲"",且str爲不爲null,則返回startPos * 若是startPos爲負數或0,則默認是第一次出現的位置 * * 說明:同理! * public static int lastIndexOf(String str, String searchStr, int startPos) */ System.out.println(StringUtils.indexOf("abcdefg", "b",1));//1 System.out.println(StringUtils.indexOf("abcdefgcde", "cde",1));//2 System.out.println(StringUtils.indexOf("abcdefgcde", "d",-1));//3 System.out.println(StringUtils.indexOf("abcdefgcde", "d",0));//3 System.out.println(StringUtils.indexOf("abcdefgcde", "d",20));//-1 System.out.println(StringUtils.indexOf("abcdefg", null,2));//-1 System.out.println(StringUtils.indexOf(null, "aa",2));//-1 System.out.println(StringUtils.indexOf("abcdefg", "",2));//2 System.out.println("13=========================="); /** * 14.public static int ordinalIndexOf(String str, String searchStr, int ordinal) 返回字符串searchStr在字符串str中第ordinal次出現的位置。 若是str=null或searchStr=null或ordinal<=0則返回-1 */ System.out.println(StringUtils.ordinalIndexOf(null, "aa", 2));//-1 System.out.println(StringUtils.ordinalIndexOf("abcdefgabc", null, 2));//-1 System.out.println(StringUtils.ordinalIndexOf("abcdefgabc", "aa", -2));//-1 System.out.println(StringUtils.ordinalIndexOf("", "", 2));//0 System.out.println(StringUtils.ordinalIndexOf("abcdefabc", "ab", 1));//0 System.out.println(StringUtils.ordinalIndexOf("abcdefabc", "ab", 2));//6 System.out.println(StringUtils.ordinalIndexOf("abcdefabc", "a", 1));//0 System.out.println(StringUtils.ordinalIndexOf("abcdefabc", "a", 2));//6 System.out.println(StringUtils.ordinalIndexOf("", "aa", 2));//-1 System.out.println(StringUtils.ordinalIndexOf("aaaaa", "", 1));//0 System.out.println("14=========================="); /** * 15.public static boolean contains(String str, char searchChar) * 判斷字符串str中是否包含字符searchChar。 若是str爲null或"",返回false; 若是searchChar不在str中,返回false。 */ System.out.println(StringUtils.contains("abc", 'a'));//true System.out.println(StringUtils.contains(null, 'a'));//false System.out.println(StringUtils.contains("", 'a'));//false System.out.println("15=========================="); /** * 16.public static boolean contains(String str, String searchStr) * 判斷字符串str是否包含字符串searchStr。 * 若是str爲null或searchStr爲null,返回false * 若是str爲"",而且searchStr爲"",返回true * * 說明:同理!!(只是不區分大小寫!) * public static boolean containsIgnoreCase(String str, String searchStr) */ System.out.println(StringUtils.contains("abc", "ab"));//true System.out.println(StringUtils.contains("abc", "ac"));//false System.out.println(StringUtils.contains(null, "ac"));//false System.out.println(StringUtils.contains("abc", null));//false System.out.println(StringUtils.contains("", ""));//true System.out.println("16=========================="); /** * 17.public static String substring(String str, int start) * 獲得字符串str的子串。 若是start小於0,位置是從後往前數的第|start|個 若是str爲null或"",則返回它自己 */ System.out.println(StringUtils.substring(null, 1));//null System.out.println(StringUtils.substring("", 1));// System.out.println(StringUtils.substring("asdf", 0));//asdf System.out.println(StringUtils.substring("asdf", 1));//sdf System.out.println(StringUtils.substring("asdf", 3));//f System.out.println(StringUtils.substring("asdf", -1));//f System.out.println(StringUtils.substring("asdf", -2));//df System.out.println(StringUtils.substring("asdf", -3));//sdf System.out.println(StringUtils.substring("asdf", -8));//asdf System.out.println("17=========================="); /** * 18.public static String substring(String str, int start, int end) 獲得字符串str的子串。 若是start小於0,位置是從後往前數的第|start|個, 若是end小於0,位置是從後往前數的第|end|個, 若是str爲null或"",則返回它自己 */ System.out.println(StringUtils.substring(null, 0,1));//null System.out.println(StringUtils.substring("", 0,1));// System.out.println(StringUtils.substring("asdf", 0,2));//as System.out.println(StringUtils.substring("asdf", 0,-1));//asd System.out.println(StringUtils.substring("asdf", 2,-1));//d System.out.println(StringUtils.substring("asdf", 2,-2));// System.out.println(StringUtils.substring("asdf", 3,2));// System.out.println(StringUtils.substring("asdf", -1,-3));// System.out.println(StringUtils.substring("asdf", -3,-1));//sd System.out.println(StringUtils.substring("asdf", -8,5));//asdf System.out.println("18=========================="); /** * 19.public static String mid(String str, int pos, int len) * 獲得字符串str從pos開始len長度的子串。 若是str爲null或爲"",則返回它自己 若是len小於0或pos大於srt的長度,則返回"" 若是pos小於0,則pos設爲0 */ System.out.println(StringUtils.mid(null, 0, 1));//null System.out.println(StringUtils.mid("", 0, 1));// System.out.println(StringUtils.mid("abc", 0, -1));// System.out.println(StringUtils.mid("abc", 6, 1));// System.out.println(StringUtils.mid("abcd", 0, 2));//ab System.out.println(StringUtils.mid("abcd", -3, 2));//ab System.out.println("19=========================="); /** * 20.public static String join(Object[] array) * 把數組中的元素鏈接成一個字符串返回。 */ System.out.println(StringUtils.join(null)); //null System.out.println(StringUtils.join(new String[]{}));// System.out.println(StringUtils.join(new String[]{"aa","bb"}));//aabb System.out.println("20=========================="); /** * 21. public static int countMatches(String str, String sub) * 計算字符串sub在字符串str中出現的次數。 若是str爲null或"",則返回0 */ System.out.println(StringUtils.countMatches(null, "a"));//0 System.out.println(StringUtils.countMatches("", "a"));//0 System.out.println(StringUtils.countMatches("abcd", "a"));//1 System.out.println(StringUtils.countMatches("abcdabc", "a"));//2 System.out.println(StringUtils.countMatches("abcd", "f"));//0 System.out.println(StringUtils.countMatches("abc ", ""));//0 System.out.println(StringUtils.countMatches("abc ", " "));//1 System.out.println(StringUtils.countMatches("abc", null));//0 System.out.println("21=========================="); /** * 22.public static String swapCase(String str) * 把字符串中的字符大寫轉換爲小寫,小寫轉換爲大寫。 */ System.out.println(StringUtils.swapCase(null));//null System.out.println(StringUtils.swapCase(""));// System.out.println(StringUtils.swapCase("Hello tom!"));//hELLO TOM! System.out.println("22=========================="); /** * 23.public static String repeat(String str, int repeat) * 重複字符串repeat次,組合成一個新串返回。 * 若是字符串str爲null或"",則返回它自己 * 若是repeat小於0,則返回"" */ System.out.println(StringUtils.repeat(null, 2));//null System.out.println(StringUtils.repeat("", 3));// System.out.println(StringUtils.repeat("a", 3));//aaa System.out.println(StringUtils.repeat("abc", 2));//abcabc System.out.println(StringUtils.repeat("a", -2));// System.out.println("23=========================="); /** * 24.public static String replace(String text, String repl, String with) * 在字符串text中用with代替repl,替換全部 */ System.out.println(StringUtils.replace(null, "ab", "xx")); //null System.out.println(StringUtils.replace("abcdef", null, "xx"));//abcdef System.out.println(StringUtils.replace("abcdef", "ab", null));//abcdef System.out.println(StringUtils.replace("abcdefab,hanchao","ab","xxx"));//xxxcdefxxx,hanchao System.out.println("24=========================="); /** * 參考:下面文章比較全面! * http://blog.sina.com.cn/s/blog_4550f3ca0100qrsd.html */ } }