This commit is contained in:
Looly
2020-05-05 23:49:41 +08:00
parent 513fb1c861
commit bf65fa3c5f
5 changed files with 22 additions and 2 deletions

View File

@@ -1510,7 +1510,7 @@ public class StrUtil {
}
/**
* 切分字符串
* 切分字符串,如果分隔符不存在则返回原字符串
*
* @param str 被切分的字符串
* @param separator 分隔符
@@ -1976,11 +1976,14 @@ public class StrUtil {
* @since 5.2.5
*/
public static String[] subBetweenAll(CharSequence str, CharSequence prefix, CharSequence suffix) {
if (hasEmpty(str, prefix, suffix)) {
if (hasEmpty(str, prefix, suffix) ||
// 不包含起始字符串,则肯定没有子串
false == contains(str, prefix)) {
return new String[0];
}
final List<String> result = new LinkedList<>();
final String[] split = split(str, prefix);
for (String fragment : split(str, prefix)) {
int suffixIndex = fragment.indexOf(suffix.toString());
if (suffixIndex > 0) {

View File

@@ -432,5 +432,18 @@ public class StrUtilTest {
Assert.assertArrayEquals(new String[0], StrUtil.subBetweenAll("abc",null,"z"));
Assert.assertArrayEquals(new String[0], StrUtil.subBetweenAll("abc","y",null));
}
@Test
public void subBetweenAllTest2() {
//issue#861@Github起始不匹配的时候应该直接空
String src1 = "/* \n* hutool */ asdas /* \n* hutool */";
String src2 = "/ * hutool */ asdas / * hutool */";
String[] results1 = StrUtil.subBetweenAll(src1,"/**","*/");
Assert.assertEquals(0, results1.length);
String[] results2 = StrUtil.subBetweenAll(src2,"/*","*/");
Assert.assertEquals(0, results2.length);
}
}