mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
fix bug
This commit is contained in:
parent
8855746b53
commit
4fa9794d06
@ -34,6 +34,7 @@
|
||||
* 【http 】 修复Snowflake时间回拨导致ID重复的bug(issue#1206@Github)
|
||||
* 【core 】 修复StrUtil.lastIndexOf查找位于首位的字符串找不到的bug(issue#I24RSV@Gitee)
|
||||
* 【poi 】 修复BigExcelWriter的autoSizeColumnAll问题(pr#1221@Github)
|
||||
* 【core 】 修复StrUtil.subBetweenAll不支持相同字符的问题(pr#1217@Github)
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
@ -2405,10 +2405,19 @@ public class StrUtil {
|
||||
}
|
||||
|
||||
final List<String> result = new LinkedList<>();
|
||||
for (String fragment : split(str, prefix)) {
|
||||
int suffixIndex = fragment.indexOf(suffix.toString());
|
||||
if (suffixIndex > 0) {
|
||||
result.add(fragment.substring(0, suffixIndex));
|
||||
final String[] split = split(str, prefix);
|
||||
if(prefix.equals(suffix)){
|
||||
// 前后缀字符相同,单独处理
|
||||
for (int i = 1, length = split.length - 1; i < length; i += 2) {
|
||||
result.add(split[i]);
|
||||
}
|
||||
} else{
|
||||
int suffixIndex;
|
||||
for (String fragment : split) {
|
||||
suffixIndex = fragment.indexOf(suffix.toString());
|
||||
if (suffixIndex > 0) {
|
||||
result.add(fragment.substring(0, suffixIndex));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -2433,23 +2442,13 @@ public class StrUtil {
|
||||
* </pre>
|
||||
*
|
||||
* @param str 被切割的字符串
|
||||
* @param beforeAndAfter 截取开始和结束的字符串标识
|
||||
* @param prefixAndSuffix 截取开始和结束的字符串标识
|
||||
* @return 截取后的字符串
|
||||
* @author gotanks
|
||||
* @since 5.4.7
|
||||
* @since 5.5.0
|
||||
*/
|
||||
public static String[] subBetweenAll(CharSequence str, CharSequence beforeAndAfter) {
|
||||
String[] resultArr = new String[0];
|
||||
if (hasEmpty(str, beforeAndAfter) || !contains(str, beforeAndAfter)) {
|
||||
return resultArr;
|
||||
}
|
||||
|
||||
final List<String> result = new LinkedList<>();
|
||||
String[] split = split(str, beforeAndAfter);
|
||||
for (int i = 1, length = split.length - 1; i < length; i = i + 2) {
|
||||
result.add(split[i]);
|
||||
}
|
||||
return result.toArray(resultArr);
|
||||
public static String[] subBetweenAll(CharSequence str, CharSequence prefixAndSuffix) {
|
||||
return subBetweenAll(str, prefixAndSuffix, prefixAndSuffix);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1,6 +1,5 @@
|
||||
package cn.hutool.core.util;
|
||||
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.core.lang.Dict;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
@ -437,9 +436,21 @@ public class StrUtilTest {
|
||||
@Test
|
||||
public void subBetweenAllTest3() {
|
||||
String src1 = "'abc'and'123'";
|
||||
String[] strings = StrUtil.subBetweenAll(src1, "'", "'");
|
||||
Assert.assertEquals(2, strings.length);
|
||||
Assert.assertEquals("abc", strings[0]);
|
||||
Assert.assertEquals("123", strings[1]);
|
||||
|
||||
final String[] strings = StrUtil.subBetweenAll(src1, "'", "'");
|
||||
Console.log(strings);
|
||||
String src2 = "'abc''123'";
|
||||
strings = StrUtil.subBetweenAll(src2, "'", "'");
|
||||
Assert.assertEquals(2, strings.length);
|
||||
Assert.assertEquals("abc", strings[0]);
|
||||
Assert.assertEquals("123", strings[1]);
|
||||
|
||||
String src3 = "'abc'123'";
|
||||
strings = StrUtil.subBetweenAll(src3, "'", "'");
|
||||
Assert.assertEquals(1, strings.length);
|
||||
Assert.assertEquals("abc", strings[0]);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
Loading…
x
Reference in New Issue
Block a user