今天在编码的过程中,对于null,采用==null进行判断。并且为了过滤"",使用了str.trim().length()==0,当str为null时,报空指针异常。
于是决定使用Apache的commons-lang,简化代码的同时也减少程序的bug。现对学习内容进行总结。
首先导入commons-lang包,为方便测试,导入Junit进行单元测试。代码如下:
public class Main { Logger logger = Logger.getLogger(Main.class.getName()); public static void main(String[] args) { System.out.println("Hello World!"); } @Test public void equalsTest() { System.out.println(StringUtils.equals("he", "he")); //T System.out.println(StringUtils.equals("he", "ho")); //F System.out.println(StringUtils.equals("he", "HE")); //F System.out.println(StringUtils.equalsIgnoreCase("he", "he")); //T System.out.println(StringUtils.equalsIgnoreCase("he", "ho")); //F System.out.println(StringUtils.equalsIgnoreCase("he", "HE")); //T } @Test public void testIsEmpty(){ System.out.println(StringUtils.isEmpty(null)); //T System.out.println(StringUtils.isEmpty("")); //T System.out.println(StringUtils.isEmpty(" ")); //F System.out.println(StringUtils.isEmpty("bob")); //F System.out.println(StringUtils.isEmpty(" bob ")); //F } @Test public void testIsNotEmpty(){ System.out.println(StringUtils.isNotEmpty(null)); //F System.out.println(StringUtils.isNotEmpty("")); //F System.out.println(StringUtils.isNotEmpty(" ")); //T System.out.println(StringUtils.isNotEmpty("bob")); //T System.out.println(StringUtils.isNotEmpty(" bob ")); //T } @Test public void testIsBlank(){ System.out.println(StringUtils.isBlank(null)); // T System.out.println(StringUtils.isBlank("")); // T System.out.println(StringUtils.isBlank(" ")); // T System.out.println(StringUtils.isBlank("bob")); //F System.out.println(StringUtils.isBlank(" bob ")); //F } @Test public void testIsNotBlank(){ System.out.println(StringUtils.isNotBlank(null)); //F System.out.println(StringUtils.isNotBlank("")); //F System.out.println(StringUtils.isNotBlank(" ")); //F System.out.println(StringUtils.isNotBlank("bob")); // T System.out.println(StringUtils.isNotBlank(" bob ")); // T } //public static String[] split(String str,String separatorChars) @Test public void testSplit() { //默认半角空格分割 String str1 = "aaa bbb ccc"; String[] dim1 = StringUtils.split(str1); // => ["aaa", "bbb", "ccc"] for(int i = 0;i<dim1.length;i++) { System.out.println(dim1[i]); // } String contrivedExampleString = "one.two.three.four"; String[] result = contrivedExampleString.split("."); System.out.println(result.length); // 0 //指定分隔符 String[] res= StringUtils.split(contrivedExampleString,"."); for(int i = 0;i<res.length;i++) { System.out.println(res[i]); // } //去除空字符串 String str3 = "aaa,,bbb"; String[] dim3 = StringUtils.split(str3, ","); // => ["aaa", "bbb"] for(int i = 0;i<dim3.length;i++) { System.out.println(dim3[i]); // } //包含空字符串 String str4 = "aaa,,bbb"; String[] dim4 = StringUtils.splitPreserveAllTokens(str4, ","); // => ["aaa", "", "bbb"] System.out.println(dim4.length);//3 } @Test public void testJoin() { String[] numbers = {"one", "two", "three"}; String numberStr= StringUtils.join(numbers,","); System.out.println(numberStr); // returns "one,two,three" } @Test public void trimTest() { System.out.println(StringUtils.trim(null)); // null System.out.println(StringUtils.trim("")); // "" System.out.println(StringUtils.trim(" ")); // "" System.out.println(StringUtils.trim("abc")); // "abc" System.out.println(StringUtils.trim(" abc")); // "abc" System.out.println(StringUtils.trim(" abc ")); // "abc" System.out.println(StringUtils.trim(" ab c ")); // "ab c" } @Test public void stripTest() { System.out.println(StringUtils.strip(null)); // null System.out.println(StringUtils.strip("")); // "" System.out.println(StringUtils.strip(" ")); // "" System.out.println(StringUtils.strip("abc")); // "abc" System.out.println(StringUtils.strip(" abc")); // "abc" System.out.println(StringUtils.strip("abc ")); // "abc" System.out.println(StringUtils.strip(" abc ")); // "abc" System.out.println(StringUtils.strip(" ab c ")); // "ab c" } @Test public void testCountMatches() { int nCount = StringUtils.countMatches("UPDATE tb_table SET xx=?,xyz=?, sss=? WHERE id=?", "?"); System.out.println(nCount); //4 } @Test public void reverseTest() { String str = "hello"; String res= StringUtils.reverse(str); //olleh System.out.println(res); } @Test public void repeatTest() { String str = "hello"; String res1= StringUtils.repeat(str, 3); //hellohellohello String res2= StringUtils.repeat(str,",",3); //hello,hello,hello System.out.println(res1); System.out.println(res2); } }
参考文献
commons-lang3-3.4-srcsrc est
http://www.cnblogs.com/ITtangtang/p/3966955.html
http://ray-yui.iteye.com/blog/1958319