From 03fd34b039f547d82cab7638a2ce4d3f520d37a8 Mon Sep 17 00:00:00 2001 From: gotanks Date: Sat, 14 Nov 2020 09:48:40 +0800 Subject: [PATCH] =?UTF-8?q?StrUtil=E5=B7=A5=E5=85=B7=E7=B1=BB=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0subBetweenAl(str,=20beforeAndAfter)=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cn/hutool/core/util/StrUtil.java | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/hutool-core/src/main/java/cn/hutool/core/util/StrUtil.java b/hutool-core/src/main/java/cn/hutool/core/util/StrUtil.java index 6ddb37c70..2b61b7a5e 100644 --- a/hutool-core/src/main/java/cn/hutool/core/util/StrUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/util/StrUtil.java @@ -2415,6 +2415,43 @@ public class StrUtil { return result.toArray(new String[0]); } + /** + * 截取指定字符串多段中间部分,不包括标识字符串
+ *

+ * 栗子: + * + *

+	 * StrUtil.subBetweenAll(null, *)          			= []
+	 * StrUtil.subBetweenAll(*, null)          			= []
+	 * StrUtil.subBetweenAll(*, *)          			= []
+	 * StrUtil.subBetweenAll("", "")          			= []
+	 * StrUtil.subBetweenAll("", "#")         			= []
+	 * StrUtil.subBetweenAll("gotanks", "")     		= []
+	 * StrUtil.subBetweenAll("#gotanks#", "#")   		= ["gotanks"]
+	 * StrUtil.subBetweenAll("#hello# #world#!", "#")   = ["hello", "world"]
+	 * StrUtil.subBetweenAll("#hello# world#!", "#");   = ["hello"]
+	 * 
+ * + * @param str 被切割的字符串 + * @param beforeAndAfter 截取开始和结束的字符串标识 + * @return 截取后的字符串 + * @author gotanks + * @since 5.4.7 + */ + public static String[] subBetweenAll(CharSequence str, CharSequence beforeAndAfter) { + String[] resultArr = new String[0]; + if (hasEmpty(str, beforeAndAfter) || !contains(str, beforeAndAfter)) { + return resultArr; + } + + final List 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); + } + /** * 给定字符串是否被字符包围 *