`
ray_yui
  • 浏览: 216693 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

开源工具 — Apache Commons Lang(2)

阅读更多

开源工具系列文章:
      Apache Commons Lang(1):http://ray-yui.iteye.com/blog/1953020
      Apache Commons Lang(2):http://ray-yui.iteye.com/blog/1958319
      Apache Commons BeanUtils:http://ray-yui.iteye.com/blog/1961451
      Apache Commons Collections:http://ray-yui.iteye.com/blog/2021484
      Apache Commons IO:http://ray-yui.iteye.com/blog/2023034


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

      NumberUtils  and  RandomUtils

public class TestMain {
	public static void main(String[] args) throws IllegalAccessException {
		String str = "12.7";
		/*
		 * org.apache.commons.lang.NumberUtils已经被弃用,
		 * 注意要引入org.apache.commons.lang.math.NumberUtils
		 */

		// 判断字符串是否为整数
		NumberUtils.isDigits(str);

		// 判断字符串是否为数字
		NumberUtils.isNumber(str);

		// 获取参数中最大的值,支持传入数组
		NumberUtils.max(10, 20, 30);

		// 获取参数中最小的值,支持传入数组
		NumberUtils.min(10, 20, 30);

		// 将字符串转换为int类型,支持float,long,short等数值类型
		NumberUtils.toInt(str);

		// 通过字符串创建BigDecimal类型 ,支持int,float,long等数值
		NumberUtils.createBigDecimal(str);


		/*
		 * RandomUtils帮助我们产生随机数,不止是数字类型 , 
		 * 连boolean类型都可以通过RandomUtils产生
		 */
		RandomUtils.nextBoolean();
		RandomUtils.nextDouble();
		RandomUtils.nextLong();
		// 注意这里传入的参数不是随机种子,而是在0~1000之间产生一位随机数
		RandomUtils.nextInt(1000);

	}
}


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


      StringUtils

public class TestMain {
	public static void main(String[] args) throws IllegalAccessException {
		String str = "Hello World";
		/*
		 * 由于StringUtils拥有100+的方法,笔者不逐一列举用法,
		 * 只列举笔者认为常用的或笔者使用过的
		 */

		// isEmpty和isBlank的区别在于isEmpty不会忽略空格,
		// " "<--对于这样的字符串isEmpty会认为不是空,
        // 而isBlank会认为是空,isBlank更常用
		StringUtils.isEmpty(str);
		StringUtils.isNotEmpty(str);
		StringUtils.isBlank(str);
		StringUtils.isNotBlank(str);


		// strip      --> 去除两端的aa
		// stripStart --> 去除开始位置的hell
		// stripEnd   --> 去除结尾位置的orld
		StringUtils.strip(str, "aa");
		StringUtils.stripStart(str, "hell");
		StringUtils.stripEnd(str, "orld");


		// 返回字符串在第三次出现A的位置
		StringUtils.ordinalIndexOf(str, "A", 3);


		// 获取str 开始为hello结尾为orld中间的字符串
		// 注意此方法返回字符串      -->substringBetween
		// 注意此方法返回字符串数组(多了个s) --> substringsBetween
		StringUtils.substringBetween(str, "hell", "orld");
		StringUtils.substringsBetween(str, "hell", "orld");


		// 重复字符串,第二种重载形式为在重复中用hahah拼接
		StringUtils.repeat(str, 3);
		StringUtils.repeat(str, "hahah", 2);


		// 统计参数2在字符串中出现的次数
		StringUtils.countMatches(str, "l");


		// 判断字符串是否全小写或大写
		StringUtils.isAllLowerCase(str);
		StringUtils.isAllUpperCase(str);


		// isAlpha        --> 全部由字母组成返回true
		// isNumeric      -->全部由数字组成返回true
		// isAlphanumeric -->全部由字母或数字组成返回true
		// isAlphaSpace   -->全部由字母或空格组成返回true
		// isWhitespace   -->全部由空格组成返回true
		StringUtils.isAlpha(str);
		StringUtils.isNumeric(str);
		StringUtils.isAlphanumeric(str);
		StringUtils.isAlphaSpace(str);
		StringUtils.isWhitespace(str);


		// 缩进字符串,第二参数最低为 4,要包含...
		// 现在Hello World输出为H...
		StringUtils.abbreviate(str, 4);


		// 首字母大写或小写
		StringUtils.capitalize(str);
		StringUtils.uncapitalize(str);


		// 将字符串数组转变为一个字符串,通过","拼接,支持传入iterator和collection
		StringUtils.join(new String[] { "Hello", "World" }, ",");



		/*
		 * 经常性要把后台的字符串传递到前提作为html代码进行解释,
		 * 可以使用以下方法进行转换,以下方法输出为
		 * <p>Hello</p>
		 */
		StringEscapeUtils.escapeHtml("Hello
");
	}
}


      DateUtils and DateFormatUtils

public class TestMain {
	public static void main(String[] args) throws IllegalAccessException {
		Date day1 = new Date();
		/*
		 * 由于Aache的DateUtils和DateFormatUtils并没有Joda强大,
		 *  所以在这里只作简单的示例
		 */
		
		// 增加一天
		DateUtils.addDays(day1, 1);
		// 减少一年
		DateUtils.addYears(day1, -1);

		// 格式化时间,第三参数为国际化,表示按美国时间显示
		DateFormatUtils.format(day1, "yyyy-MM-dd", Locale.UK);

	}
}


总结:
      commons工具包很多开源组织都有提供,例如google,spring,apache都有各自的工具包,有众多的选择,但最终的目的只是为了方便我们程序的开发和维护,简化我们编写一些常用的逻辑,提升我们开发的效率,从而达到活在开源,善用开源
15
5
分享到:
评论
9 楼 timer_yin 2014-02-25  
感谢 知道了joda
8 楼 zcqshine 2013-10-21  
不错,顶..........尽量不要重复造轮子,hoho
7 楼 k10509806 2013-10-16  
ray_yui 写道
white_crucifix 写道
cywhoyi 写道
white_crucifix 写道
恩,google的guava lib里封装了很多奇奇怪怪的特殊的数据结构

自从用了guava神清气爽,apache common包比较早很多JDK1.5之后的属性在之前的工具类中并未采纳,比如cache、List等,guava确实优势比较明显


想问一下guava里哪些特性用的比较多?

guava做得确实比commons好,我在项目当中也是使用guava较多,guava的文章也写了几篇待发,例如commons的collections还没支持泛型,而且guava对数据结构的支持和种类也比commons多,但由于commons比较经典,所以先写了commons

可是很多开源框架都是基于commons的,然后项目不想再引用其他jar包了,就用commons了。可能这就是commons的优势吧。
6 楼 ray_yui 2013-10-16  
white_crucifix 写道
cywhoyi 写道
white_crucifix 写道
恩,google的guava lib里封装了很多奇奇怪怪的特殊的数据结构

自从用了guava神清气爽,apache common包比较早很多JDK1.5之后的属性在之前的工具类中并未采纳,比如cache、List等,guava确实优势比较明显


想问一下guava里哪些特性用的比较多?

guava做得确实比commons好,我在项目当中也是使用guava较多,guava的文章也写了几篇待发,例如commons的collections还没支持泛型,而且guava对数据结构的支持和种类也比commons多,但由于commons比较经典,所以先写了commons
5 楼 zjhlht 2013-10-16  
不错!当API进行整理一下!
4 楼 white_crucifix 2013-10-16  
cywhoyi 写道
white_crucifix 写道
恩,google的guava lib里封装了很多奇奇怪怪的特殊的数据结构

自从用了guava神清气爽,apache common包比较早很多JDK1.5之后的属性在之前的工具类中并未采纳,比如cache、List等,guava确实优势比较明显


想问一下guava里哪些特性用的比较多?
3 楼 cywhoyi 2013-10-16  
white_crucifix 写道
恩,google的guava lib里封装了很多奇奇怪怪的特殊的数据结构

自从用了guava神清气爽,apache common包比较早很多JDK1.5之后的属性在之前的工具类中并未采纳,比如cache、List等,guava确实优势比较明显
2 楼 yixiandave 2013-10-16  
commons.lang里面也就ArrayUtils用的比较多,其他的确实忽略了。
1 楼 white_crucifix 2013-10-15  
恩,google的guava lib里封装了很多奇奇怪怪的特殊的数据结构

相关推荐

Global site tag (gtag.js) - Google Analytics