commons-lang中常用方法

简介: commons-lang中常用方法 [java] view plain copy public class TestLangDemo {     public void charSetDemo() {         System.

commons-lang中常用方法

[java] view plain copy

  1. public class TestLangDemo {
  2.     public void charSetDemo() {
  3.         System.out.println("**CharSetDemo**");
  4.         CharSet charSet = CharSet.getInstance("aeiou");
  5.         String demoStr = "The quick brown fox jumps over the lazy dog.";
  6.         int count = 0;
  7.         for (int i = 0, len = demoStr.length(); i < len; i++) {
  8.             if (charSet.contains(demoStr.charAt(i))) {
  9.                 count++;
  10.             }
  11.         }
  12.         System.out.println("count: " + count);
  13.     }
  14.     public void charSetUtilsDemo() {
  15.         System.out.println("**CharSetUtilsDemo**");
  16.         System.out.println("计算字符串中包含某字符数.");
  17.         System.out.println(CharSetUtils.count("The quick brown fox jumps over the lazy dog.""aeiou"));
  18.         System.out.println("删除字符串中某字符.");
  19.         System.out.println(CharSetUtils.delete("The quick brown fox jumps over the lazy dog.""aeiou"));
  20.         System.out.println("保留字符串中某字符.");
  21.         System.out.println(CharSetUtils.keep("The quick brown fox jumps over the lazy dog.""aeiou"));
  22.         System.out.println("合并重复的字符.");
  23.         System.out.println(CharSetUtils.squeeze("a  bbbbbb     c dd""b d"));
  24.     }
  25.     public void objectUtilsDemo() {
  26.         System.out.println("**ObjectUtilsDemo**");
  27.         System.out.println("Object为null时,默认打印某字符.");
  28.         Object obj = null;
  29.         System.out.println(ObjectUtils.defaultIfNull(obj, "空"));
  30.         System.out.println("验证两个引用是否指向的Object是否相等,取决于Object的equals()方法.");
  31.         Object a = new Object();
  32.         Object b = a;
  33.         Object c = new Object();
  34.         System.out.println(ObjectUtils.equals(a, b));
  35.         System.out.println(ObjectUtils.equals(a, c));
  36.         System.out.println("用父类Object的toString()方法返回对象信息.");
  37.         Date date = new Date();
  38.         System.out.println(ObjectUtils.identityToString(date));
  39.         System.out.println(date);
  40.         System.out.println("返回类本身的toString()方法结果,对象为null时,返回0长度字符串.");
  41.         System.out.println(ObjectUtils.toString(date));
  42.         System.out.println(ObjectUtils.toString(null));
  43.         System.out.println(date);
  44.     }
  45.     public void serializationUtilsDemo() {
  46.         System.out.println("*SerializationUtils**");
  47.         Date date = new Date();
  48.         byte[] bytes = SerializationUtils.serialize(date);
  49.         System.out.println(ArrayUtils.toString(bytes));
  50.         System.out.println(date);
  51.         Date reDate = (Date) SerializationUtils.deserialize(bytes);
  52.         System.out.println(reDate);
  53.         System.out.println(ObjectUtils.equals(date, reDate));
  54.         System.out.println(date == reDate);
  55.         FileOutputStream fos = null;
  56.         FileInputStream fis = null;
  57.         try {
  58.             fos = new FileOutputStream(new File("d:/test.txt"));
  59.             fis = new FileInputStream(new File("d:/test.txt"));
  60.             SerializationUtils.serialize(date, fos);
  61.             Date reDate2 = (Date) SerializationUtils.deserialize(fis);
  62.             System.out.println(date.equals(reDate2));
  63.         } catch (FileNotFoundException e) {
  64.             e.printStackTrace();
  65.         } finally {
  66.             try {
  67.                 fos.close();
  68.                 fis.close();
  69.             } catch (IOException e) {
  70.                 e.printStackTrace();
  71.             }
  72.         }
  73.     }
  74.     public void randomStringUtilsDemo() {
  75.         System.out.println("**RandomStringUtilsDemo**");
  76.         System.out.println("生成指定长度的随机字符串,好像没什么用.");
  77.         System.out.println(RandomStringUtils.random(500));
  78.         System.out.println("在指定字符串中生成长度为n的随机字符串.");
  79.         System.out.println(RandomStringUtils.random(5"abcdefghijk"));
  80.         System.out.println("指定从字符或数字中生成随机字符串.");
  81.         System.out.println(RandomStringUtils.random(5truefalse));
  82.         System.out.println(RandomStringUtils.random(5falsetrue));
  83.     }
  84.     public void stringUtilsDemo() {
  85.         System.out.println("**StringUtilsDemo**");
  86.         System.out.println("将字符串重复n次,将文字按某宽度居中,将字符串数组用某字符串连接.");
  87.         String[] header = new String[3];
  88.         header[0] = StringUtils.repeat("*"50);
  89.         header[1] = StringUtils.center("  StringUtilsDemo  "50"^O^");
  90.         header[2] = header[0];
  91.         String head = StringUtils.join(header, "\n");
  92.         System.out.println(head);
  93.         System.out.println("缩短到某长度,用...结尾.");
  94.         System.out.println(StringUtils.abbreviate("The quick brown fox jumps over the lazy dog."10));
  95.         System.out.println(StringUtils.abbreviate("The quick brown fox jumps over the lazy dog."1510));
  96.         System.out.println("返回两字符串不同处索引号.");
  97.         System.out.println(StringUtils.indexOfDifference("aaabc""aaacc"));
  98.         System.out.println("返回两字符串不同处开始至结束.");
  99.         System.out.println(StringUtils.difference("aaabcde""aaaccde"));
  100.         System.out.println("截去字符串为以指定字符串结尾的部分.");
  101.         System.out.println(StringUtils.chomp("aaabcde""de"));
  102.         System.out.println("检查一字符串是否为另一字符串的子集.");
  103.         System.out.println(StringUtils.containsOnly("aad""aadd"));
  104.         System.out.println("检查一字符串是否不是另一字符串的子集.");
  105.         System.out.println(StringUtils.containsNone("defg""aadd"));
  106.         System.out.println("检查一字符串是否包含另一字符串.");
  107.         System.out.println(StringUtils.contains("defg""ef"));
  108.         System.out.println(StringUtils.containsOnly("ef""defg"));
  109.         System.out.println("返回可以处理null的toString().");
  110.         System.out.println(StringUtils.defaultString("aaaa"));
  111.         System.out.println("?" + StringUtils.defaultString(null) + "!");
  112.         System.out.println("去除字符中的空格.");
  113.         System.out.println(StringUtils.deleteWhitespace("aa  bb  cc"));
  114.         System.out.println("分隔符处理成数组.");
  115.         String[] strArray = StringUtils.split("a,b,,c,d,null,e"",");
  116.         System.out.println(strArray.length);
  117.         System.out.println(strArray.toString());
  118.         System.out.println("判断是否是某类字符.");
  119.         System.out.println(StringUtils.isAlpha("ab"));
  120.         System.out.println(StringUtils.isAlphanumeric("12"));
  121.         System.out.println(StringUtils.isBlank(""));
  122.         System.out.println(StringUtils.isNumeric("123"));
  123.     }
  124.     public void systemUtilsDemo() {
  125.         System.out.println(genHeader("SystemUtilsDemo"));
  126.         System.out.println("获得系统文件分隔符.");
  127.         System.out.println(SystemUtils.FILE_SEPARATOR);
  128.         System.out.println("获得源文件编码.");
  129.         System.out.println(SystemUtils.FILE_ENCODING);
  130.         System.out.println("获得ext目录.");
  131.         System.out.println(SystemUtils.JAVA_EXT_DIRS);
  132.         System.out.println("获得java版本.");
  133.         System.out.println(SystemUtils.JAVA_VM_VERSION);
  134.         System.out.println("获得java厂商.");
  135.         System.out.println(SystemUtils.JAVA_VENDOR);
  136.     }
  137.     public void classUtilsDemo() {
  138.         System.out.println(genHeader("ClassUtilsDemo"));
  139.         System.out.println("获取类实现的所有接口.");
  140.         System.out.println(ClassUtils.getAllInterfaces(Date.class));
  141.         System.out.println("获取类所有父类.");
  142.         System.out.println(ClassUtils.getAllSuperclasses(Date.class));
  143.         System.out.println("获取简单类名.");
  144.         System.out.println(ClassUtils.getShortClassName(Date.class));
  145.         System.out.println("获取包名.");
  146.         System.out.println(ClassUtils.getPackageName(Date.class));
  147.         System.out.println("判断是否可以转型.");
  148.         System.out.println(ClassUtils.isAssignable(Date.class, Object.class));
  149.         System.out.println(ClassUtils.isAssignable(Object.class, Date.class));
  150.     }
  151.     public void stringEscapeUtilsDemo() {
  152.         System.out.println(genHeader("StringEcsapeUtils"));
  153.         System.out.println("转换特殊字符.");
  154.         System.out.println("html:" + StringEscapeUtils.escapeHtml3(" "));
  155.         System.out.println("html:" + StringEscapeUtils.escapeHtml4(" "));
  156.         System.out.println("html:" + StringEscapeUtils.unescapeHtml3("<p>"));
  157.         System.out.println("html:" + StringEscapeUtils.unescapeHtml4("<p>"));
  158.     }
  159.     private final class BuildDemo {
  160.         String name;
  161.         int age;
  162.         public BuildDemo(String name, int age) {
  163.             this.name = name;
  164.             this.age = age;
  165.         }
  166.         public String toString() {
  167.             ToStringBuilder tsb = new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE);
  168.             tsb.append("Name", name);
  169.             tsb.append("Age", age);
  170.             return tsb.toString();
  171.         }
  172.         public int hashCode() {
  173.             HashCodeBuilder hcb = new HashCodeBuilder();
  174.             hcb.append(name);
  175.             hcb.append(age);
  176.             return hcb.hashCode();
  177.         }
  178.         public boolean equals(Object obj) {
  179.             if (!(obj instanceof BuildDemo)) {
  180.                 return false;
  181.             }
  182.             BuildDemo bd = (BuildDemo) obj;
  183.             EqualsBuilder eb = new EqualsBuilder();
  184.             eb.append(name, bd.name);
  185.             eb.append(age, bd.age);
  186.             return eb.isEquals();
  187.         }
  188.     }
  189.     public void builderDemo() {
  190.         System.out.println(genHeader("BuilderDemo"));
  191.         BuildDemo obj1 = new BuildDemo("a"1);
  192.         BuildDemo obj2 = new BuildDemo("b"2);
  193.         BuildDemo obj3 = new BuildDemo("a"1);
  194.         System.out.println("toString()");
  195.         System.out.println(obj1);
  196.         System.out.println(obj2);
  197.         System.out.println(obj3);
  198.         System.out.println("hashCode()");
  199.         System.out.println(obj1.hashCode());
  200.         System.out.println(obj2.hashCode());
  201.         System.out.println(obj3.hashCode());
  202.         System.out.println("equals()");
  203.         System.out.println(obj1.equals(obj2));
  204.         System.out.println(obj1.equals(obj3));
  205.     }
  206.     public void numberUtils() {
  207.         System.out.println(genHeader("NumberUtils"));
  208.         System.out.println("字符串转为数字(不知道有什么用).");
  209.         System.out.println(NumberUtils.toInt("ba"33));
  210.         System.out.println("从数组中选出最大值.");
  211.         System.out.println(NumberUtils.max(new int[] { 1234 }));
  212.         System.out.println("判断字符串是否全是整数.");
  213.         System.out.println(NumberUtils.isDigits("123.1"));
  214.         System.out.println("判断字符串是否是有效数字.");
  215.         System.out.println(NumberUtils.isNumber("0123.1"));
  216.     }
  217.     public void dateFormatUtilsDemo() {
  218.         System.out.println(genHeader("DateFormatUtilsDemo"));
  219.         System.out.println("格式化日期输出.");
  220.         System.out.println(DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss"));
  221.         System.out.println("秒表.");
  222.         StopWatch sw = new StopWatch();
  223.         sw.start();
  224.         for (Iterator iterator = DateUtils.iterator(new Date(), DateUtils.RANGE_WEEK_CENTER); iterator.hasNext();) {
  225.             Calendar cal = (Calendar) iterator.next();
  226.             System.out.println(DateFormatUtils.format(cal.getTime(), "yy-MM-dd HH:mm"));
  227.         }
  228.         sw.stop();
  229.         System.out.println("秒表计时:" + sw.getTime());
  230.     }
  231.     private String genHeader(String head) {
  232.         String[] header = new String[3];
  233.         header[0] = StringUtils.repeat("*"50);
  234.         header[1] = StringUtils.center("  " + head + "  "50"^O^");
  235.         header[2] = header[0];
  236.         return StringUtils.join(header, "\n");
  237.     }
  238.     private void validateDemo() {
  239.         String[] strarray = { "a""b""c" };
  240.         System.out.println("验证功能");
  241.         System.out.println(Validate.notEmpty(strarray));
  242.     }
  243.     private void wordUtilsDemo() {
  244.         System.out.println("单词处理功能");
  245.         String str1 = "wOrD";
  246.         String str2 = "ghj\nui\tpo";
  247.         System.out.println(WordUtils.capitalize(str1)); // 首字母大写
  248.         System.out.println(WordUtils.capitalizeFully(str1)); // 首字母大写其它字母小写
  249.         char[] ctrg = { '.' };
  250.         System.out.println(WordUtils.capitalizeFully("i aM.fine", ctrg)); // 在规则地方转换
  251.         System.out.println(WordUtils.initials(str1)); // 获取首字母
  252.         System.out.println(WordUtils.initials("Ben John Lee"null)); // 取每个单词的首字母
  253.         char[] ctr = { ' ''.' };
  254.         System.out.println(WordUtils.initials("Ben J.Lee", ctr)); // 按指定规则获取首字母
  255.         System.out.println(WordUtils.swapCase(str1)); // 大小写逆转
  256.         System.out.println(WordUtils.wrap(str2, 1)); // 解析\n和\t等字符
  257.     }
  258.     /**
  259.      * @param args
  260.      */
  261.     public static void main(String[] args) {
  262.         TestLangDemo langDemo = new TestLangDemo();
  263.         langDemo.charSetDemo();
  264.         langDemo.charSetUtilsDemo();
  265.         langDemo.objectUtilsDemo();
  266.         langDemo.serializationUtilsDemo();
  267.         langDemo.randomStringUtilsDemo();
  268.         langDemo.stringUtilsDemo();
  269.         langDemo.systemUtilsDemo();
  270.         langDemo.classUtilsDemo();
  271.         langDemo.stringEscapeUtilsDemo();
  272.         langDemo.builderDemo();
  273.         langDemo.numberUtils();
  274.         langDemo.dateFormatUtilsDemo();
  275.         langDemo.validateDemo();
  276.         langDemo.wordUtilsDemo();
  277.     }
  278. }
原文地址http://www.bieryun.com/2222.html
相关文章
|
3月前
|
Java Maven
java使用apache-commons-lang3生成随机字符串(可自定义规则、RandomUtils
java使用apache-commons-lang3生成随机字符串(可自定义规则、RandomUtils
70 0
|
5月前
|
Java
org.apache.poi.POIXMLException: java.lang.reflect.InvocationTargetException
org.apache.poi.POIXMLException: java.lang.reflect.InvocationTargetException
|
12月前
apache.commons.lang3常用工具类
apache.commons.lang3常用工具类
319 0
Java:org.apache.commons.lang3.StringUtils判断字符串为空
Java:org.apache.commons.lang3.StringUtils判断字符串为空
160 0
|
前端开发 Java API
【小家Java】common-lang3中StringUtils的使用详解(上)
【小家Java】common-lang3中StringUtils的使用详解(上)
【小家Java】common-lang3中StringUtils的使用详解(下)
【小家Java】common-lang3中StringUtils的使用详解(下)
|
Apache
org.apache.commons.lang.StringUtils的常用方法
org.apache.commons.lang.StringUtils的常用方法
881 0