mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
fix brief bug
This commit is contained in:
parent
d8f393f7d9
commit
56e107ebda
@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
-------------------------------------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
# 5.7.5 (2021-07-18)
|
# 5.7.5 (2021-07-19)
|
||||||
|
|
||||||
### 🐣新特性
|
### 🐣新特性
|
||||||
* 【core 】 DateUtil增加ceiling重载,可选是否归零毫秒
|
* 【core 】 DateUtil增加ceiling重载,可选是否归零毫秒
|
||||||
@ -24,6 +24,7 @@
|
|||||||
* 【core 】 修复DateUtil.format格式化Instant报错问题(issue#I40CY2@Gitee)
|
* 【core 】 修复DateUtil.format格式化Instant报错问题(issue#I40CY2@Gitee)
|
||||||
* 【core 】 修复StrUtil.toUnderlineCase大写问题(issue#I40CGS@Gitee)
|
* 【core 】 修复StrUtil.toUnderlineCase大写问题(issue#I40CGS@Gitee)
|
||||||
* 【jwt 】 修复JWT.validate报错问题(issue#I40MR2@Gitee)
|
* 【jwt 】 修复JWT.validate报错问题(issue#I40MR2@Gitee)
|
||||||
|
* 【core 】 修复StrUtil.brief越界问题
|
||||||
|
|
||||||
-------------------------------------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
@ -4245,22 +4245,42 @@ public class CharSequenceUtil {
|
|||||||
/**
|
/**
|
||||||
* 将给定字符串,变成 "xxx...xxx" 形式的字符串
|
* 将给定字符串,变成 "xxx...xxx" 形式的字符串
|
||||||
*
|
*
|
||||||
|
* <ul>
|
||||||
|
* <li>abcdef 5 -》 a...f</li>
|
||||||
|
* <li>abcdef 4 -》 a..f</li>
|
||||||
|
* <li>abcdef 3 -》 a.f</li>
|
||||||
|
* <li>abcdef 2 -》 a.</li>
|
||||||
|
* <li>abcdef 1 -》 a</li>
|
||||||
|
* </ul>
|
||||||
|
*
|
||||||
* @param str 字符串
|
* @param str 字符串
|
||||||
* @param maxLength 最大长度
|
* @param maxLength 结果的最大长度
|
||||||
* @return 截取后的字符串
|
* @return 截取后的字符串
|
||||||
*/
|
*/
|
||||||
public static String brief(CharSequence str, int maxLength) {
|
public static String brief(CharSequence str, int maxLength) {
|
||||||
if (null == str) {
|
if (null == str) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if (maxLength <= 0 || str.length() <= maxLength) {
|
final int strLength = str.length();
|
||||||
|
if (maxLength <= 0 || strLength <= maxLength) {
|
||||||
return str.toString();
|
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();
|
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));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -482,6 +482,38 @@ public class StrUtilTest {
|
|||||||
Assert.assertEquals(brief.length(), maxLength);
|
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
|
@Test
|
||||||
public void filterTest() {
|
public void filterTest() {
|
||||||
final String filterNumber = StrUtil.filter("hutool678", CharUtil::isNumber);
|
final String filterNumber = StrUtil.filter("hutool678", CharUtil::isNumber);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user