org.apache.commons.commons-lang3工具类(一)

https://blog.csdn.net/qq_31289187/article/details/85343068

本文只是简单的介绍了commons-lang3中的SystemUtils、StringUtils、ArrayUtils这三个工具类中常用的方法,我已经例举了很多方法。commons-lang3中可以让我们写的代码更加的优雅、提供开发效率,最重要的是我们自己写的工具类有可能出现奇怪的问题。对了,Google的guava也是非常好用的工具包!

       代码中的注释我写的很详细了,欢迎大家关注我朋友的微信公众号,里面有很多大咖的学习资源,让我们一起学习一起浪,还有2天就2019年了,提前祝大家新年快乐,工资翻倍!!!哈哈哈!
1、StringUtils

    package com.cn.dl;
     
    import org.apache.commons.lang3.StringUtils;
     
    /**
     *<dependency>
          <groupId>org.apache.commons</groupId>
          <artifactId>commons-lang3</artifactId>
          <version>3.8.1</version>
      </dependency>
     *
     * commons-lang3中StringUtils常用来操作字符串的方法
     * Created by yanshao on 2018/12/29.
     */
    public class CommonsLang3_StringUtils_Demo {
     
     
        public static void main(String[] args) {
     
            String str1 = null;
            String str2 = "abs";
            //判断传入的多个字符串是否为null或者长度有0
            System.out.println("isAllEmpty>>>>"+StringUtils.isAllEmpty(str1,str2));
     
            //boolean isNotEmpty(CharSequence cs)
            //boolean isAnyEmpty(CharSequence... css)
            //boolean isNoneEmpty(CharSequence... css)
            //isAnyBlank(CharSequence... css)
            //isNoneBlank(CharSequence... css)
            //isAllBlank(CharSequence... css)
     
            //判断传入的字符串是否为null或者长度有0
            System.out.println("isEmpty>>>>"+StringUtils.isEmpty(str1));
     
            String str3 = "  ";
            //判断传入的字符串==null或者字符串不存在非空白字符,返回true
            System.out.println("isBlank>>>>"+StringUtils.isBlank(str3));
     
            String str4 = "1234.123";
            //判断传入的字符串是否每个字符是否为数字,如果有一个字符串不是数字,返回false
            System.out.println("isNumeric>>>>"+StringUtils.isNumeric(str4));
     
     
            //比较字符串的大小,比较的每个字符的acill的大小
            String len1 = "azcd";
            String len2 = "abcdefg";
            System.out.println("compare>>>>"+StringUtils.compare(len1,len2));
     
            //替换字符串的中间字符
            String strs1 = "yanshao@gamil.com";
            System.out.println("abbreviateMiddle>>>>"+StringUtils.abbreviateMiddle(strs1,"****",10));
     
            //两边添加字符串,返回的长度为size=10的字符串
            String strs2 = "12";
            System.out.println("center>>>>"+StringUtils.center(strs2,10,"@@"));
     
     
            //返回从0到str.length-1位置的字符串,这个在字符串通过特殊字符拼接最后返回拼接字符串特别有用
            String strs3 = "yanshao@man@25@";
            System.out.println("chop>>>>"+StringUtils.chop(strs3));
     
            //判断字符串中是否包含指定字符串其中一个,包含返回true
            String strs4 = "yanshao@abc@&&error@yes";
            System.out.println("containsAny>>>>"+StringUtils.containsAny(strs4,"abe","bbb","aaaa"));
     
            //判断字符串中是否包含指定字符串中其中一个字符,不包含返回true
            System.out.println("containsNone>>>>"+StringUtils.containsNone(strs4,"2222"));
     
            //忽略大小写
            System.out.println("containsIgnoreCase>>>>"+StringUtils.containsIgnoreCase(strs4,"YAN"));
     
            //字符串中只包含指定字符,返回true
            System.out.println("containsOnly>>>"+StringUtils.containsOnly("errorabc","errorabc"));
     
            //判断字符串中包含指定字符串的次数
            System.out.println("countMatches>>>>"+StringUtils.countMatches(strs4,"abc"));
     
            //判断是否包含空白字符,包含返回true
            System.out.println("containsWhitespace>>>>"+StringUtils.containsWhitespace("yan shao"));
     
            //判断两个字符串是否相同,相同返回true
            System.out.println("equals>>>"+StringUtils.equals("str1","str1"));
     
            //判断字符串是否和其它字符串中的一个是否相同,有相同返回true
            System.out.println("endsWithAny>>>"+StringUtils.endsWithAny("str1",str1,str2,str3));
     
            // TODO: 2018/12/29 忽略大小写
            //boolean equalsIgnoreCase(CharSequence str1, CharSequence str2)
            //equalsAnyIgnoreCase(CharSequence string, CharSequence... searchStrings)
     
            //判断字符串是否也固定字符串结尾,是的话返回true
            System.out.println("endsWith>>>"+StringUtils.endsWith(strs4,"yes"));
     
            //判断是否也其中一个字符串结尾,是的话返回true
            System.out.println("endsWithAny>>>"+StringUtils.endsWithAny(strs4,str1,str2,str3));
     
            // TODO: 2018/12/29 忽略大小写
            //boolean endsWithIgnoreCase(CharSequence str, CharSequence suffix)
     
            //指定截取字符串末尾的字符串
            System.out.println("removeEnd>>>>"+StringUtils.removeEnd(strs4,"abc"));
     
            //获取字符串中公共开头的部分
            System.out.println("getCommonPrefix>>>"+StringUtils.getCommonPrefix("abc","acc","acd","abd"));
     
            //截取字符串,指定位置
            System.out.println("substring>>>>"+StringUtils.substring(strs4,0,4));
     
            //从最后一次出现指定字符的位置+1开始截取
            System.out.println("substringAfterLast>>>>"+StringUtils.substringAfterLast(strs4,"e"));
     
            // TODO: 2018/12/29 其它截取函数
            //String substringAfter(String str, String separator)
            //String substringAfterLast(String str, String separator)
            //String substringBetween(String str, String open, String close)
     
            //查找指定字符串所在的位置
            System.out.println("indexOf>>>>"+StringUtils.indexOf(strs4,"yes"));
     
            //查找指定字符所在的位置,第一次出现
            System.out.println("indexOf>>>"+StringUtils.indexOf(strs4,97,0));
     
            //查找字符串中的每个字符,是否在指定的字符串中
            //如果ynsho@abc&er中包含strs4中的每一个字符,返回-1
            //如果ynsho@abc&er中包含strs4中不包含指定字符,返回str4中当前字符的位置
            // TODO: 2018/12/29 debug跟一遍代码就知道了
            System.out.println("indexOfAnyBut>>>>"+StringUtils.indexOfAnyBut(strs4,"ynho@abc&er"));
     
     
            //返回两个字符串不相同的位置
            System.out.println("indexOfDifference>>>>"+StringUtils.indexOfDifference("abcd","abce"));
     
     
            System.out.println("lowerCase>>>>"+StringUtils.lowerCase("China"));
     
            //判断字符串是否全部为字母
            System.out.println("isAlpha>>>>"+StringUtils.isAlpha("China123"));
     
            //判断字符串是否只包含字母和空格
            System.out.println("isAlphaSpace>>>>"+StringUtils.isAlphaSpace("China  "));
     
            //判断是否只包含字母和数字
            System.out.println("isAlphanumeric>>>"+StringUtils.isAlphanumeric("China123"));
     
     
        }
     
    }

2、SystemUtils

    package com.cn.dl;
     
    import org.apache.commons.lang3.SystemUtils;
     
    /**
     *<dependency>
     <groupId>org.apache.commons</groupId>
     <artifactId>commons-lang3</artifactId>
     <version>3.8.1</version>
     </dependency>
     *
     * commons-lang3中SystemUtils常用的方法
     * Created by yanshao on 2018/12/29.
     */
    // TODO: 2018/12/29 SystemUtils在实际生成中用到的很少,点进去看到方法名基本都知道这个方法是做什么的了
    public class CommonsLang3_SystemUtils_Demo {
     
        public static void main(String[] args) {
     
            //获取java jre所在的目录
            System.out.println("getJavaHome>>>>"+SystemUtils.getJavaHome());
     
            //获取当前工程所在的目录
            System.out.println(SystemUtils.getUserDir());
     
            // TODO: 2018/12/29 判断系统类型,这个方法在根据系统类型,获取指定目录很有用
            System.out.println("IS_OS_LINUX>>>"+SystemUtils.IS_OS_LINUX);
            System.out.println("IS_OS_WINDOWS>>>"+SystemUtils.IS_OS_WINDOWS);
            System.out.println("IS_OS_MAC>>>"+SystemUtils.IS_OS_MAC);
     
     
            //获取当前所在的地区
            System.out.println("USER_COUNTRY>>>"+SystemUtils.USER_COUNTRY);
     
            //当前使用的语言类型
            System.out.println("USER_LANGUAGE>>>"+SystemUtils.USER_LANGUAGE);
     
     
        }
     
    }

3、ArrayUtils

    package com.cn.dl;
     
    import org.apache.commons.lang3.ArrayUtils;
     
    /**
     *<dependency>
     <groupId>org.apache.commons</groupId>
     <artifactId>commons-lang3</artifactId>
     <version>3.8.1</version>
     </dependency>
     *
     * commons-lang3中ArrayUtils常用方法
     * Created by yanshao on 2018/12/29.
     */
    public class CommonsLang3_ArrayUtils_Demo {
     
        public static void main(String[] args) {
     
            //ArrayUtils add、addAll的用法
            boolean[] booleens = new boolean[0];
            booleens = ArrayUtils.add(booleens,Boolean.TRUE);
            booleens = ArrayUtils.add(booleens,Boolean.TRUE);
            booleens = ArrayUtils.addAll(booleens,Boolean.TRUE,Boolean.TRUE,Boolean.TRUE,Boolean.TRUE);
            System.out.println("booleens>>>"+ArrayUtils.toString(booleens));
     
            //ArrayUtils remove、removeAll的用法
            booleens = ArrayUtils.remove(booleens,0);
            System.out.println("booleens>>>"+ArrayUtils.toString(booleens));
            booleens = ArrayUtils.removeAll(booleens,0,1,2,3);
            System.out.println("booleens>>>"+ArrayUtils.toString(booleens));
     
            //ArrayUtils insert的用法
            booleens = ArrayUtils.insert(1,booleens,Boolean.FALSE);
            System.out.println("booleens>>>"+ArrayUtils.toString(booleens));
     
            //ArrayUtils clone的用法
            boolean[] booleens1 = ArrayUtils.clone(booleens);
            booleens1 = ArrayUtils.add(booleens1,Boolean.TRUE);
            System.out.println("booleens>>>"+ArrayUtils.toString(booleens));
            System.out.println("booleens1>>>"+ArrayUtils.toString(booleens1));
     
            //返回第一次出现false的位置
            System.out.println("indexOf>>>>"+ArrayUtils.indexOf(booleens,Boolean.FALSE));
     
            //判断数组是否为空
            System.out.println("isEmpty>>>>"+ArrayUtils.isEmpty(booleens));
     
            //判断指定的下标是否有效
            System.out.println("isArrayIndexValid>>>>"+ArrayUtils.isArrayIndexValid(new Boolean[10],1));;
     
            //判断两个数组的长度是否相同
            System.out.println("isSameLength>>>>"+ArrayUtils.isSameLength(booleens1,booleens));
     
            //判断两个对象的类型是否相同
            System.out.println("isSameType>>>>"+ArrayUtils.isSameType(booleens1,booleens));
     
            //判断数组是否有序
            System.out.println("isSorted>>>>"+ArrayUtils.isSorted(booleens));
            System.out.println("isSorted>>>>"+ArrayUtils.isSorted(new Boolean[]{false,true}));
     
            //字符串反转
            ArrayUtils.reverse(booleens);
            System.out.println("booleens>>>"+ArrayUtils.toString(booleens));
     
            booleens1 = ArrayUtils.add(booleens1,Boolean.FALSE);
            System.out.println("booleens1>>>"+ArrayUtils.toString(booleens1));
            // TODO: 2018/12/29  把指定位置以后的元素放在数组的前面,地址位置以前的元素放在数组后面
            ArrayUtils.shift(booleens1,1);
            System.out.println("booleens1>>>"+ArrayUtils.toString(booleens1));
     
     
            //随机交换数组中两两元素的位置
            ArrayUtils.shuffle(booleens1);
            System.out.println("booleens1>>>"+ArrayUtils.toString(booleens1));
     
            //数组截取
            booleens = ArrayUtils.subarray(booleens,0,1);
            System.out.println("booleens>>>"+ArrayUtils.toString(booleens));
     
            //交换数组中两个元素的位置
            ArrayUtils.swap(booleens1,0,3);
            System.out.println("booleens1>>>"+ArrayUtils.toString(booleens1));
     
        }
     
    }

 
 

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

余额充值