注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

星期五

明天不上班

 
 
 

日志

 
 
关于我

一个特立独行的Java程序员,比较宅,上上网,写博客,听音乐,看电影。

网易考拉推荐

关于StringUtils的一些练习  

2011-10-10 22:19:01|  分类: Java |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
       下面是一些自己在Eclipse上的简单练习,主要是熟悉StringUtils的API
       (比较重要的用黑体标出来)
       //StringUtils(org.apache.commons.lang.StringUtils)
        String testString0 = null;
        String testString1 = "";
        String testString2 = "This is a test string";
        //isBlank
        System.out.println(StringUtils.isBlank(testString0));
        System.out.println(StringUtils.isBlank(testString1));
        System.out.println(StringUtils.isBlank(testString2));
        //isEmpty
        System.out.println(StringUtils.isEmpty(testString0));
        System.out.println(StringUtils.isEmpty(testString1));
        System.out.println(StringUtils.isEmpty(testString2));
        
        //关于StringUtils中的一些定义
        /*
         null -> null
         empty -> a zero-length string("")
         space -> the space character(' ', char 32)
         whitespace -> the character defined by Character.isWhitespace(char)
         trim - the characters <= 32 as in String.trim()
         */

        
        
        //String 中的方法不是null safe的,相应的StringUtils中方法是null safe的
        //null safe
        
        String testString4 = null;
        String testString5 = "  This is a null safe test";
        //System.out.println(testString4.startsWith("T")); throws java.lang.NullPointerException
        System.out.println(testString5.startsWith("T"));
        System.out.println(StringUtils.startsWith(testString4, "T"));
        System.out.println(StringUtils.startsWith(testString5, "T"));
        //trim test
        //if null is null if empty("") is empty
        System.out.println(StringUtils.trim(testString4));
        //if null or empty is null
        System.out.println(StringUtils.trimToNull(testString4));
        //if null or empty is empty
        System.out.println(StringUtils.trimToEmpty(testString4));
        
        
      /*
        null safe 的方法
        IsEmpty/IsBlank - checks if a String contains text
        Trim/Strip - removes leading and trailing whitespace
        Equals - compares two strings null-safe
        startsWith - check if a String starts with a prefix null-safe
        endsWith - check if a String ends with a suffix null-safe
        IndexOf/LastIndexOf/Contains - null-safe index-of checks
        IndexOfAny/LastIndexOfAny/IndexOfAnyBut/LastIndexOfAnyBut - index-of any of a set of Strings
        ContainsOnly/ContainsNone/ContainsAny - does String contains only/none/any of these characters
        Substring/Left/Right/Mid - null-safe substring extractions
        SubstringBefore/SubstringAfter/SubstringBetween - substring extraction relative to other strings
        Split/Join - splits a String into an array of substrings and vice versa
        Remove/Delete - removes part of a String
        Replace/Overlay - Searches a String and replaces one String with another
        Chomp/Chop - removes the last part of a String
        LeftPad/RightPad/Center/Repeat - pads a String
        UpperCase/LowerCase/SwapCase/Capitalize/Uncapitalize - changes the case of a String
        CountMatches - counts the number of occurrences of one String in another
        IsAlpha/IsNumeric/IsWhitespace/IsAsciiPrintable - checks the characters in a String
        DefaultString - protects against a null input String
        Reverse/ReverseDelimited - reverses a String
        Abbreviate - abbreviates a string using ellipsis
        Difference - compares Strings and reports on their differences
        LevensteinDistance - the number of changes needed to change one String into another
      */
        
       //常用方法详解
       //abbreviate(String str, int maxWidth) Abbreviates a String using ellipse
       //max-3 's substring and ...
       System.out.println(StringUtils.abbreviate("This is a Test String", 10));
       System.out.println(StringUtils.abbreviate("This", 10));
       //abbreviate(String str, int offset, int maxWidth)
       //... + substring(offset, maxWidth) + ...
       System.out.println(StringUtils.abbreviate("This is a Test String", 5, 10));    
       //difference 返回第二个字符串的不同 下面的例子返回edf
       System.out.println(StringUtils.difference("abcdefg", "abcedf"));
       //indexOfDifference 返回索引
       System.out.println(StringUtils.indexOfDifference("abcdefg", "abcedf"));
       //getCommonPrefix 返回共同的前缀字符串
       System.out.println(StringUtils.getCommonPrefix(new String[]{"abc","abcde","abfg"}));
       //startsWith return true or false
       System.out.println(StringUtils.startsWith("abcdef", "abc"));
       //startWidthAny
       System.out.println(StringUtils.startsWithAny("abcdef", new String[]{"abc", "bcd"}));
       //类似的endWidth
       //normalizeSpace
       System.out.println(StringUtils.normalizeSpace(" This is a text "));
       //reverse
       System.out.println(StringUtils.reverse("a_bc_def_gh"));
       //reverseDelimited 和 reverseDelimitedString
       System.out.println(StringUtils.reverseDelimited("a_bc_def_gh", '_'));
       //defaultString if null return the second string
       System.out.println(StringUtils.defaultString(null,null));
       System.out.println(StringUtils.defaultString(null,"This is a null"));
       //一些判断
       /*
        isAllLowerCase 都是小写字母
        isAllUpperCase 都是大写字母
        isAlpha 都是字母
        isAlphaSpace 字母和空格
        isAlphanumeric 字母和数字
        isAlphanumericSpace 字母数字和空格
        isAsciiPrintable
        isNumeric数字
        isNumericSpace数字和空格
        isWhitespace
        */
       //countMatches
       System.out.println(StringUtils.countMatches("abababcdeababyui", "ab"));
       //capitaliseAllWords
       System.out.println(StringUtils.capitaliseAllWords("This is a book"));
       //swapCase
       System.out.println(StringUtils.swapCase("This is a book"));
       //uncapitalize
       System.out.println(StringUtils.uncapitalize("This is a book"));
       
       /*
        capitalise
        lowerCase
        upperCase
        */
       
       /*
           字符串定位
        center
        leftPad
        rightPad
        */
       System.out.println(StringUtils.center("abc", 10, "."));
       System.out.println(StringUtils.leftPad("abc", 10, "."));
       /*
        repeat
        */
       //chop Remove the last character from a String. If the String ends in \r\n, then remove both of them.
       System.out.println(StringUtils.chop("This is a book"));
       System.out.println(StringUtils.chop("This is a book\r\n"));
       System.out.println(StringUtils.chop("This is a book\n"));
       System.out.println(StringUtils.chop("This is a book\r"));
       
       //overlay
       //Overlays part of a String with another String.
       System.out.println(StringUtils.overlay("abcdef", "zzzz", 2, 4));
       //replaceChars
       System.out.println(StringUtils.replaceChars("abcdef", "abc", "///"));
       //replaceEachRepeatedly
       //replaceEach
       System.out.println(StringUtils.replaceEach("abcdef", new String[]{"a","b"}, new String[]{"c","d"}));
       //replace
       System.out.println(StringUtils.replace("abadafa", "a", "_", 2));
       //replaceOnce
       
       //remove
       System.out.println(StringUtils.remove("abcdefg", "c"));
       //removeEndIgnoreCase
       System.out.println(StringUtils.removeEndIgnoreCase("abcdefgC", "c"));
       //removeEnd
       //removeStartIgnoreCase
       //removeStart
       //deleteSpaces
       
       //join
       System.out.println(StringUtils.join(new String[]{"a", "c", "", null, "f"},""));
       //split
       //substringBetween
       System.out.println(StringUtils.substringBetween("<b></b>", "<", ">"));
       //substringsBetween
       System.out.println(Arrays.asList(StringUtils.substringsBetween("<b></b>", "<", ">")));
       System.out.println(Arrays.asList(StringUtils.substringsBetween("<b>This</b>", "<b>", "</b>")));
       //substringAfterLast
       //substringBeforeLast
       System.out.println(StringUtils.substringAfterLast("abcdef", "e"));
       //substringAfter
       //substringBefore
       System.out.println(StringUtils.substringBefore("abcdef", "e"));
       //substring
       
       //indexOf lastIndexOf
       /*
        static int     indexOf(String str, char searchChar)
          Finds the first index within a String, handling null.
        static int     indexOf(String str, char searchChar, int startPos)
          Finds the first index within a String from a start position, handling null.
        static int     indexOf(String str, String searchStr)
          Finds the first index within a String, handling null.
        static int     indexOf(String str, String searchStr, int startPos)
          Finds the first index within a String, handling null.
        static int     indexOfAny(String str, char[] searchChars)
          Search a String to find the first index of any character in the given set of characters.
        static int     indexOfAny(String str, String searchChars)
          Search a String to find the first index of any character in the given set of characters.
        static int     indexOfAny(String str, String[] searchStrs)
          Find the first index of any of a set of potential substrings.
        static int     indexOfAnyBut(String str, char[] searchChars)
          Search a String to find the first index of any character not in the given set of characters.
        static int     indexOfAnyBut(String str, String searchChars)
          Search a String to find the first index of any character not in the given set of characters.
        static int     indexOfDifference(String[] strs)
          Compares all Strings in an array and returns the index at which the Strings begin to differ.
        static int     indexOfDifference(String str1, String str2)
          Compares two Strings, and returns the index at which the Strings begin to differ.
        static int     indexOfIgnoreCase(String str, String searchStr)
          Case in-sensitive find of the first index within a String.
        static int     indexOfIgnoreCase(String str, String searchStr, int startPos)
          Case in-sensitive find of the first index within a String from the specified position.
        
        */
       //contains
       /*
        static boolean     contains(String str, char searchChar)
          Checks if String contains a search character, handling null.
        static boolean     contains(String str, String searchStr)
          Checks if String contains a search String, handling null.
        static boolean     containsAny(String str, char[] searchChars)
          Checks if the String contains any character in the given set of characters.
        static boolean     containsAny(String str, String searchChars)
           Checks if the String contains any character in the given set of characters.
        static boolean     containsIgnoreCase(String str, String searchStr)
          Checks if String contains a search String irrespective of case, handling null.
        static boolean     containsNone(String str, char[] searchChars)
          Checks that the String does not contain certain characters.
        static boolean     containsNone(String str, String invalidChars)
          Checks that the String does not contain certain characters.
        static boolean     containsOnly(String str, char[] valid)
          Checks if the String contains only certain characters.
        static boolean     containsOnly(String str, String validChars)
          Checks if the String contains only certain characters.
        */
  评论这张
 
阅读(1130)| 评论(0)
推荐

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017