diff --git a/CHANGELOG.md b/CHANGELOG.md
index 886a87c3e..a89c37fbf 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -3,7 +3,7 @@
-------------------------------------------------------------------------------------------------------------
-# 5.7.5 (2021-07-18)
+# 5.7.5 (2021-07-19)
### 🐣新特性
* 【core 】 DateUtil增加ceiling重载,可选是否归零毫秒
@@ -24,6 +24,7 @@
* 【core 】 修复DateUtil.format格式化Instant报错问题(issue#I40CY2@Gitee)
* 【core 】 修复StrUtil.toUnderlineCase大写问题(issue#I40CGS@Gitee)
* 【jwt 】 修复JWT.validate报错问题(issue#I40MR2@Gitee)
+* 【core 】 修复StrUtil.brief越界问题
-------------------------------------------------------------------------------------------------------------
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 06b1dbebd..bf147b94e 100644
--- a/hutool-core/src/main/java/cn/hutool/core/text/CharSequenceUtil.java
+++ b/hutool-core/src/main/java/cn/hutool/core/text/CharSequenceUtil.java
@@ -4245,22 +4245,42 @@ public class CharSequenceUtil {
/**
* 将给定字符串,变成 "xxx...xxx" 形式的字符串
*
+ *
+ * - abcdef 5 -》 a...f
+ * - abcdef 4 -》 a..f
+ * - abcdef 3 -》 a.f
+ * - abcdef 2 -》 a.
+ * - abcdef 1 -》 a
+ *
+ *
* @param str 字符串
- * @param maxLength 最大长度
+ * @param maxLength 结果的最大长度
* @return 截取后的字符串
*/
public static String brief(CharSequence str, int maxLength) {
if (null == str) {
return null;
}
- if (maxLength <= 0 || str.length() <= maxLength) {
+ final int strLength = str.length();
+ if (maxLength <= 0 || strLength <= maxLength) {
return str.toString();
}
- int w = maxLength / 2;
- int l = str.length() + 3;
+ // since 5.7.5,特殊长度
+ switch (maxLength){
+ case 1:
+ return String.valueOf(str.charAt(0));
+ case 2:
+ return str.charAt(0) + ".";
+ case 3:
+ return str.charAt(0) + "." + str.charAt(str.length() - 1);
+ }
+
+ final int w = maxLength / 2;
final String str2 = str.toString();
- return format("{}...{}", str2.substring(0, maxLength - w), str2.substring(l - w));
+ return format("{}...{}",
+ str2.substring(0, maxLength - w),
+ str2.substring(strLength - w + 3));
}
/**
diff --git a/hutool-core/src/test/java/cn/hutool/core/util/StrUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/util/StrUtilTest.java
index 4e4f7971c..260a092a7 100644
--- a/hutool-core/src/test/java/cn/hutool/core/util/StrUtilTest.java
+++ b/hutool-core/src/test/java/cn/hutool/core/util/StrUtilTest.java
@@ -482,6 +482,38 @@ public class StrUtilTest {
Assert.assertEquals(brief.length(), maxLength);
}
+ @Test
+ public void briefTest2() {
+ String str = "123";
+ int maxLength = 3;
+ String brief = StrUtil.brief(str, maxLength);
+ Assert.assertEquals("123", brief);
+
+ maxLength = 2;
+ brief = StrUtil.brief(str, maxLength);
+ Assert.assertEquals("1.", brief);
+
+ maxLength = 1;
+ brief = StrUtil.brief(str, maxLength);
+ Assert.assertEquals("1", brief);
+ }
+
+ @Test
+ public void briefTest3() {
+ String str = "123abc";
+ int maxLength = 3;
+ String brief = StrUtil.brief(str, maxLength);
+ Assert.assertEquals("1.c", brief);
+
+ maxLength = 2;
+ brief = StrUtil.brief(str, maxLength);
+ Assert.assertEquals("1.", brief);
+
+ maxLength = 1;
+ brief = StrUtil.brief(str, maxLength);
+ Assert.assertEquals("1", brief);
+ }
+
@Test
public void filterTest() {
final String filterNumber = StrUtil.filter("hutool678", CharUtil::isNumber);