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));
}
}