diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ac5ef3f3..d0c81ae0b 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ * 【core 】 改进XmlUtil.xmlToBean,支持xml转bean时父节点忽略大小写 * 【core 】 优化ArrayUtil的空判断(pr#2969@Github) * 【extra 】 优化SpringUtil在非Spring环境下的异常(issue#2835@Github) +* 【core 】 StrUtil增加commonPrefix和commonSuffix方法(pr#3007@Github) +* 【core 】 NumberUtil增加重载parseXXX方法, 解析失败返回默认值(pr#3007@Github) ### 🐞Bug修复 * 【crypto】 修复NoSuchMethodError未捕获问题(issue#2966@Github) diff --git a/hutool-core/src/main/java/cn/hutool/core/text/CharSequenceUtil.java b/hutool-core/src/main/java/cn/hutool/core/text/CharSequenceUtil.java index 107476613..51d980afd 100755 --- a/hutool-core/src/main/java/cn/hutool/core/text/CharSequenceUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/text/CharSequenceUtil.java @@ -2142,9 +2142,9 @@ public class CharSequenceUtil { */ public static String subWithLength(String input, int fromIndex, int length) { final int toIndex; - if(fromIndex < 0){ + if (fromIndex < 0) { toIndex = fromIndex - length; - }else{ + } else { toIndex = fromIndex + length; } return sub(input, fromIndex, toIndex); @@ -4587,18 +4587,17 @@ public class CharSequenceUtil { } /** + * 字符串1和字符串2的公共前缀 * - * @param str1 字符串1 - * @param str2 字符串2 - * @return 字符串1和字符串2的公共前缀 + * @param str1 字符串1 + * @param str2 字符串2 + * @return 字符串1和字符串2的公共前缀 */ public static CharSequence commonPrefix(CharSequence str1, CharSequence str2) { - if (isBlank(str1) || isBlank(str2)) { return EMPTY; } - - int minLength = Math.min(str1.length(), str2.length()); + final int minLength = Math.min(str1.length(), str2.length()); int index = 0; @@ -4607,36 +4606,28 @@ public class CharSequenceUtil { if (str1.charAt(index) != str2.charAt(index)) { break; } - } - return str1.subSequence(0, index); } /** + * 字符串1和字符串2的公共后缀 * - * @param str1 字符串1 - * @param str2 字符串2 - * @return 字符串1和字符串2的公共后缀 + * @param str1 字符串1 + * @param str2 字符串2 + * @return 字符串1和字符串2的公共后缀 */ public static CharSequence commonSuffix(CharSequence str1, CharSequence str2) { - if (isBlank(str1) || isBlank(str2)) { return EMPTY; } - int str1Index = str1.length() - 1; - int str2Index = str2.length() - 1; - for (; str1Index >= 0 && str2Index >= 0; str1Index--, str2Index--) { - if (str1.charAt(str1Index) != str2.charAt(str2Index)) { break; } - } - return str1.subSequence(str1Index + 1, str1.length()); } diff --git a/hutool-core/src/test/java/cn/hutool/core/util/NumberUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/util/NumberUtilTest.java index 80bfa5ee9..dd5efc9e4 100644 --- a/hutool-core/src/test/java/cn/hutool/core/util/NumberUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/util/NumberUtilTest.java @@ -413,18 +413,18 @@ public class NumberUtilTest { // -------------------------- Parse failed ----------------------- - Long v1 = NumberUtil.parseLong(null, null); + final Long v1 = NumberUtil.parseLong(null, null); assertThat(v1, nullValue()); - Long v2 = NumberUtil.parseLong(StrUtil.EMPTY, null); + final Long v2 = NumberUtil.parseLong(StrUtil.EMPTY, null); assertThat(v2, nullValue()); - Long v3 = NumberUtil.parseLong("L3221", 1233L); + final Long v3 = NumberUtil.parseLong("L3221", 1233L); assertThat(v3, equalTo(1233L)); // -------------------------- Parse success ----------------------- - Long v4 = NumberUtil.parseLong("1233L", null); + final Long v4 = NumberUtil.parseLong("1233L", null); assertThat(v4, equalTo(1233L)); }