commons.lang

一、前言

    Java码农不识Apache,敲尽一生也枉然。旗下的开源项目众多,各个都是吊炸天。今日且说Commons,轻轻点击此链接进入Apache Commons主页,Logging、Pool、Net、ONGL、EL、IO、DBCP、Email、Collection、Lang……等等项目中常用到的包。而这篇文章的主角Lang则是我们最常用的工具作为jdk的补充,怎能不去详细探究一番!

二、字符串的处理类(StringUtils)

    org.apache.commons.lang3.StringUtils 继承Object,Operations on String that are null safe。所谓的null safe就是对String进行操作不会出现NullPointerException异常,很实用有没有!以后再也不怕到处出现空指针异常了。先看看官方文档中这个类都有些什么方法:

    这些方法基本上看方法名,就能猜出它大概的作用了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//缩短到某长度,用...结尾.其实就是(substring(str, 0, max-3) + "...")
         //public static String abbreviate(String str,int maxWidth)
         StringUtils.abbreviate( "abcdefg" 6 ); // ---"abc..."
          
         //字符串结尾的后缀是否与你要结尾的后缀匹配,若不匹配则添加后缀
         StringUtils.appendIfMissing( "abc" , "xyz" ); //---"abcxyz"
         StringUtils.appendIfMissingIgnoreCase( "abcXYZ" , "xyz" ); //---"abcXYZ"
          
         //首字母大小写转换
         StringUtils.capitalize( "cat" ); //---"Cat"
         StringUtils.uncapitalize( "Cat" ); //---"cat"
          
         //字符串扩充至指定大小且居中(若扩充大小少于原字符大小则返回原字符,若扩充大小为 负数则为0计算 )
         StringUtils.center( "abcd" 2 ); //--- "abcd"
         StringUtils.center( "ab" , - 1 ); //--- "ab"
         StringUtils.center( "ab" 4 ); //---" ab "
         StringUtils.center( "a" 4 "yz" ); //---"yayz"
         StringUtils.center( "abc" 7 "" ); //---"  abc  "
          
         //去除字符串中的"\n", "\r", or "\r\n"
         StringUtils.chomp( "abc\r\n" ); //---"abc"
          
         //判断一字符串是否包含另一字符串
         StringUtils.contains( "abc" "z" ); //---false
         StringUtils.containsIgnoreCase( "abc" "A" ); //---true
          
         //统计一字符串在另一字符串中出现次数
         StringUtils.countMatches( "abba" "a" ); //---2
          
         //删除字符串中的梭有空格
         StringUtils.deleteWhitespace( "   ab  c  " ); //---"abc"
          
         //比较两字符串,返回不同之处。确切的说是返回第二个参数中与第一个参数所不同的字符串
         StringUtils.difference( "abcde" "abxyz" ); //---"xyz"
          
         //检查字符串结尾后缀是否匹配
         StringUtils.endsWith( "abcdef" "def" ); //---true
         StringUtils.endsWithIgnoreCase( "ABCDEF" "def" ); //---true
         StringUtils.endsWithAny( "abcxyz" new  String[] { null "xyz" "abc" }); //---true
          
         //检查起始字符串是否匹配
         StringUtils.startsWith( "abcdef" "abc" ); //---true
         StringUtils.startsWithIgnoreCase( "ABCDEF" "abc" ); //---true
         StringUtils.startsWithAny( "abcxyz" new  String[] { null "xyz" "abc" }); //---true
          
         //判断两字符串是否相同
         StringUtils.equals( "abc" "abc" ); //---true
         StringUtils.equalsIgnoreCase( "abc" "ABC" ); //---true
          
         //比较字符串数组内的所有元素的字符序列,起始一致则返回一致的字符串,若无则返回""
         StringUtils.getCommonPrefix( new  String[] { "abcde" "abxyz" }); //---"ab"
          
         //正向查找字符在字符串中第一次出现的位置
         StringUtils.indexOf( "aabaabaa" "b" ); //---2
         StringUtils.indexOf( "aabaabaa" "b" 3 ); //---5(从角标3后查找)
         StringUtils.ordinalIndexOf( "aabaabaa" "a" 3 ); //---1(查找第n次出现的位置)
          
         //反向查找字符串第一次出现的位置
         StringUtils.lastIndexOf( "aabaabaa" 'b' ); //---5
         StringUtils.lastIndexOf( "aabaabaa" 'b' 4 ); //---2
         StringUtils.lastOrdinalIndexOf( "aabaabaa" "ab" 2 ); //---1
          
         //判断字符串大写、小写
         StringUtils.isAllUpperCase( "ABC" ); //---true
         StringUtils.isAllLowerCase( "abC" ); //---false
          
         //判断是否为空(注:isBlank与isEmpty 区别)
         StringUtils.isBlank( null );StringUtils.isBlank( "" );StringUtils.isBlank( " " ); //---true
         StringUtils.isNoneBlank( " " "bar" ); //---false
          
         StringUtils.isEmpty( null );StringUtils.isEmpty( "" ); //---true
         StringUtils.isEmpty( " " ); //---false
         StringUtils.isNoneEmpty( " " "bar" ); //---true
          
         //判断字符串数字
         StringUtils.isNumeric( "123" ); //---false
         StringUtils.isNumeric( "12 3" ); //---false (不识别运算符号、小数点、空格……)
         StringUtils.isNumericSpace( "12 3" ); //---true
          
         //数组中加入分隔符号
         //StringUtils.join([1, 2, 3], ';');//---"1;2;3"
          
         //大小写转换
         StringUtils.upperCase( "aBc" ); //---"ABC"
         StringUtils.lowerCase( "aBc" ); //---"abc"
         StringUtils.swapCase( "The dog has a BONE" ); //---"tHE DOG HAS A bone"
          
         //替换字符串内容……(replacePattern、replceOnce)
         StringUtils.replace( "aba" "a" "z" ); //---"zbz"
         StringUtils.overlay( "abcdef" "zz" 2 4 ); //---"abzzef"(指定区域)
         StringUtils.replaceEach( "abcde" new  String[]{ "ab" "d" },
                 new  String[]{ "w" "t" }); //---"wcte"(多组指定替换ab->w,d->t)
          
         //重复字符
         StringUtils.repeat( 'e' 3 ); //---"eee"
          
         //反转字符串
         StringUtils.reverse( "bat" ); //---"tab"
          
         //删除某字符
         StringUtils.remove( "queued" 'u' ); //---"qeed"
          
         //分割字符串
         StringUtils.split( "a..b.c" '.' ); //---["a", "b", "c"]
         StringUtils.split( "ab:cd:ef" ":" 2 ); //---["ab", "cd:ef"]
         StringUtils.splitByWholeSeparator( "ab-!-cd-!-ef" "-!-" 2 ); //---["ab", "cd-!-ef"]
         StringUtils.splitByWholeSeparatorPreserveAllTokens( "ab::cd:ef" ":" ); //-["ab"," ","cd","ef"]
          
         //去除首尾空格,类似trim……(stripStart、stripEnd、stripAll、stripAccents)
         StringUtils.strip( " ab c " ); //---"ab c"
         StringUtils.stripToNull( null ); //---null
         StringUtils.stripToEmpty( null ); //---""
          
         //截取字符串
         StringUtils.substring( "abcd" 2 ); //---"cd"
         StringUtils.substring( "abcdef" 2 4 ); //---"cd"
          
         //left、right从左(右)开始截取n位字符
         StringUtils.left( "abc" 2 ); //---"ab"
         StringUtils.right( "abc" 2 ); //---"bc"
         //从第n位开始截取m位字符       n  m
         StringUtils.mid( "abcdefg" 2 4 ); //---"cdef"
          
         StringUtils.substringBefore( "abcba" "b" ); //---"a"
         StringUtils.substringBeforeLast( "abcba" "b" ); //---"abc"
         StringUtils.substringAfter( "abcba" "b" ); //---"cba"
         StringUtils.substringAfterLast( "abcba" "b" ); //---"a"
          
         StringUtils.substringBetween( "tagabctag" "tag" ); //---"abc"
         StringUtils.substringBetween( "yabczyabcz" "y" "z" ); //---"abc"

   

三、其它类简介

    RandomStringUtils:

   

1
2
3
4
5
6
7
         //随机生成n位数数字
         RandomStringUtils.randomNumeric(n);
         //在指定字符串中生成长度为n的随机字符串
         RandomStringUtils.random(n,  "abcdefghijk" );
         //指定从字符或数字中生成随机字符串
         System.out.println(RandomStringUtils.random(n,  true false ));  
         System.out.println(RandomStringUtils.random(n,  false true ));

   

    NumberUtils:

1
2
3
4
5
6
         //从数组中选出最大值
         NumberUtils.max( new  int [] {  1 2 3 4  }); //---4
         //判断字符串是否全是整数
         NumberUtils.isDigits( "153.4" ); //--false
         //判断字符串是否是有效数字
         NumberUtils.isNumber( "0321.1" ); //---false

    ArrayUtils:

   

  

1
2
3
4
5
6
7
8
9
       //创建数组
         String[] array = ArrayUtils.toArray( "1" "2" );
         //判断两个数据是否相等,如果内容相同, 顺序相同 则返回 true
         ArrayUtils.isEquals(arr1,arr2);
         //判断数组中是否包含某一对象
         ArrayUtils.contains(arr,  "33" );
         //二维数组转换成MAP
         Map map = ArrayUtils.toMap( new  String[][] { 
                 "RED" "#FF0000"  }, {  "GREEN" "#00FF00"  }, {  "BLUE" "#0000FF"  } });

   

    DateUtils:

   

1
2
3
4
5
6
         //日期加n天
         DateUtils.addDays( new  Date(), n);
         //判断是否同一天
         DateUtils.isSameDay(date1, date2);
         //字符串时间转换为Date
         DateUtils.parseDate(str, parsePatterns);

   

四、结语

    本文只是简单的介绍了commons-lang中的一些常用工具类,还有许多挺实用的就不一一列举。还是要自己去查阅文档试用了才能体会到它的简便。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: org.apache.commons.lang是一个开源的Java工具库,提供了许多常用的工具类,用于简化Java开发过程中的一些常见任务。 我们可以通过以下步骤下载org.apache.commons.lang: 1. 打开Apache Commons的官方网站,网址为https://commons.apache.org/proper/commons-lang/ 2. 在网站页面的顶部导航栏找到"Download"链接,并点击进入下载页面。 3. 在下载页面中,找到最新版本的org.apache.commons.lang,并点击下载。 4. 下载完成后,将下载的文件解压缩到你希望存放的目录中。 当我们成功下载并解压缩org.apache.commons.lang后,我们就可以开始在我们的Java项目中使用它了。 要在我们的项目中使用org.apache.commons.lang,我们需要在项目的构建路径中添加对应的库文件。具体步骤如下: 1. 打开你的Java集成开发环境(如Eclipse、IntelliJ等)。 2. 找到你的项目,并右键点击项目名称。然后在弹出菜单中选择"Build Path"或"设置构建路径"。 3. 在构建路径设置对话框中,选择"Libraries"或"库"选项卡。 4. 点击"Add JARs"或"添加JAR文件"按钮,并选择你下载并解压缩的org.apache.commons.lang的JAR文件。 5. 点击"OK"或"确定"按钮,保存设置,完成对org.apache.commons.lang的导入。 这样,我们就可以在我们的代码中使用org.apache.commons.lang的各种工具类了,比如StringUtils、NumberUtils等等,来简化我们的开发工作,提高代码的可读性和效率。同时,我们也可以通过查阅官方文档和示例代码来了解更多关于org.apache.commons.lang的使用方法和功能。 ### 回答2: org.apache.commons.lang是Apache Commons项目中的一个模块,提供了一系列常用的工具类和方法,主要目的是为了简化Java开发过程中的一些常见操作。以下是关于如何下载org.apache.commons.lang的步骤: 1. 打开Apache Commons官方网站(https://commons.apache.org/)。 2. 在页面上方的菜单栏中,点击"Projects"(项目)。 3. 在项目列表中,找到并点击"Commons Lang"。 4. 在该项目的页面上,可以找到项目的描述、版本信息、特性列表等相关信息。 5. 在页面上方的菜单栏中,点击"Download"(下载)。 6. 在下载页面中,可以找到各个版本的发布包和文档。 7. 根据需要选择合适的版本(一般推荐选择最新稳定版),点击相应版本的下载链接。 8. 下载完成后,解压文件,即可得到org.apache.commons.lang相关的jar文件。 9. 在开发环境中,将该jar文件引入到项目中。 以上就是关于如何下载org.apache.commons.lang的简要步骤。请注意,在下载和使用任何第三方库之前,务必仔细阅读官方文档,了解其使用方法和要求,以免出现使用异常和安全问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值