add method

This commit is contained in:
Looly
2021-11-24 23:10:59 +08:00
parent ff8317dd8c
commit 30515f0351
3 changed files with 36 additions and 5 deletions

View File

@@ -23,6 +23,7 @@
* 【core 】 Opt增加ofTry方法pr#1956@Github
* 【core 】 DateUtil.toIntSecond标记为弃用issue#I4JHPR@Gitee
* 【db 】 Db.executeBatch标记一个重载为弃用issue#I4JIPH@Gitee
* 【core 】 增加CharSequenceUtil.subPreGbk重载issue#I4JO2E@Gitee
*
### 🐞Bug修复
* 【core 】 修复FileResource构造fileName参数无效问题issue#1942@Github

View File

@@ -1979,19 +1979,32 @@ public class CharSequenceUtil {
* 截取部分字符串这里一个汉字的长度认为是2
*
* @param str 字符串
* @param len 切割的位置
* @param len bytes切割的位置(包含)
* @param suffix 切割后加上后缀
* @return 切割后的字符串
* @since 3.1.1
*/
public static String subPreGbk(CharSequence str, int len, CharSequence suffix) {
return subPreGbk(str, len, true) + suffix;
}
/**
* 截取部分字符串这里一个汉字的长度认为是2<br>
* 可以自定义halfUp如len为10如果截取后最后一个字符是半个字符{@code true}表示保留则长度是11否则长度9
*
* @param str 字符串
* @param len bytes切割到的位置包含
* @param halfUp 遇到截取一半的GBK字符是否保留。
* @return 切割后的字符串
* @since 5.7.17
*/
public static String subPreGbk(CharSequence str, int len, boolean halfUp) {
if (isEmpty(str)) {
return str(str);
}
byte[] b;
int counterOfDoubleByte = 0;
b = str.toString().getBytes(CharsetUtil.CHARSET_GBK);
final byte[] b = bytes(str, CharsetUtil.CHARSET_GBK);
if (b.length <= len) {
return str.toString();
}
@@ -2002,9 +2015,13 @@ public class CharSequenceUtil {
}
if (counterOfDoubleByte % 2 != 0) {
len += 1;
if(halfUp){
len += 1;
}else{
len -= 1;
}
}
return new String(b, 0, len, CharsetUtil.CHARSET_GBK) + suffix;
return new String(b, 0, len, CharsetUtil.CHARSET_GBK);
}
/**

View File

@@ -1,5 +1,6 @@
package cn.hutool.core.text;
import cn.hutool.core.util.CharsetUtil;
import org.junit.Assert;
import org.junit.Test;
@@ -65,4 +66,16 @@ public class CharSequenceUtilTest {
index = CharSequenceUtil.indexOf("abc123", 'b', 0, 3);
Assert.assertEquals(1, index);
}
@Test
public void subPreGbkTest(){
// https://gitee.com/dromara/hutool/issues/I4JO2E
String s = "华硕K42Intel酷睿i31代2G以下独立显卡不含机械硬盘固态硬盘120GB-192GB4GB-6GB";
String v = CharSequenceUtil.subPreGbk(s, 40, false);
Assert.assertEquals(39, v.getBytes(CharsetUtil.CHARSET_GBK).length);
v = CharSequenceUtil.subPreGbk(s, 40, true);
Assert.assertEquals(41, v.getBytes(CharsetUtil.CHARSET_GBK).length);
}
}