Java常用工具类(二)org.apache.commons.lang

本文介绍了Apache Commons Lang库中常用工具类的使用方法,包括字符串处理、数组操作、日期格式化等功能,帮助开发者提高编码效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**
* 1、字符串的空判断  org.aphche.commons.lang.StringUtils

*/

                // Checks if a String is empty ("") or null.
		System.out.println(StringUtils.isEmpty(null)); // true
		System.out.println(StringUtils.isEmpty("")); // true
		System.out.println(StringUtils.isEmpty(" ")); // false
		System.out.println(StringUtils.isEmpty("bob")); // false
		System.out.println(StringUtils.isEmpty("  bob  ")); // false

		// Checks if a String is whitespace, empty ("") or null.
		System.out.println(StringUtils.isBlank(null)); // true
		System.out.println(StringUtils.isBlank("")); // true
		System.out.println(StringUtils.isBlank(" ")); // true
		System.out.println(StringUtils.isBlank("bob")); // false
		System.out.println(StringUtils.isBlank("  bob  ")); // false

/**
* 2、字符串的Trim
*/

		// The String is trimmed using String.trim(). Trim removes start and end characters <= 32. To strip whitespace use strip(String).
		System.out.println(StringUtils.trim(null)); // null
		System.out.println(StringUtils.trim("")); // ""
		System.out.println(StringUtils.trim("     ")); // ""
		System.out.println(StringUtils.trim("abc")); // "abc"
		System.out.println(StringUtils.trim("    abc")); // "abc"
		System.out.println(StringUtils.trim("    abc  ")); // "abc"
		System.out.println(StringUtils.trim("    ab c  ")); // "ab c"

		// Strips whitespace from the start and end of a String.
		System.out.println(StringUtils.strip(null)); // null
		System.out.println(StringUtils.strip("")); // ""
		System.out.println(StringUtils.strip("   ")); // ""
		System.out.println(StringUtils.strip("abc")); // "abc"
		System.out.println(StringUtils.strip("  abc")); // "abc"
		System.out.println(StringUtils.strip("abc  ")); // "abc"
		System.out.println(StringUtils.strip(" abc ")); // "abc"
		System.out.println(StringUtils.strip(" ab c ")); // "ab c"

		// strip(String,String);
		System.out.println(StringUtils.strip("  abcyx", "xyz")); // "  abc"
		System.out.println(StringUtils.strip(null, "*")); // null
		System.out.println(StringUtils.strip("", "*"));// ""
		System.out.println(StringUtils.strip("abc", null)); // "abc"
		System.out.println(StringUtils.strip("  abc", null)); // "abc"
		System.out.println(StringUtils.strip("abc  ", null)); // "abc"
		System.out.println(StringUtils.strip(" abc ", null)); // "abc"
		System.out.println(StringUtils.strip("  abcyx", "xyz")); // "  abc"

		// Strips any of a set of characters from the start of a String.
		System.out.println(StringUtils.stripStart("yxabcxyz  ", "xyz")); // "abcxyz  "

		// Strips any of a set of characters from the end of a String.
		System.out.println(StringUtils.stripEnd("  xyzabcyx", "xyz")); // "  xyzabc"


/**
* 3、字符串的分割 
*/
		//默认半角空格分割  
		String str1 = "aaa bbb ccc";
		String[] dim1 = StringUtils.split(str1); // => ["aaa", "bbb", "ccc"]
		System.out.println(StringUtils.split("abc def")); // ["abc", "def"]
		System.out.println(StringUtils.split("abc  def")); // ["abc", "def"]
		System.out.println(StringUtils.split(" abc ")); // ["abc"]
		  
		//指定分隔符  
		String str2 = "aaa,bbb,ccc";  
		String[] dim2 = StringUtils.split(str2, ","); // => ["aaa", "bbb", "ccc"]  
		  
		//去除空字符串  
		String str3 = "aaa,,bbb";  
		String[] dim3 = StringUtils.split(str3, ","); // => ["aaa", "bbb"]  
		  
		System.out.println(dim3.length);//2  
		System.out.println(dim3[0]);//"aaa"  
		System.out.println(dim3[1]);//"bbb"  
		  
		//包含空字符串  
		String str4 = "aaa,,bbb";  
		String[] dim4 = StringUtils.splitPreserveAllTokens(str4, ","); // => ["aaa", "", "bbb"]  
		System.out.println(StringUtils.splitPreserveAllTokens("::cd:ef", ":"));// ["", "", cd", "ef"]
		System.out.println(StringUtils.splitPreserveAllTokens(":cd:ef:", ":"));// ["", cd", "ef", ""]
		  
		//Splits the provided text into an array with a maximum length.
		//A zero or negative value implies no limit
		String str5 = "aaa,bbb,ccc";  
		String[] dim5 = StringUtils.split(str5, ",", 2); // => ["aaa", "bbb,ccc"]  
		System.out.println(StringUtils.split("ab:cd:ef", ":", 0));//["ab", "cd", "ef"]
		System.out.println(StringUtils.split("ab:cd:ef", ":", 2));//["ab", "cd:ef"]


/**
* 4、字符串的连接 
*/

		//数组元素拼接  
		String[] array = {"aaa", "bbb", "ccc"};  
		StringUtils.join(array, ",");   //"aaa,bbb,ccc"  
		StringUtils.join(array, "--");// "aaa--bbb--ccc"  
		StringUtils.join(array, "");//"aaabbbccc"
		
		//集合元素拼接,the separator character to use, null treated as "".  
		List<String> list = new ArrayList<String>();  
		list.add("aaa");  
		list.add("bbb");  
		list.add("ccc");  
		StringUtils.join(list, ","); //"aaa,bbb,ccc" 

/**
* 5、字符串的Escape  org.apache.commons.lang.StringEscapeUtils
*/

		System.out.println(StringEscapeUtils.escapeCsv("测试测试哦"));//"测试测试哦"  
		System.out.println(StringEscapeUtils.escapeCsv("测试,测试哦"));//"\"测试,测试哦\""  
		System.out.println(StringEscapeUtils.escapeCsv("测试\n测试哦"));//"\"测试\n测试哦\""  
		  
//		System.out.println(StringEscapeUtils.escapeHtml4("测试测试哦"));//"<p>测试测试哦</p>"  
		System.out.println(StringEscapeUtils.escapeJava("\"rensaninng\",欢迎您!"));//"\"rensaninng\"\uFF0C\u6B22\u8FCE\u60A8\uFF01"  
		  
//		System.out.println(StringEscapeUtils.escapeEcmaScript("测试'测试哦"));//"\u6D4B\u8BD5\'\u6D4B\u8BD5\u54E6"  
		System.out.println(StringEscapeUtils.escapeXml("<tt>\"bread\" & \"butter\"</tt>"));//"<tt>"bread" & "butter"</tt>"  		

/**
* 6、随机数   org.apache.commons.lang.RandomStringUtils
*/

		// 10位英字  
		System.out.println(RandomStringUtils.randomAlphabetic(10));  
		  
		// 10位英数  
		System.out.println(RandomStringUtils.randomAlphanumeric(10));  
		  
		// 10位ASCII码  
		System.out.println(RandomStringUtils.randomAscii(10));  
		  
		// 指定文字10位  
		System.out.println(RandomStringUtils.random(10, "abcde")); 

/**
* 7、数组   org.apache.commons.lang.ArrayUtils
*/

		// 追加元素到数组尾部  
		int[] array1 = {1, 2};  
		array1 = ArrayUtils.add(array1, 3); // => [1, 2, 3]  
		  
		System.out.println(array1.length);//3  
		System.out.println(array1[2]);//3  
		  
		// 删除指定位置的元素  
		int[] array2 = {1, 2, 3};  
		array2 = ArrayUtils.remove(array2, 2); // => [1, 2]  
		  
		System.out.println(array2.length);//2  
		  
		// 截取部分元素   The start index is inclusive, the end index exclusive.
		int[] array3 = {1, 2, 3, 4};  
		array3 = ArrayUtils.subarray(array3, 1, 3); // => [2, 3]  
		  
		System.out.println(array3.length);//2  
		  
		// 数组拷贝  
		String[] array4 = {"aaa", "bbb", "ccc"};  
		String[] copied = (String[]) ArrayUtils.clone(array4); // => {"aaa", "bbb", "ccc"}  
		          
		System.out.println(copied.length);//3         
		  
		// 判断是否包含某元素  Checks if the object is in the given array.
		String[] array5 = {"aaa", "bbb", "ccc", "bbb"};  
		boolean result1 = ArrayUtils.contains(array5, "bbb"); // => true       
		System.out.println(result1);//true  
		  
		// 判断某元素在数组中出现的位置(从前往后,没有返回-1)  
		int result2 = ArrayUtils.indexOf(array5, "bbb"); // => 1       
		System.out.println(result2);//1  
		  
		// 判断某元素在数组中出现的位置(从后往前,没有返回-1)  
		int result3 = ArrayUtils.lastIndexOf(array5, "bbb"); // => 3  
		System.out.println(result3);//3  
		  
		// 数组转Map  
		Map<Object, Object> map = ArrayUtils.toMap(new String[][]{  
		    {"key1", "value1"},  
		    {"key2", "value2"}  
		});  
		System.out.println(map.get("key1"));//"value1"  
		System.out.println(map.get("key2"));//"value2"  
		  
		// 判断数组是否为空  
		Object[] array61 = new Object[0];  
		Object[] array62 = null;  
		Object[] array63 = new Object[]{"aaa"};  
		  
		System.out.println(ArrayUtils.isEmpty(array61));//true   Checks if an array of Objects is empty or null.
		System.out.println(ArrayUtils.isEmpty(array62));//true  
		System.out.println(ArrayUtils.isNotEmpty(array63));//true  true if the array is not empty or not null
		  
		// 判断数组长度是否相等  
		Object[] array71 = new Object[]{"aa", "bb", "cc"};  
		Object[] array72 = new Object[]{"dd", "ee", "ff"};  
		  
		System.out.println(ArrayUtils.isSameLength(array71, array72));//true  
		  
		// 判断数组元素内容是否相等  
		Object[] array81 = new Object[]{"aa", "bb", "cc"};  
		Object[] array82 = new Object[]{"aa", "bb", "cc"};  
		  
		System.out.println(ArrayUtils.isEquals(array81, array82));  
		  
		// Integer[] 转化为 int[]  
		Integer[] array9 = new Integer[]{1, 2};  
		int[] result = ArrayUtils.toPrimitive(array9);  
		  
		System.out.println(result.length);//2  
		System.out.println(result[0]);//1  
		  
		// int[] 转化为 Integer[]   
		int[] array10 = new int[]{1, 2};  
		Integer[] result10 = ArrayUtils.toObject(array10);  
		  
		System.out.println(result.length);//2  
		System.out.println(result10[0].intValue());//1

/**
* 8、日期    org.apache.commons.lang.time.DateUtils
*/

		// 生成Date对象  
		Date date = DateUtils.parseDate("2010/01/01 11:22:33", new String[]{"yyyy/MM/dd HH:mm:ss"});  
		  
		// 10天后  
		Date tenDaysAfter = DateUtils.addDays(date, 10); // => 2010/01/11 11:22:33  
		System.out.println(DateFormatUtils.format(tenDaysAfter, "yyyy/MM/dd HH:mm:ss"));  
		  
		// 前一个月  
		Date prevMonth = DateUtils.addMonths(date, -1); // => 2009/12/01 11:22:33  
		System.out.println(DateFormatUtils.format(prevMonth, "yyyy/MM/dd HH:mm:ss"));  
		  
		// 判断是否是同一天  
		Date date1 = DateUtils.parseDate("2010/01/01 11:22:33", new String[]{"yyyy/MM/dd HH:mm:ss"});  
		Date date2 = DateUtils.parseDate("2010/01/01 22:33:44", new String[]{"yyyy/MM/dd HH:mm:ss"});  
		System.out.println(DateUtils.isSameDay(date1, date2));// true  
		  
		// 日期格式化  
		System.out.println(DateFormatUtils.format(new Date(), "yyyy/MM/dd HH:mm:ss"));



commons-lang3.3.1.jar、Apache Commons包中的一个,包含了一些数据型工具,是java.lang.*的扩展。必须使用的jar包。为JRE5.0+的更好的版本所提供 Jar文件包含的: META-INF/MANIFEST.MFMETA-INF/LICENSE.txtMETA-INF/NOTICE.txtorg.apache.commons.lang.ArrayUtils.class org.apache.commons.lang.BitField.class org.apache.commons.lang.BooleanUtils.class org.apache.commons.lang.CharEncoding.class org.apache.commons.lang.CharRange.class org.apache.commons.lang.CharSet.class org.apache.commons.lang.CharSetUtils.class org.apache.commons.lang.CharUtils.class org.apache.commons.lang.ClassUtils.class org.apache.commons.lang.Entities$ArrayEntityMap.class org.apache.commons.lang.Entities$BinaryEntityMap.class org.apache.commons.lang.Entities$EntityMap.class org.apache.commons.lang.Entities$HashEntityMap.class org.apache.commons.lang.Entities$LookupEntityMap.class org.apache.commons.lang.Entities$MapIntMap.class org.apache.commons.lang.Entities$PrimitiveEntityMap.class org.apache.commons.lang.Entities$TreeEntityMap.class org.apache.commons.lang.Entities.class org.apache.commons.lang.IllegalClassException.class org.apache.commons.lang.IncompleteArgumentException.class org.apache.commons.lang.IntHashMap$Entry.class org.apache.commons.lang.IntHashMap.class org.apache.commons.lang.LocaleUtils.class org.apache.commons.lang.NotImplementedException.class org.apache.commons.lang.NullArgumentException.class org.apache.commons.lang.NumberRange.class org.apache.commons.lang.NumberUtils.class org.apache.commons.lang.ObjectUtils$Null.class org.apache.commons.lang.ObjectUtils.class org.apache.commons.lang.RandomStringUtils.class org.apache.commons.lang.SerializationException.class org.apache.commons.lang.SerializationUtils.class org.apache.commons.lang.StringEscapeUtils.class org.apache.commons.lang.StringUtils.class org.apache.commons.lang.SystemUtils.class org.apache.commons.lang.UnhandledException.class org.apache.commons.lang.Validate.class org.apache.commons.lang.WordUtils.class org.apache.commons.lang.builder.CompareToBuilder.class org.apache.commons.lang.builder.EqualsBuilder.class org.apache.commons.lang.builder.HashCodeBuilder.class org.apache.commons.lang.builder.ReflectionToStringBuilder$1.class org.apache.commons.lang.builder.ReflectionToStringBuilder.class org.apache.commons.lang.builder.StandardToStringStyle.class org.apache.commons.lang.builder.ToStringBuilder.class org.apache.commons.lang.builder.ToStringStyle$DefaultToStringStyle.class org.apache.commons.lang.builder.ToStringStyle$MultiLineToStringStyle.class org.apache.commons.lang.builder.ToStringStyle$NoFieldNameToStringStyle.class org.apache.commons.lang.builder.ToStringStyle$ShortPrefixToStringStyle.class org.apache.commons.lang.builder.ToStringStyle$SimpleToStringStyle.class org.apache.commons.lang.builder.ToStringStyle.class org.apache.commons.lang.enum.Enum$Entry.class org.apache.commons.lang.enum.Enum.class org.apache.commons.lang.enum.EnumUtils.class org.apache.commons.lang.enum.ValuedEnum.class org.apache.commons.lang.enums.Enum$Entry.class org.apache.commons.lang.enums.Enum.class org.apache.commons.lang.enums.EnumUtils.class org.apache.commons.lang.enums.ValuedEnum.class org.apache.commons.lang.exception.ExceptionUtils.class org.apache.commons.lang.exception.Nestable.class org.apache.commons.lang.exception.NestableDelegate.class org.apache.commons.lang.exception.NestableError.class org.apache.commons.lang.exception.NestableException.class org.apache.commons.lang.exception.NestableRuntimeException.class org.apache.commons.lang.math.DoubleRange.class org.apache.commons.lang.math.FloatRange.class org.apache.commons.lang.math.Fraction.class org.apache.commons.lang.math.IntRange.class org.apache.commons.lang.math.JVMRandom.class org.apache.commons.lang.math.LongRange.class org.apache.commons.lang.math.NumberRange.class org.apache.commons.lang.math.NumberUtils.class org.apache.commons.lang.math.RandomUtils.class org.apache.commons.lang.math.Range.class org.apache.commons.lang.mutable.Mutable.class org.apache.commons.lang.mutable.MutableBoolean.class org.apache.commons.lang.mutable.MutableByte.class org.apache.commons.lang.mutable.MutableDouble.class org.apache.commons.lang.mutable.MutableFloat.class org.apache.commons.lang.mutable.MutableInt.class org.apache.commons.lang.mutable.MutableLong.class org.apache.commons.lang.mutable.MutableObject.class org.apache.commons.lang.mutable.MutableShort.class org.apache.commons.lang.text.CompositeFormat.class org.apache.commons.lang.text.StrBuilder$StrBuilderReader.class org.apache.commons.lang.text.StrBuilder$StrBuilderTokenizer.class org.apache.commons.lang.text.StrBuilder$StrBuilderWriter.class org.apache.commons.lang.text.StrBuilder.class org.apache.commons.lang.text.StrLookup$MapStrLookup.class org.apache.commons.lang.text.StrLookup.class org.apache.commons.lang.text.StrMatcher$CharMatcher.class org.apache.commons.lang.text.StrMatcher$CharSetMatcher.class org.apache.commons.lang.text.StrMatcher$NoMatcher.class org.apache.commons.lang.text.StrMatcher$StringMatcher.class org.apache.commons.lang.text.StrMatcher$TrimMatcher.class org.apache.commons.lang.text.StrMatcher.class org.apache.commons.lang.text.StrSubstitutor.class org.apache.commons.lang.text.StrTokenizer.class org.apache.commons.lang.time.DateFormatUtils.class org.apache.commons.lang.time.DateUtils$DateIterator.class org.apache.commons.lang.time.DateUtils.class org.apache.commons.lang.time.DurationFormatUtils$Token.class org.apache.commons.lang.time.DurationFormatUtils.class org.apache.commons.lang.time.FastDateFormat$CharacterLiteral.class org.apache.commons.lang.time.FastDateFormat$NumberRule.class org.apache.commons.lang.time.FastDateFormat$PaddedNumberField.class org.apache.commons.lang.time.FastDateFormat$Pair.class org.apache.commons.lang.time.FastDateFormat$Rule.class org.apache.commons.lang.time.FastDateFormat$StringLiteral.class org.apache.commons.lang.time.FastDateFormat$TextField.class org.apache.commons.lang.time.FastDateFormat$TimeZoneDisplayKey.class org.apache.commons.lang.time.FastDateFormat$TimeZoneNameRule.class org.apache.commons.lang.time.FastDateFormat$TimeZoneNumberRule.class org.apache.commons.lang.time.FastDateFormat$TwelveHourField.class org.apache.commons.lang.time.FastDateFormat$TwentyFourHourField.class org.apache.commons.lang.time.FastDateFormat$TwoDigitMonthField.class org.apache.commons.lang.time.FastDateFormat$TwoDigitNumberField.class org.apache.commons.lang.time.FastDateFormat$TwoDigitYearField.class org.apache.commons.lang.time.FastDateFormat$UnpaddedMonthField.class org.apache.commons.lang.time.FastDateFormat$UnpaddedNumberField.class org.apache.commons.lang.time.FastDateFormat.class org.apache.commons.lang.time.StopWatch.class
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值