斷點

          每天進步一點點!
          posts - 174, comments - 56, trackbacks - 0, articles - 21

          StringUtils

          Posted on 2010-05-30 10:46 斷點 閱讀(632) 評論(0)  編輯  收藏 所屬分類: Apache

          --項目中的用法。
          1、StringUtils.join
          List dwVoListInTab = this.prodService.getPicTabVOList(picId);
          List dwNameListInTab = new ArrayList();
                  for (PrdTabVO dwVo : dwVoListInTab) {
                    String dwName = this.prodService.cvtTabNo2DWName(useProdNo, picId,
                      dwVo.getCTabNo());
                    dwNameListInTab.add(dwName);
                    dwNameMap.put(dwVo.getCNmeEn(), dwName);
                  }
                  ((List)plyDwNameList).addAll(dwNameListInTab);

          String dwNameListStr = "['" + StringUtils.join(dwNameListInTab.toArray(), "','") + "']";
          其它:
          StringUtils.join(new String[]{"cat","dog","carrot","leaf","door"}, ":")
          // cat:dog:carrot:leaf:door

          2、StringUtils.isNotEmpty    //Checks if a String is not empty ("") and not null.

          if (StringUtils.isNotEmpty(onload)) 
                    onload = sub.replace(onload);
                  else {
                    onload = "";
                  }

          public String replace(char oldChar,char newChar)返回一個新的字符串,它是通過用 newChar 替換此字符串中出現(xiàn)的所有 oldChar 得到的。
          如果 oldChar 在此 String 對象表示的字符序列中沒有出現(xiàn),則返回對此 String 對象的引用。

          3、StringUtils.equals    //StringUtils.equals(null, null)   = true
           if (!StringUtils.equals(prodKindNo, "00")) {
          }

          4、StringUtils.isBlank     //Checks if a String is whitespace, empty ("") or null.
          if (StringUtils.isBlank(taskId))
                  jsBuffer.append("var taskId='';\n");
                else {
                  jsBuffer.append("var taskId='" + taskId + "';\n");
                }

           
          5、StringUtils.leftPad(String str, int size,String padStr) --左填充
          //投保年度【保險起期 - 初登年月】 單位:年
          String ply_year = StringUtils.leftPad(String.valueOf(DateUtils.compareYear(regDate,base.getTInsrncBgnTm())), 2, '0'); 
           
          /**
          4260         * <p>Left pad a String with a specified String.</p>
          4261         *
          4262         * <p>Pad to a size of <code>size</code>.</p>
          4263         *
          4264         * <pre>
          4265         * StringUtils.leftPad(null, *, *)      = null
          4266         * StringUtils.leftPad("", 3, "z")      = "zzz"
          4267         * StringUtils.leftPad("bat", 3, "yz")  = "bat"
          4268         * StringUtils.leftPad("bat", 5, "yz")  = "yzbat"
          4269         * StringUtils.leftPad("bat", 8, "yz")  = "yzyzybat"
          4270         * StringUtils.leftPad("bat", 1, "yz")  = "bat"
          4271         * StringUtils.leftPad("bat", -1, "yz") = "bat"
          4272         * StringUtils.leftPad("bat", 5, null)  = "  bat"
          4273         * StringUtils.leftPad("bat", 5, "")    = "  bat"
          4274         * </pre>
          4275         *
          4276         * @param str  the String to pad out, may be null
          4277         * @param size  the size to pad to
          4278         * @param padStr  the String to pad with, null or empty treated as single space
          4279         * @return left padded String or original String if no padding is necessary,
          4280         *  <code>null</code> if null String input
          4281         */
          4282        public static String leftPad(String str, int size, String padStr) {
          4283            if (str == null) {
          4284                return null;
          4285            }
          4286            if (isEmpty(padStr)) {
          4287                padStr = " ";
          4288            }
          4289            int padLen = padStr.length();
          4290            int strLen = str.length();
          4291            int pads = size - strLen;
          4292            if (pads <= 0) {
          4293                return str; // returns original String when possible
          4294            }
          4295            if (padLen == 1 && pads <= PAD_LIMIT) {
          4296                return leftPad(str, size, padStr.charAt(0));
          4297            }
          4298   
          4299            if (pads == padLen) {
          4300                return padStr.concat(str);
          4301            } else if (pads < padLen) {
          4302                return padStr.substring(0, pads).concat(str);
          4303            } else {
          4304                char[] padding = new char[pads];
          4305                char[] padChars = padStr.toCharArray();
          4306                for (int i = 0; i < pads; i++) {
          4307                    padding[i] = padChars[i % padLen];
          4308                }
          4309                return new String(padding).concat(str);
          4310            }
          4311        }


          ---------------------------------------------


          詳見:http://commons.apache.org/lang/api/org/apache/commons/lang/StringUtils.html

          isEmpty
          public static boolean isEmpty(CharSequence str)
          Checks if a String is empty ("") or null.

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

          NOTE: This method changed in Lang version 2.0. It no longer trims the String. That

          functionality is available in isBlank().

           

          Parameters:
          str - the String to check, may be null
          Returns:
          true if the String is empty or null


          isNotEmpty
          public static boolean isNotEmpty(CharSequence str)
          Checks if a 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

           

          Parameters:
          str - the String to check, may be null
          Returns:
          true if the String is not empty and not null


          isBlank
          public static boolean isBlank(CharSequence str)
          Checks if a String is whitespace, empty ("") or null.

           StringUtils.isBlank(null)      = true
          StringUtils.isBlank("")        = true
          StringUtils.isBlank(" ")       = true
          StringUtils.isBlank("bob")     = false
          StringUtils.isBlank("  bob  ") = false

           

          Parameters:
          str - the String to check, may be null
          Returns:
          true if the String is null, empty or whitespace
          Since:
          2.0


          isNotBlank
          public static boolean isNotBlank(CharSequence str)
          Checks if a String is not empty (""), not null and not whitespace only.

           StringUtils.isNotBlank(null)      = false
          StringUtils.isNotBlank("")        = false
          StringUtils.isNotBlank(" ")       = false
          StringUtils.isNotBlank("bob")     = true
          StringUtils.isNotBlank("  bob  ") = true

           

          Parameters:
          str - the String to check, may be null
          Returns:
          true if the String is not empty and not null and not whitespace
          Since:
          2.0


          trim
          public static String trim(String str)
          Removes control characters (char <= 32) from both ends of this String, handling null by

          returning null.

          The String is trimmed using String.trim(). Trim removes start and end characters <= 32.

          To strip whitespace use strip(String).

          To trim your choice of characters, use the strip(String, String) methods.

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

           

          Parameters:
          str - the String to be trimmed, may be null
          Returns:
          the trimmed string, null if null String input


          trimToNull
          public static String trimToNull(String str)
          Removes control characters (char <= 32) from both ends of this String returning null if

          the String is empty ("") after the trim or if it is null.

          The String is trimmed using String.trim(). Trim removes start and end characters <= 32.

          To strip whitespace use stripToNull(String).

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

           

          Parameters:
          str - the String to be trimmed, may be null
          Returns:
          the trimmed String, null if only chars <= 32, empty or null String input
          Since:
          2.0


          trimToEmpty
          public static String trimToEmpty(String str)
          Removes control characters (char <= 32) from both ends of this String returning an empty

          String ("") if the String is empty ("") after the trim or if it is null.

          The String is trimmed using String.trim(). Trim removes start and end characters <= 32.

          To strip whitespace use stripToEmpty(String).

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

           

          Parameters:
          str - the String to be trimmed, may be null
          Returns:
          the trimmed String, or an empty String if null input
          Since:
          2.0
          equals
          public static boolean equals(String str1,
          String str2)
          Compares two Strings, returning true if they are equal.

          nulls are handled without exceptions. Two null references are considered to be equal. The

          comparison is case sensitive.

           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

           

          Parameters:
          str1 - the first String, may be null
          str2 - the second String, may be null
          Returns:
          true if the Strings are equal, case sensitive, or both null
          See Also:
          String.equals(Object) 
           
          startsWith
          public static boolean startsWith(String str,
          String prefix)
          Check if a String starts with a specified prefix.

          nulls are handled without exceptions. Two null references are considered to be equal. The

          comparison is case sensitive.

           StringUtils.startsWith(null, null)      = true
          StringUtils.startsWith(null, "abc")     = false
          StringUtils.startsWith("abcdef", null)  = false
          StringUtils.startsWith("abcdef", "abc") = true
          StringUtils.startsWith("ABCDEF", "abc") = false

           

          Parameters:
          str - the String to check, may be null
          prefix - the prefix to find, may be null
          Returns:
          true if the String starts with the prefix, case sensitive, or both null
          Since:
          2.4
          See Also:
          String.startsWith(String)


          1.去除尾部換行符,使用函數(shù):StringUtils.chomp(testString)
          函數(shù)介紹:去除testString尾部的換行符
          例程:
          String input = "Hello\n";  
          System.out.println( StringUtils.chomp( input ));  
          String input2 = "Another test\r\n";  
          System.out.println( StringUtils.chomp( input2 )); 

          輸出如下:
              Hello
              Another test


          2.判斷字符串內(nèi)容的類型,函數(shù)介紹:
          StringUtils.isNumeric( testString ) :如果testString全由數(shù)字組成返回True
          StringUtils.isAlpha( testString ) :如果testString全由字母組成返回True
          StringUtils.isAlphanumeric( testString ) :如果testString全由數(shù)字或數(shù)字組成返回True
          StringUtils.isAlphaspace( testString ) :如果testString全由字母或空格組成返回True

          例程:
          String state = "Virginia";  
          System.out.println( "Is state number? " + StringUtils.isNumeric(state ) );  
          System.out.println( "Is state alpha? " + StringUtils.isAlpha( state ));  
          System.out.println( "Is state alphanumeric? " +StringUtils.isAlphanumeric( state ) );  
          System.out.println( "Is state alphaspace? " + StringUtils.isAlphaSpace( state ) ); 

          輸出如下:
              Is state number? false
              Is state alpha? true
              Is state alphanumeric? true
              Is state alphaspace? true


          3.查找嵌套字符串,使用函數(shù):
          StringUtils.substringBetween(testString,header,tail)
          函數(shù)介紹:在testString中取得header和tail之間的字符串。不存在則返回空
          例程:
          String htmlContent = "ABC1234ABC4567";  
          System.out.println(StringUtils.substringBetween(htmlContent, "1234", "4567"));  
          System.out.println(StringUtils.substringBetween(htmlContent, "12345", "4567")); 

          輸出如下:
              ABC
              null


          4.顛倒字符串,使用函數(shù):StringUtils.reverse(testString)
          函數(shù)介紹:得到testString中字符顛倒后的字符串
          例程:
          System.out.println( StringUtils.reverse("ABCDE")); 

          輸出如下:
              EDCBA


          5.部分截取字符串,使用函數(shù):
          StringUtils.substringBetween(testString,fromString,toString ):取得兩字符之間的字符串
          StringUtils.substringAfter( ):取得指定字符串后的字符串
          StringUtils.substringBefore( ):取得指定字符串之前的字符串
          StringUtils.substringBeforeLast( ):取得最后一個指定字符串之前的字符串
          StringUtils.substringAfterLast( ):取得最后一個指定字符串之后的字符串

          函數(shù)介紹:上面應該都講明白了吧。
          例程:
          String formatted = " 25 * (30,40) [50,60] | 30";  
          System.out.print("N0: " + StringUtils.substringBeforeLast( formatted, "*" ) );  
          System.out.print(", N1: " + StringUtils.substringBetween( formatted, "(", "," ) );  
          System.out.print(", N2: " + StringUtils.substringBetween( formatted, ",", ")" ) );  
          System.out.print(", N3: " + StringUtils.substringBetween( formatted, "[", "," ) );  
          System.out.print(", N4: " + StringUtils.substringBetween( formatted, ",", "]" ) );  
          System.out.print(", N5: " + StringUtils.substringAfterLast( formatted, "|" ) ); 

          輸出如下:
              N0: 25 , N1: 30, N2: 40, N3: 50, N4: 40) [50,60, N5: 30


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 五河县| 洞口县| 泰来县| 浑源县| 庆阳市| 清远市| 论坛| 华安县| 定边县| 安新县| 伊金霍洛旗| 宜宾县| 论坛| 瑞丽市| 玛沁县| 金阳县| 丰都县| 贺州市| 舒城县| 龙江县| 晋江市| 化德县| 喀什市| 南和县| 汉中市| 巴东县| 海盐县| 金秀| 灌南县| 裕民县| 麟游县| 闵行区| 沈丘县| 大港区| 湛江市| 玉田县| 漯河市| 昌都县| 海阳市| 荥阳市| 鹿邑县|