This commit is contained in:
Looly 2020-11-15 00:50:25 +08:00
parent 8855746b53
commit 4fa9794d06
3 changed files with 32 additions and 21 deletions

View File

@ -34,6 +34,7 @@
* 【http 】 修复Snowflake时间回拨导致ID重复的bugissue#1206@Github * 【http 】 修复Snowflake时间回拨导致ID重复的bugissue#1206@Github
* 【core 】 修复StrUtil.lastIndexOf查找位于首位的字符串找不到的bugissue#I24RSV@Gitee * 【core 】 修复StrUtil.lastIndexOf查找位于首位的字符串找不到的bugissue#I24RSV@Gitee
* 【poi 】 修复BigExcelWriter的autoSizeColumnAll问题pr#1221@Github * 【poi 】 修复BigExcelWriter的autoSizeColumnAll问题pr#1221@Github
* 【core 】 修复StrUtil.subBetweenAll不支持相同字符的问题pr#1217@Github
------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------

View File

@ -2405,10 +2405,19 @@ public class StrUtil {
} }
final List<String> result = new LinkedList<>(); final List<String> result = new LinkedList<>();
for (String fragment : split(str, prefix)) { final String[] split = split(str, prefix);
int suffixIndex = fragment.indexOf(suffix.toString()); if(prefix.equals(suffix)){
if (suffixIndex > 0) { // 前后缀字符相同单独处理
result.add(fragment.substring(0, suffixIndex)); 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> * </pre>
* *
* @param str 被切割的字符串 * @param str 被切割的字符串
* @param beforeAndAfter 截取开始和结束的字符串标识 * @param prefixAndSuffix 截取开始和结束的字符串标识
* @return 截取后的字符串 * @return 截取后的字符串
* @author gotanks * @author gotanks
* @since 5.4.7 * @since 5.5.0
*/ */
public static String[] subBetweenAll(CharSequence str, CharSequence beforeAndAfter) { public static String[] subBetweenAll(CharSequence str, CharSequence prefixAndSuffix) {
String[] resultArr = new String[0]; return subBetweenAll(str, prefixAndSuffix, prefixAndSuffix);
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);
} }
/** /**

View File

@ -1,6 +1,5 @@
package cn.hutool.core.util; package cn.hutool.core.util;
import cn.hutool.core.lang.Console;
import cn.hutool.core.lang.Dict; import cn.hutool.core.lang.Dict;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
@ -437,9 +436,21 @@ public class StrUtilTest {
@Test @Test
public void subBetweenAllTest3() { public void subBetweenAllTest3() {
String src1 = "'abc'and'123'"; 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, "'", "'"); String src2 = "'abc''123'";
Console.log(strings); 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 @Test