Chinaunix首页 | 论坛 | 博客
  • 博客访问: 200847
  • 博文数量: 62
  • 博客积分: 2010
  • 博客等级: 大尉
  • 技术积分: 740
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-19 01:36
文章分类

全部博文(62)

文章存档

2009年(4)

2008年(58)

我的朋友

分类: 系统运维

2008-06-29 19:45:40

package sean.study.jakarta.commons.lang;

 

import org.apache.commons.lang.StringUtils;

 

public class StringUtilsAndWordUtilsUsage {

 

    public static void main(String[] args) {

       

        // data setup

        String str1 = "";

        String str2 = " ";

        String str3 = "\t";

        String str4 = null;

        String str5 = "123";

        String str6 = "ABCDEFG";

        String str7 = "It feels good to use Jakarta Commons.\r\n";

       

        // check for empty strings

        System.out.println("==============================");

        System.out.println("Is str1 blank? " + StringUtils.isBlank(str1));

        System.out.println("Is str2 blank? " + StringUtils.isBlank(str2));

        System.out.println("Is str3 blank? " + StringUtils.isBlank(str3));

        System.out.println("Is str4 blank? " + StringUtils.isBlank(str4));

       

        // check for numerics

        System.out.println("==============================");

        System.out.println("Is str5 numeric? " + StringUtils.isNumeric(str5));

        System.out.println("Is str6 numeric? " + StringUtils.isNumeric(str6));

       

        // reverse strings / whole words

        System.out.println("==============================");

        System.out.println("str6: " + str6);

        System.out.println("str6 reversed: " + StringUtils.reverse(str6));

        System.out.println("str7: " + str7);

        String str8 = StringUtils.chomp(str7);

        str8 = StringUtils.reverseDelimited(str8, ' ');

        System.out.println("str7 reversed whole words : \r\n" + str8);

       

        // build header (useful to print log messages that are easy to locate)

        System.out.println("==============================");

        System.out.println("print header:");

        String padding = StringUtils.repeat("=", 50);

        String msg = StringUtils.center(" Customised Header ", 50, "%");

        Object[] raw = new Object[]{padding, msg, padding};

        String header = StringUtils.join(raw, "\r\n");

        System.out.println(header);

 

    }

}

 

输出的结果如下:

 

==============================

Is str1 blank? true

Is str2 blank? true

Is str3 blank? true

Is str4 blank? true

==============================

Is str5 numeric? true

Is str6 numeric? false

==============================

str6: ABCDEFG

str6 reversed: GFEDCBA

str7: It feels good to use Jakarta Commons.

 

str7 reversed whole words :

Commons. Jakarta use to good feels It

==============================

print header:

==================================================

%%%%%%%%%%%%%%% Customised Header %%%%%%%%%%%%%%%%

==================================================

 

从代码中我们可以大致了解到这个StringUtils类简单而强大的处理能力,从检查空串(对null的情况处理很得体),到分割子串,到生成格式化的字符串,使用都很简洁,也很直截了当。

阅读(711) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~