commons-lang3常用工具类

文章目录

commons-lang3 是Apache提供的一个java.lang包的增强版本,Lang3为java.lang API提供了许多帮助程序实用程序,特别是字符串操作方法,基本数值方法,对象反射,并发,创建和序列化以及系统属性。此外,它还包含对java.util.Date的基本增强,以及一系列专用于构建方法的实用程序,例如hashCode,toString和equals。

官方教程:https://commons.apache.org/proper/commons-lang/index.html

官方API:https://commons.apache.org/proper/commons-lang/javadocs/api-release/index.html

导包

Apache库不包含常规java库。你必须添加它们。将此依赖项添加到pom.xml以访问该方法。

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.12.0</version>
</dependency>

只介绍常用的几个类和常用方法,详细的功能可以参考官方提供的API

数字处理类(NumberUtils)

//预设了基本常量,可以直接用
public static final Long LONG_ZERO = 0L;
public static final Long LONG_ONE = 1L;
public static final Long LONG_MINUS_ONE = -1L;
public static final Integer INTEGER_ZERO = 0;
public static final Integer INTEGER_ONE = 1;
public static final Integer INTEGER_TWO = 2;
public static final Integer INTEGER_MINUS_ONE = -1;
public static final Short SHORT_ZERO = Short.valueOf((short)0);
public static final Short SHORT_ONE = Short.valueOf((short)1);
public static final Short SHORT_MINUS_ONE = Short.valueOf((short)-1);
public static final Byte BYTE_ZERO = 0;
public static final Byte BYTE_ONE = 1;
public static final Byte BYTE_MINUS_ONE = -1;
public static final Double DOUBLE_ZERO = 0.0D;
public static final Double DOUBLE_ONE = 1.0D;
public static final Double DOUBLE_MINUS_ONE = -1.0D;
public static final Float FLOAT_ZERO = 0.0F;
public static final Float FLOAT_ONE = 1.0F;
public static final Float FLOAT_MINUS_ONE = -1.0F;
public static final Long LONG_INT_MAX_VALUE = 2147483647L;
public static final Long LONG_INT_MIN_VALUE = -2147483648L;

//判断字符串中是否是数字,返回的结果是true或者false
NumberUtils.isNumber("5.55");	//结果是true
NumberUtils.isNumber("a5");		//结果是false
NumberUtils.isNumber("0000000000555");//结果是true

//判断字符串中是否全部都是数字,不算小数点,返回的结果是true或者
falseNumberUtils.isDigits("0000000000.555");	//false
NumberUtils.isDigits("0000000000555");			//true

//字符串转换为数字
NumberUtils.toInt("5");
NumberUtils.toLong("5");
NumberUtils.toByte("3");
NumberUtils.toFloat("132");
NumberUtils.toDouble("4");
NumberUtils.toShort("3");

字符串处理类(StringUtils)

//预设了一些字符串常量,可以直接用
private static final int STRING_BUILDER_SIZE = 256;
public static final String SPACE = " ";
public static final String EMPTY = "";
public static final String LF = "\n";
public static final String CR = "\r";

//判断字符串中是否包含某个字符串(方法比较多不一一列举,自行探索)
StringUtils.contains("abcdefghijklmn", "ABC");				// false
StringUtils.containsIgnoreCase("abcdefghijklmn", "ABC");	// true

//统计一个字符串中,某个字符串出现的次数
StringUtils.countMatches("abcdefga", "a");					// 2

//删除字符串中的梭有空格
StringUtils.deleteWhitespace("   ab  c  ");					// "abc"

//比较两字符串,返回不同之处。确切的说是返回第二个参数中与第一个参数所不同的字符串
StringUtils.difference("abcde", "abxyz");					// "xyz"

//检查起始字符串是否匹配
StringUtils.startsWith("abcdef", "abc");					// true
StringUtils.startsWithIgnoreCase("ABCDEF", "abc");			// true
StringUtils.startsWithAny("abcxyz", new String[] {null, "xyz", "abc"});// true

//检查字符串结尾后缀是否匹配
StringUtils.endsWith("abcdef", "def");						// true
StringUtils.endsWithIgnoreCase("ABCDEF", "def");			// true
StringUtils.endsWithAny("abcxyz", new String[] {null, "xyz", "abc"});// true

//判断两字符串是否相同
StringUtils.equals("abc", "abc");							// true
StringUtils.equalsIgnoreCase("abc", "ABC");					// true

//比较字符串数组内的所有元素的字符序列,起始一致则返回一致的字符串,若无则返回""
StringUtils.getCommonPrefix(new String[] {"abcde", "abxyz"});// "ab"

//正向查找字符在字符串中第一次出现的位置
StringUtils.indexOf("aabaabaa", "b");						// 2
StringUtils.indexOf("aabaabaa", "b", 3);					// 5(从角标3后查找)
StringUtils.ordinalIndexOf("aabaabaa", "a", 3);				// 1(查找第n次出现的位置)

//反向查找字符串第一次出现的位置
StringUtils.lastIndexOf("aabaabaa", 'b');					// 5
StringUtils.lastIndexOf("aabaabaa", 'b', 4);				// 2
StringUtils.lastOrdinalIndexOf("aabaabaa", "ab", 2);		// 1

//判断字符串大写、小写
StringUtils.isAllUpperCase("ABC");							// true
StringUtils.isAllLowerCase("abC");							// false

//判断是否为空(注:isBlank与isEmpty 区别)
StringUtils.isBlank(null);									// true
StringUtils.isBlank("");									// true
StringUtils.isBlank(" ");									// true
StringUtils.isNoneBlank(" ", "bar");						// false

StringUtils.isEmpty(null);
StringUtils.isEmpty("");									// true
StringUtils.isEmpty(" ");									// false
StringUtils.isNoneEmpty(" ", "bar");						// true

//判断字符串数字
StringUtils.isNumeric("123");								// true
StringUtils.isNumeric("12 3");								// false (不识别运算符号、小数点、空格……)
StringUtils.isNumericSpace("12 3");							// true

//数组中加入分隔符号
StringUtils.join([1, 2, 3],;);							// "1;2;3"

//大小写转换
StringUtils.upperCase("aBc");								// "ABC"
StringUtils.lowerCase("aBc");								// "abc"
StringUtils.swapCase("The dog has a BONE");// "tHE DOG HAS A bone"

//替换字符串内容……(replacePattern、replceOnce)
StringUtils.replace("aba", "a", "z");						// "zbz"
StringUtils.overlay("abcdef", "zz", 2, 4);					// "abzzef"(指定区域)
StringUtils.replaceEach("abcde", new String[]{"ab", "d"},
                        new String[]{"w", "t"});			// "wcte"(多组指定替换ab->w,d->t)

//重复字符
StringUtils.repeat(‘e‘, 3);									// "eee"

//反转字符串
StringUtils.reverse("bat");									// "tab"

//删除某字符
StringUtils.remove("queued", ‘u‘);							// "qeed"

//分割字符串
StringUtils.split("a..b.c",.);							// ["a", "b", "c"]
StringUtils.split("ab:cd:ef", ":", 2);						// ["ab", "cd:ef"]
StringUtils.splitByWholeSeparator("ab-!-cd-!-ef", "-!-", 2);// ["ab", "cd-!-ef"]
StringUtils.splitByWholeSeparatorPreserveAllTokens("ab::cd:ef", ":");//-["ab"," ","cd","ef"]

//去除首尾空格,类似trim……(stripStart、stripEnd、stripAll、stripAccents)
StringUtils.strip(" ab c ");								// "ab c"
StringUtils.stripToNull(null);								// null
StringUtils.stripToEmpty(null);								// ""

//截取字符串
StringUtils.substring("abcd", 2);							// "cd"
StringUtils.substring("abcdef", 2, 4);						// "cd"

//left、right从左(右)开始截取n位字符
StringUtils.left("abc", 2);									// "ab"
StringUtils.right("abc", 2);								// "bc"
//从第n位开始截取m位字符       n  m
StringUtils.mid("abcdefg", 2, 4);							// "cdef"

随机生成

//随机生成一个范围内的整型
RandomUtils.nextInt(6,10);			// [6~10]内的整型

//随机生成一个长度的整型
RandomStringUtils.randomNumeric(10);// 长度为10的整型

日期类(DateUtils)

// 指定日期后加n天
DateUtils.addDays(new Date(), 5);	//加5天

//将字符串转换为日期
DateUtils.parseDate("2021-10-10", "yyyy-MM-dd");	//将字符串2021-10-10转换为date

//判断是否是同一天
DateUtils.isSameDay(date1, date2);

参考:https://www.jianshu.com/p/1886903ed14c

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值