Examples of common methods used org.apache.commons.lang.StringUtil

2010-03-26  来源:本站原创  分类:Java  人气:243 

1. An empty string check
Use the function: StringUtils.isBlank (testString)
Function Description: When testString is empty, the length of zero, or only by whitespace (whitespace) the composition, return True; otherwise, returns False
Example:

view plaincopy to clipboardprint?

  1. String test = "";
  2. String test2 = "\ n \ n \ t";
  3. String test3 = null;
  4. String test4 = "Test";
  5. System.out.println ( "test blank?" + StringUtils.isBlank (test));
  6. System.out.println ( "test2 blank?" + StringUtils.isBlank (test2));
  7. System.out.println ( "test3 blank?" + StringUtils.isBlank (test3));
  8. System.out.println ( "test4 blank?" + StringUtils.isBlank (test4));


Output is as follows:
test blank? true
test2 blank? true
test3 blank? true
test4 blank? False
Function StringUtils.isNotBlank (testString) the functions and StringUtils.isBlank (testString) the opposite.

2. Remove whitespace characters <br /> use the function: StringUtils.trimToNull (testString)
Function Description: head and tail removed testString blank characters, if only testString entirely by white space characters
(whitespace) component returns null
Example:

view plaincopy to clipboardprint?

  1. String test1 = "\ t";
  2. String test2 = "A Test";
  3. String test3 = null;
  4. System.out.println ( "test1 trimToNull:" + StringUtils.trimToNull (test1));
  5. System.out.println ( "test2 trimToNull:" + StringUtils.trimToNull (test2));
  6. System.out.println ( "test3 trimToNull:" + StringUtils.trimToNull (test3));


Output is as follows:
test1 trimToNull: null
test2 trimToNull: A Test
test3 trimToNull: null

Note: Function StringUtils.trim (testString) and the
StringUtils.trimToNull (testString) function is similar, but testString by a blank character
(whitespace) is returned when the composition of zero-length string.

3. To obtain a string of abbreviations <br /> use the function: StringUtils.abbreviate (testString, width), and StringUtils.abbreviate (testString, offset, width)
Function introduction: in a given width obtained within testString abbreviation, when testString less than the width of the length of the original string is returned.
Example:

view plaincopy to clipboardprint?

  1. String test = "This is a test of the abbreviation.";
  2. String test2 = "Test";
  3. System.out.println (StringUtils.abbreviate (test, 15));
  4. System.out.println (StringUtils.abbreviate (test, 5,15));
  5. System.out.println (StringUtils.abbreviate (test2, 10));


Output is as follows:
This is a te ...
... is a test ...
Test

4. <br /> Split a string using the function: StringUtils.split (testString, splitChars, arrayLength)
Function Description: splitChars can contain a series of strings to split points testString, and can be set to be the length of the array. Note that setting the length of arrayLength and split in contravention of the relationship between sub-strings, it is recommended not to set the length of the under normal circumstances.
Example:

view plaincopy to clipboardprint?

  1. String input = "A b, cd | e";
  2. String input2 = "Pharmacy, basketball funky";
  3. String [] array1 = StringUtils.split (input, ",.|");
  4. String [] array2 = StringUtils.split (input2, ",", 2);
  5. System.out.println (ArrayUtils.toString (array1));
  6. System.out.println (ArrayUtils.toString (array2));


Output is as follows:
(A, b, c, d, e)
(Pharmacy, basketball funky)

5. To find a nested string <br /> use the function: StringUtils.substringBetween (testString, header, tail)
Function Description: In the testString made between the header and tail strings. Does not exist returns an empty routine:

view plaincopy to clipboardprint?

  1. String htmlContent = "ABC1234ABC4567";
  2. System.out.println (StringUtils.substringBetween (htmlContent, "1234", "4567"));
  3. System.out.println (StringUtils.substringBetween (htmlContent, "12345", "4567"));


Output is as follows:
ABC
null

6. To remove the rear line breaks <br /> use the function: StringUtils.chomp (testString)
Function Description: Remove line breaks in the tail testString routines:

view plaincopy to clipboardprint?

  1. String input = "Hello \ n";
  2. System.out.println (StringUtils.chomp (input));
  3. String input2 = "Another test \ r \ n";
  4. System.out.println (StringUtils.chomp (input2));


Output is as follows:
Hello
Another test

7. Repeat string <br /> use the function: StringUtils.repeat (repeatString, count)
Function Description: With the repeatString string repeated count times after the routine:

view plaincopy to clipboardprint?

  1. System.out.println (StringUtils.repeat ( "*", 10));
  2. System.out.println (StringUtils.repeat ( "China", 5));


Output is as follows:
**********
China China China China China

Other functions: StringUtils.center (testString, count, repeatString);
Function Description: A testString repeated several times inserting repeatString the middle of the string after received a total length of the string count
Example:

view plaincopy to clipboardprint?

  1. System.out.println (StringUtils.center ( "China", 11 ,"*"));


Output is as follows:
*** China ***

8. <br /> Reverse a string using the function: StringUtils.reverse (testString)
Function description: get testString characters after the string reversal routine:

view plaincopy to clipboardprint?

  1. System.out.println (StringUtils.reverse ( "ABCDE"));


Output is as follows:
EDCBA

9. To determine the type of content the string <br /> function description:
StringUtils.isNumeric (testString): If the testString composed entirely of numbers to return True
StringUtils.isAlpha (testString): If the testString composed entirely of letters return True
StringUtils.isAlphanumeric (testString): If the testString entirely by the number or numbers to return True
StringUtils.isAlphaspace (testString): If the testString composed entirely of letters or spaces return True

Example:

view plaincopy to clipboardprint?

  1. String state = "Virginia";
  2. System.out.println ( "Is state number?" + StringUtils.isNumeric (state));
  3. System.out.println ( "Is state alpha?" + StringUtils.isAlpha (state));
  4. System.out.println ( "Is state alphanumeric?" + StringUtils.isAlphanumeric (state));
  5. System.out.println ( "Is state alphaspace?" + StringUtils.isAlphaSpace (state));


Output is as follows:
Is state number? False
Is state alpha? True
Is state alphanumeric? True
Is state alphaspace? True

10. To obtain a string in another string appearing <br /> use the function: StringUtils.countMatches (testString, seqString)
Function Description: Get seqString in the testString in the number of occurrences found is returned to zero routine:

view plaincopy to clipboardprint?

  1. System.out.println (StringUtils.countMatches ( "Chinese People", "e"));


Output:
4

11. Part of the interception of a string <br /> use the function:
StringUtils.substringBetween (testString, fromString, toString): to obtain a string between two characters in
StringUtils.substringAfter (): get the string after the specified string
StringUtils.substringBefore (): get the string before the specified string
StringUtils.substringBeforeLast (): get the last one before the specified string string
StringUtils.substringAfterLast (): win the final after a specified string string

Function Description: The above should all say get the point.
Example:

view plaincopy to clipboardprint?

  1. String formatted = "25 * (30,40) [50,60] | 30";
  2. System.out.print ( "N0:" + StringUtils.substringBeforeLast (formatted, "*"));
  3. System.out.print ( ", N1:" + StringUtils.substringBetween (formatted, "(", ","));
  4. System.out.print ( ", N2:" + StringUtils.substringBetween (formatted, ",", ")"));
  5. System.out.print ( ", N3:" + StringUtils.substringBetween (formatted, "[", ","));
  6. System.out.print ( ", N4:" + StringUtils.substringBetween (formatted, ",", "]"));
  7. System.out.print ( ", N5:" + StringUtils.substringAfterLast (formatted, "|"));


Output is as follows:
N0: 25, N1: 30, N2: 40, N3: 50, N4: 40) [50,60, N5: 30

相关文章
  • Examples of common methods used org.apache.commons.lang.StringUtil 2010-03-26

    1. An empty string check Use the function: StringUtils.isBlank (testString) Function Description: When testString is empty, the length of zero, or only by whitespace (whitespace) the composition, return True; otherwise, returns False Example: view pl

  • Write less code! With 4 Apache Commons Lang class to understand the benefits of code reuse 2010-07-08

    Advantage of code reuse In the early software development, all that the developer's productivity and he proportional to the amount of code written. At that time, there is such a plausible benchmark: the code can be used leading to a general binary as

  • org.apache.commons.lang.builder--ToStringBuilder class 2010-03-29

    // This example is from the Jakarta Commons Cookbook public class PoliticalCandidate { private String lastName; private String firstName; private Date dateOfBirth; private BigDecimal moneyRaised; private State homeState; // get/set methods Omit publi

  • org.apache.commons.lang.StringUtils handle string type 2010-03-29

    org.apache.commons.lang Class StringUtils java.lang.Object org.apache.commons.lang.StringUtils public class StringUtils extends Object Operations on String that are null safe. Strings are null safe, do not throw NullPointerException, have done the ap

  • org.apache.commons.lang.StringUtils 中 Join 函数 2013-05-31

    写代码的时候,经常会碰到需要把一个List中的每个元素,按逗号分隔转成字符串的需求,以前是自己写一段比较难看的代码,先把字符串拼出来,再把最后面多余的逗号去掉:虽然功能可以实现,但总觉得最后加的那一步操作很没有必要: public static String join(List<String> list, String seperator){ if(list.isEmpty()){ return ""; } StringBuffer sb = new StringBuffe

  • org.apache.commons.lang.exception.NestableRunti... 2013-11-06

    用JSON报的一个错误java.lang.ClassNotFoundException: org.apache.commons.lang.exception.NestableRuntimeExcept . 2012-01-30 14:56:11| 分类: java|字号 订阅 以前在做项目的时候就曾接触过JSON的技术,但那个时候是项目经理把所有该配制的都配了,工具类也提供了,如何使用也跟我们说了,那个时候只是觉得很好用,倒没有研究过. 今天自己写了一个JSON的例子,可以一调用就出了问题,报下

  • org.apache.commons.lang.time 2014-05-21

    org.apache.commons.lang.time .这个包里面包含了如下 5 个类: DateFormatUtils – 提供格式化日期和时间的功能及相关常量: DateUtils – 在Calendar和Date的基础上提供更方便的访问: DurationFormatUtils – 提供格式化时间跨度的功能及相关常量: FastDateFormat – 为java.text.SimpleDateFormat提供一个的线程安全的替代类: StopWatch – 是一个方便的计时器. 我们

  • org.apache.commons.lang.builder--ReflectionToStringBuilder class 2010-03-29

    Let us look at what the common usage: // This example is from the Jakarta Commons Cookbook public class PoliticalCandidate { private String lastName; private String firstName; private Date dateOfBirth; private BigDecimal moneyRaised; private State ho

  • org.apache.commons.lang.builder Package Overview 2010-03-29

    The classes in this package, the main role is to generate some of the more common methods, such as: toString, hashCode, equals, compareTo. Here look at this package as a whole class diagram, draw the dependency graph there is no relationship between

  • apache.commons.lang.StringUtils 使用心得 2015-05-04

    在JAVA中我们用的最多的类应该就是String了.对于String的处理说简单也简单,但是有的时候要自己去实现一些功能还是要浪费一点时间的.一年之前接触了StringUtils这个工具类,就猛然爱上了它,日复一日心里始终觉得这东西实在太好了.不敢独享,所以决定要总结一下个人使用StringUtils的一些心得. 1.StringUtils.isEmpty(String str) 经常需要去判断一个字符串是否为空,null,"".使用该方法可以轻松得到结论: StringUtils.i

  • org.apache.commons.lang.time.DateFormatUtils 2010-04-14

    Common date format formatting operations: Example 1: to yyyy-MM-dd format: DateFormatUtils.ISO_DATE_FORMAT.format (new Date ()): 2009-03-20 Example 2: to yyyy-MM-ddZZ format: DateFormatUtils.ISO_DATE_TIME_ZONE_FORMAT.format (new Date ()): 2009-03-20

  • org.apache.commons.lang.StringUtils 2010-04-14

    String to judge: Example 1: Determine whether a null (blank): StringUtils.isBlank (null): true StringUtils.isBlank (""): true StringUtils.isBlank (""): true Example 2: Determine whether or not empty (blank): StringUtils.isNotBlank (nul

  • org.apache.commons.lang.ArrayUtils 2010-04-14

    Example 1: Array Printing: Array aStr: ("abc", "uvw", "xyz", "123", "456") ArrayUtils.toString (aStr): (abc, uvw, xyz, 123,456) Example 2: it contains an element of the array: Array aStr: ("abc",

  • org.apache.commons.lang.math.NumberUtils 2010-04-14

    Determine whether the string figures: Example 1: Determine a string of characters are all numbers: NumberUtils.isDigits ("11"): true NumberUtils.isDigits ("011"): true NumberUtils.isDigits ("0.11"): false Example 2: Determine

  • org.apache.commons.lang.time.DateUtils 2010-04-14

    To a date format string to convert the Date object: / / Code fragment: Date date = DateUtils.parseDate ("2008-12-12 12:12", new String [] ("yyyy-MM-dd HH: mm")); System.out.println ("Date object:" + date); / / Run Results: Da

  • org.apache.commons.lang.time.DurationFormatUtils 2010-04-14

    Format date interval: / / Code fragment: Date date1 = new Date (); Date date2 = DateUtils.addWeeks (date1, -2); String dura = DurationFormatUtils.formatDuration (date1.getTime () - Date2.getTime (), "d-day H-hour m minutes, s seconds"); System.o

  • apache commons - lang 常用方法记录 2014-12-28

    Lang 中工具类比较多,这里介绍几个主要的: ClassUtils: getShortClassName,这个函数应该在 java.lang.Class 类中有的,我看到很多人自己写这个函数.getAllInterfaces,convertClassNamesToClasses,isAssignable,primitivesToWrappers,isInnerClass. NumberUtils: 关于数字以及数字和字符串转换的类 stringToInt,toDouble,createNumb

  • Application Reference Apache Commons 2010-04-13

    org.apache.commons.lang the current (2009-03-19) the latest version: 2.4 With the similar role of this package java.lang, Commons Lang in this group is to provide some basic API, and general handling and processing, such as automatic generation of to

  • Apache Commons工具集简介 2013-11-14

    Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动.下面是我这几年做开发过程中 自己用过的工具类做简单介绍. 组件 功能介绍 BeanUtils 提供了对于JavaBean进行各种操作,克隆对象,属性等等. Betwixt XML与Java对象之间相互转换. Codec 处理常用的编码方法的工具类包 例如DES.SHA1.MD5.Base64等. Collections java集合框架操作. Compress java提供文件打包 压缩类库. Co

  • apache commons to collect summary 2010-03-29

    import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.FilenameFilter; import java.io.IO