开源工具 — Apache Commons Lang(2)

很多情况下我们都需要将字符串转换为数字,或判断字符串是否是数字等等操作,NumberUtils帮助我们方便的从字符串转换为数字,在不使用NumberUtils情况下,若然字符串值不是数字,使用Integer.parseInt()时会报出java.lang.NumberFormatException,但在NumberUtils的情况下,只会返回0而不产生错误 

      NumberUtils  and  RandomUtils 

Java代码   
  1. public class TestMain {  
  2.     public static void main(String[] args) throws IllegalAccessException {  
  3.         String str = "12.7";  
  4.         /* 
  5.          * org.apache.commons.lang.NumberUtils已经被弃用, 
  6.          * 注意要引入org.apache.commons.lang.math.NumberUtils 
  7.          */  
  8.   
  9.         // 判断字符串是否为整数  
  10.         NumberUtils.isDigits(str);  
  11.   
  12.         // 判断字符串是否为数字  
  13.         NumberUtils.isNumber(str);  
  14.   
  15.         // 获取参数中最大的值,支持传入数组  
  16.         NumberUtils.max(102030);  
  17.   
  18.         // 获取参数中最小的值,支持传入数组  
  19.         NumberUtils.min(102030);  
  20.   
  21.         // 将字符串转换为int类型,支持float,long,short等数值类型  
  22.         NumberUtils.toInt(str);  
  23.   
  24.         // 通过字符串创建BigDecimal类型 ,支持int,float,long等数值  
  25.         NumberUtils.createBigDecimal(str);  
  26.   
  27.   
  28.         /* 
  29.          * RandomUtils帮助我们产生随机数,不止是数字类型 ,  
  30.          * 连boolean类型都可以通过RandomUtils产生 
  31.          */  
  32.         RandomUtils.nextBoolean();  
  33.         RandomUtils.nextDouble();  
  34.         RandomUtils.nextLong();  
  35.         // 注意这里传入的参数不是随机种子,而是在0~1000之间产生一位随机数  
  36.         RandomUtils.nextInt(1000);  
  37.   
  38.     }  
  39. }  
Java代码    收藏代码
  1. public class TestMain {  
  2.     public static void main(String[] args) throws IllegalAccessException {  
  3.         String str = "12.7";  
  4.         /* 
  5.          * org.apache.commons.lang.NumberUtils已经被弃用, 
  6.          * 注意要引入org.apache.commons.lang.math.NumberUtils 
  7.          */  
  8.   
  9.         // 判断字符串是否为整数  
  10.         NumberUtils.isDigits(str);  
  11.   
  12.         // 判断字符串是否为数字  
  13.         NumberUtils.isNumber(str);  
  14.   
  15.         // 获取参数中最大的值,支持传入数组  
  16.         NumberUtils.max(102030);  
  17.   
  18.         // 获取参数中最小的值,支持传入数组  
  19.         NumberUtils.min(102030);  
  20.   
  21.         // 将字符串转换为int类型,支持float,long,short等数值类型  
  22.         NumberUtils.toInt(str);  
  23.   
  24.         // 通过字符串创建BigDecimal类型 ,支持int,float,long等数值  
  25.         NumberUtils.createBigDecimal(str);  
  26.   
  27.   
  28.         /* 
  29.          * RandomUtils帮助我们产生随机数,不止是数字类型 ,  
  30.          * 连boolean类型都可以通过RandomUtils产生 
  31.          */  
  32.         RandomUtils.nextBoolean();  
  33.         RandomUtils.nextDouble();  
  34.         RandomUtils.nextLong();  
  35.         // 注意这里传入的参数不是随机种子,而是在0~1000之间产生一位随机数  
  36.         RandomUtils.nextInt(1000);  
  37.   
  38.     }  
  39. }  



      在开发当中字符串的使用和操作时最为频繁的,而null的字符串经常让我们报出NullPointerException,在使用StringUtils后,将不需要为字符串的null值而烦恼,却又提供了更多的操作让我们更方便的操作字符串 


      StringUtils 

Java代码   
  1. public class TestMain {  
  2.     public static void main(String[] args) throws IllegalAccessException {  
  3.         String str = "Hello World";  
  4.         /* 
  5.          * 由于StringUtils拥有100+的方法,笔者不逐一列举用法, 
  6.          * 只列举笔者认为常用的或笔者使用过的 
  7.          */  
  8.   
  9.         // isEmpty和isBlank的区别在于isEmpty不会忽略空格,  
  10.         // " "<--对于这样的字符串isEmpty会认为不是空,  
  11.         // 而isBlank会认为是空,isBlank更常用  
  12.         StringUtils.isEmpty(str);  
  13.         StringUtils.isNotEmpty(str);  
  14.         StringUtils.isBlank(str);  
  15.         StringUtils.isNotBlank(str);  
  16.   
  17.   
  18.         // strip      --> 去除两端的aa  
  19.         // stripStart --> 去除开始位置的hell  
  20.         // stripEnd   --> 去除结尾位置的orld  
  21.         StringUtils.strip(str, "aa");  
  22.         StringUtils.stripStart(str, "hell");  
  23.         StringUtils.stripEnd(str, "orld");  
  24.   
  25.   
  26.         // 返回字符串在第三次出现A的位置  
  27.         StringUtils.ordinalIndexOf(str, "A"3);  
  28.   
  29.   
  30.         // 获取str 开始为hello结尾为orld中间的字符串  
  31.         // 注意此方法返回字符串      -->substringBetween  
  32.         // 注意此方法返回字符串数组(多了个s) --> substringsBetween  
  33.         StringUtils.substringBetween(str, "hell""orld");  
  34.         StringUtils.substringsBetween(str, "hell""orld");  
  35.   
  36.   
  37.         // 重复字符串,第二种重载形式为在重复中用hahah拼接  
  38.         StringUtils.repeat(str, 3);  
  39.         StringUtils.repeat(str, "hahah"2);  
  40.   
  41.   
  42.         // 统计参数2在字符串中出现的次数  
  43.         StringUtils.countMatches(str, "l");  
  44.   
  45.   
  46.         // 判断字符串是否全小写或大写  
  47.         StringUtils.isAllLowerCase(str);  
  48.         StringUtils.isAllUpperCase(str);  
  49.   
  50.   
  51.         // isAlpha        --> 全部由字母组成返回true  
  52.         // isNumeric      -->全部由数字组成返回true  
  53.         // isAlphanumeric -->全部由字母或数字组成返回true  
  54.         // isAlphaSpace   -->全部由字母或空格组成返回true  
  55.         // isWhitespace   -->全部由空格组成返回true  
  56.         StringUtils.isAlpha(str);  
  57.         StringUtils.isNumeric(str);  
  58.         StringUtils.isAlphanumeric(str);  
  59.         StringUtils.isAlphaSpace(str);  
  60.         StringUtils.isWhitespace(str);  
  61.   
  62.   
  63.         // 缩进字符串,第二参数最低为 4,要包含...  
  64.         // 现在Hello World输出为H...  
  65.         StringUtils.abbreviate(str, 4);  
  66.   
  67.   
  68.         // 首字母大写或小写  
  69.         StringUtils.capitalize(str);  
  70.         StringUtils.uncapitalize(str);  
  71.   
  72.   
  73.         // 将字符串数组转变为一个字符串,通过","拼接,支持传入iterator和collection  
  74.         StringUtils.join(new String[] { "Hello""World" }, ",");  
  75.   
  76.   
  77.   
  78.         /* 
  79.          * 经常性要把后台的字符串传递到前提作为html代码进行解释, 
  80.          * 可以使用以下方法进行转换,以下方法输出为 
  81.          * <p>Hello</p> 
  82.          */  
  83.         StringEscapeUtils.escapeHtml("Hello  
  84. ");  
  85.     }  
  86. }  
Java代码    收藏代码
  1. public class TestMain {  
  2.     public static void main(String[] args) throws IllegalAccessException {  
  3.         String str = "Hello World";  
  4.         /* 
  5.          * 由于StringUtils拥有100+的方法,笔者不逐一列举用法, 
  6.          * 只列举笔者认为常用的或笔者使用过的 
  7.          */  
  8.   
  9.         // isEmpty和isBlank的区别在于isEmpty不会忽略空格,  
  10.         // " "<--对于这样的字符串isEmpty会认为不是空,  
  11.         // 而isBlank会认为是空,isBlank更常用  
  12.         StringUtils.isEmpty(str);  
  13.         StringUtils.isNotEmpty(str);  
  14.         StringUtils.isBlank(str);  
  15.         StringUtils.isNotBlank(str);  
  16.   
  17.   
  18.         // strip      --> 去除两端的aa  
  19.         // stripStart --> 去除开始位置的hell  
  20.         // stripEnd   --> 去除结尾位置的orld  
  21.         StringUtils.strip(str, "aa");  
  22.         StringUtils.stripStart(str, "hell");  
  23.         StringUtils.stripEnd(str, "orld");  
  24.   
  25.   
  26.         // 返回字符串在第三次出现A的位置  
  27.         StringUtils.ordinalIndexOf(str, "A"3);  
  28.   
  29.   
  30.         // 获取str 开始为hello结尾为orld中间的字符串  
  31.         // 注意此方法返回字符串      -->substringBetween  
  32.         // 注意此方法返回字符串数组(多了个s) --> substringsBetween  
  33.         StringUtils.substringBetween(str, "hell""orld");  
  34.         StringUtils.substringsBetween(str, "hell""orld");  
  35.   
  36.   
  37.         // 重复字符串,第二种重载形式为在重复中用hahah拼接  
  38.         StringUtils.repeat(str, 3);  
  39.         StringUtils.repeat(str, "hahah"2);  
  40.   
  41.   
  42.         // 统计参数2在字符串中出现的次数  
  43.         StringUtils.countMatches(str, "l");  
  44.   
  45.   
  46.         // 判断字符串是否全小写或大写  
  47.         StringUtils.isAllLowerCase(str);  
  48.         StringUtils.isAllUpperCase(str);  
  49.   
  50.   
  51.         // isAlpha        --> 全部由字母组成返回true  
  52.         // isNumeric      -->全部由数字组成返回true  
  53.         // isAlphanumeric -->全部由字母或数字组成返回true  
  54.         // isAlphaSpace   -->全部由字母或空格组成返回true  
  55.         // isWhitespace   -->全部由空格组成返回true  
  56.         StringUtils.isAlpha(str);  
  57.         StringUtils.isNumeric(str);  
  58.         StringUtils.isAlphanumeric(str);  
  59.         StringUtils.isAlphaSpace(str);  
  60.         StringUtils.isWhitespace(str);  
  61.   
  62.   
  63.         // 缩进字符串,第二参数最低为 4,要包含...  
  64.         // 现在Hello World输出为H...  
  65.         StringUtils.abbreviate(str, 4);  
  66.   
  67.   
  68.         // 首字母大写或小写  
  69.         StringUtils.capitalize(str);  
  70.         StringUtils.uncapitalize(str);  
  71.   
  72.   
  73.         // 将字符串数组转变为一个字符串,通过","拼接,支持传入iterator和collection  
  74.         StringUtils.join(new String[] { "Hello""World" }, ",");  
  75.   
  76.   
  77.   
  78.         /* 
  79.          * 经常性要把后台的字符串传递到前提作为html代码进行解释, 
  80.          * 可以使用以下方法进行转换,以下方法输出为 
  81.          * <p>Hello</p> 
  82.          */  
  83.         StringEscapeUtils.escapeHtml("Hello  
  84. ");  
  85.     }  
  86. }  



      DateUtils and DateFormatUtils 

Java代码   
  1. public class TestMain {  
  2.     public static void main(String[] args) throws IllegalAccessException {  
  3.         Date day1 = new Date();  
  4.         /* 
  5.          * 由于Aache的DateUtils和DateFormatUtils并没有Joda强大, 
  6.          *  所以在这里只作简单的示例 
  7.          */  
  8.           
  9.         // 增加一天  
  10.         DateUtils.addDays(day1, 1);  
  11.         // 减少一年  
  12.         DateUtils.addYears(day1, -1);  
  13.   
  14.         // 格式化时间,第三参数为国际化,表示按美国时间显示  
  15.         DateFormatUtils.format(day1, "yyyy-MM-dd", Locale.UK);  
  16.   
  17.     }  
  18. }  
Java代码    收藏代码
  1. public class TestMain {  
  2.     public static void main(String[] args) throws IllegalAccessException {  
  3.         Date day1 = new Date();  
  4.         /* 
  5.          * 由于Aache的DateUtils和DateFormatUtils并没有Joda强大, 
  6.          *  所以在这里只作简单的示例 
  7.          */  
  8.           
  9.         // 增加一天  
  10.         DateUtils.addDays(day1, 1);  
  11.         // 减少一年  
  12.         DateUtils.addYears(day1, -1);  
  13.   
  14.         // 格式化时间,第三参数为国际化,表示按美国时间显示  
  15.         DateFormatUtils.format(day1, "yyyy-MM-dd", Locale.UK);  
  16.   
  17.     }  
  18. }  



总结: 
      commons工具包很多开源组织都有提供,例如google,spring,apache都有各自的工具包,有众多的选择,但最终的目的只是为了方便我们程序的开发和维护,简化我们编写一些常用的逻辑,提升我们开发的效率,从而达到活在开源,善用开源

  • 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.langApache 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、付费专栏及课程。

余额充值