-
2004年12月30日
如何使用org.apache.commons.lang(学习笔记)
版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
http://fireshort.blogbus.com/logs/560878.html
CharRange
You use the CharRange class to create and maintain a range of characters. The class provides functionality to determine if a character lies in a certain range as well as to determine if one character range lies within another character range.CharSet
The CharSet class simplifies creating and using a set of characters. The CharSet can be created from the characters in a string and is especially useful when you want to check if a certain string holds only valid characters. You could create a CharSet and then check each character in the string against the character set.CharSetUtils
CharSetUtils provides static methods to handle character-related functionality that is often required during developmentNote For CharSet, case does matter. As a result, the character b and the character B are not the same.
System.out.println("B,o,k,e and r count = " +
CharSetUtils.count("BorisBecker", new String[] { "Bo", "ker" })); //8
System.out.println("Delete B,o,k,e and r = " +
CharSetUtils.delete("BorisBecker", new String[] { "Bo", "ker" })); //isc
//Keeps only the characters specified
System.out.println("Keep B and o = " +
CharSetUtils.keep("BorisBecker", "Bo")); //BoB
//Removes specified character repetitions
System.out.println("Squeeze B and o = " +
CharSetUtils.squeeze("BBoooorisbbbecker", "Bo")); //BorisbbbeckerObjectUtils
The ObjectUtils class is a simple one that provides a couple of useful features. The toString method that has been introduced in Lang version 2 can be useful to avoid one of the most deadly problems facing Java developers, the NullPointerException. You can use the toString method to return an empty string or a specified string if the String instance provided is null. You could now replace all ternary operator usages in the form strInstance= (strInstance == null ? "": strInstance); with strInstance = ObjectUtils.toString(strInstance);.SerializationUtils
Working with Java Input/Output (I/O) has never been my favorite. Remembering numerous classes, their hierarchies, and when to use what can be quite a pain. You can use the SerializationUtils class to take up the task of serializing and deserializing objects, so you do not have to worry about object input and output streams.The SerializationUtils class also provides methods that can use a byte array to serialize and deserialize. A clone method is also provided that serializes an object to a byte array and then deserializes it back to achieve cloning of the object.
RandomStringUtils
RandomStringUtils is one class that you can have a lot of fun using. The class generates random strings based on various parameters specified. One great option is that you can use it to hold a daily lottery at your desk. The point, of course, is that the source code can be rigged to make you win! A commonly required business application for the RandomStringUtils class is to generate passwords.RandomStringUtils是一个设计用来随机生成文本位的类,这在密码的生成上很常见。其全部范围是unicode,并且支持英文字母、Ascii码、AsciiNumeric和数字的特别重载。
StringUtils
StringUtils is the class in the Lang component that provides the most number of static methods to use. StringUtils has methods that can do almost everything you could want to do with a string. The class provides more than 100 methods; you will learn about some of the most useful ones.The StringUtils class even has some methods such as equals, trim, and substring that do what java.lang.String methods do. The StringUtils methods, however, provide built-in handling for null strings. A string being null is one of the most common causes of NullPointerException being thrown, so using the StringUtils methods is a better option in cases where you might get a null string. The Javadocs for the StringUtils class list all the methods that are null safe. Another useful addition to the Javadocs in Lang 2 is that now every method in StringUtils has some usage examples.
NULL 这个空值常常会导致程序的 NullPointerException, 在撰写接收使用者 request 程序的时候会特別麻烦, 需要多输入 null 的检查, 不如使用 EMPTY 空字串来得方便, 所以我们可以将一些 StringUtils.defaultString(request.getParameter("xxx")) 来确保不是 NULL.
capitalise(String):一个String大写化函数(这里保留了英式拼法),它不像大多数字符串库那样使用toUpperCase,而是恰当地使用了toTitleCase。
join(Object[], String):它把对象数组里每个对象的toString合并成带有指定定界符的单个String。所以join( {"A","B",C"}, ";")的结果是"A;B;C"。Iterator也可以被连接。
split(String, String):它能分割带有定界符的文本。其功能没有StringTokenizer(它不能一次处理多组定界符)强大,但是能够很快、很容易地应用于很多场合。它是上一个合并方法的反向操作:split("A;B;C",";") => {"A", "B", "C"}。这个分割方法现在已经可以在 JDK 1.4的java.lang.String里找到了。
reverseDelimitedString(String, String):这是个很有趣的方法。它能以定界符为基础颠倒一段文本。所以:reverseDelimitedString("org.apache.commons")的结果就变成了"commons.apache.org."
replace(String, String, String):这个String替换方法常常是很让人期待的。JDK1.4能够使用正规表达式解决这个问题,但是StringUtils类所提供的基于String更简单的版本更常用。
SystemUtils
增加了一些 java 系统上面的处理, 例如检查 java 的版本 isJavaVersionAtLeast , 执行的作业系统等等..
SystemUtils is a handy little class that reads the system properties and returns appropriate messages based on the property values. You can use this class to find more information about the host machine and operating system. The most common usage, however, is to check the version of the JSDK being used. So, if you are using the logging introduced in JSDK 1.4, you could first check if the version being used is 1.4 or higher, and only if it is do you use the JSDK 1.4 features.The class provides only three public static methods, one of which is deprecated. Dozens of useful public static variables handle specific cases.
ClassUtils
The ClassUtils class is a new class introduced with version 2. This class provides static methods that can fetch you more information about a class or an object without having to directly use the Reflection Application Programming Interface (API).However, think twice before using some of the methods; if your design is proper, you ideally should not have to put such checks in your code.
StringEscapeUtils
Having to handle Hypertext Markup Language (HTML), JavaScript, Structured Query Language (SQL), and Java simultaneously is very much a part of a Java developer's life. However, generating output in a different format or handling input in a different format can be quite a task.For example, say you want to display <p>MyName<p> on a Web page. If you just send this as part of the HTML, what would appear on the screen is a new paragraph with the word MyName in it. The <p> tags used would not be displayed. What you need to send in the HTML would be this line of code:
<p>MyName<p>
This would then result in you getting the expected display <p>MyName<p>. TheStringEscapeUtils class makes implementing these cases quite simple because it provides ways of converting characters to achieve the expected output.
随机文章:
用Java进行LDAP编程的方式 2007年01月23日Java中对有BOM头的UTF-8文件的处理 2006年10月19日j2ee系统与rtx的整合实现 2006年07月27日commons-fileupload中文乱码问题的解决 2005年12月20日javadbf中文问题的解决 2005年10月19日
收藏到:Del.icio.us
引用地址:






评论