斷點

          每天進步一點點!
          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 替換此字符串中出現的所有 oldChar 得到的。
          如果 oldChar 在此 String 對象表示的字符序列中沒有出現,則返回對此 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.去除尾部換行符,使用函數:StringUtils.chomp(testString)
          函數介紹:去除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.判斷字符串內容的類型,函數介紹:
          StringUtils.isNumeric( testString ) :如果testString全由數字組成返回True
          StringUtils.isAlpha( testString ) :如果testString全由字母組成返回True
          StringUtils.isAlphanumeric( testString ) :如果testString全由數字或數字組成返回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.查找嵌套字符串,使用函數:
          StringUtils.substringBetween(testString,header,tail)
          函數介紹:在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.顛倒字符串,使用函數:StringUtils.reverse(testString)
          函數介紹:得到testString中字符顛倒后的字符串
          例程:
          System.out.println( StringUtils.reverse("ABCDE")); 

          輸出如下:
              EDCBA


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

          函數介紹:上面應該都講明白了吧。
          例程:
          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


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


          網站導航:
           
          主站蜘蛛池模板: 法库县| 普陀区| 巩义市| 七台河市| 奉新县| 晋城| 尼玛县| 岳阳市| 开远市| 历史| 宁国市| 岑溪市| 马尔康县| 清水县| 西宁市| 青海省| 盐城市| 乌海市| 晋中市| 中宁县| 梅州市| 建宁县| 静乐县| 榆林市| 黄冈市| 怀仁县| 衡山县| 卢湾区| 曲靖市| 东山县| 丽江市| 呼伦贝尔市| 沧州市| 万安县| 曲水县| 环江| 济阳县| 新野县| 麻栗坡县| 城固县| 乌苏市|