From 7d72d8c5e7e307fe233a10edc5484bc6af0be6a5 Mon Sep 17 00:00:00 2001 From: lilanlong <506706088@qq.com> Date: Tue, 16 Jul 2024 10:58:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=A7=BB=E9=99=A4=E6=89=80?= =?UTF-8?q?=E6=9C=89=E5=89=8D=E7=BC=80=E6=96=B9=E6=B3=95=EF=BC=9AremoveAll?= =?UTF-8?q?Prefix=20=E5=A2=9E=E5=8A=A0=E7=A7=BB=E9=99=A4=E6=89=80=E6=9C=89?= =?UTF-8?q?=E5=90=8E=E7=BC=80=E6=96=B9=E6=B3=95=EF=BC=9AremoveAllSuffix?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/hutool/core/text/CharSequenceUtil.java | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/hutool-core/src/main/java/cn/hutool/core/text/CharSequenceUtil.java b/hutool-core/src/main/java/cn/hutool/core/text/CharSequenceUtil.java index af17ce0ed..42fff4a12 100755 --- a/hutool-core/src/main/java/cn/hutool/core/text/CharSequenceUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/text/CharSequenceUtil.java @@ -1464,6 +1464,25 @@ public class CharSequenceUtil { } return str2; } + + /** + * 去掉指定所有前缀 + * + * @param str 字符串 + * @param prefix 前缀 + * @return 切掉所有前缀的字符串,若前缀不是 preffix, 返回原字符串 + */ + public static String removeAllPrefix(CharSequence str, CharSequence prefix) { + if (isEmpty(str) || isEmpty(prefix)) { + return str(str); + } + + String str2 = str.toString(); + while (str2.startsWith(prefix.toString())) { + str2 = removePrefix(str2, prefix); + } + return str2; + } /** * 忽略大小写去掉指定前缀 @@ -1502,6 +1521,26 @@ public class CharSequenceUtil { } return str2; } + + /** + * 去掉指定所有后缀 + * + * @param str 字符串 + * @param suffix 后缀 + * @return 切掉所有后缀的字符串,若后缀不是 suffix, 返回原字符串 + */ + public static String removeAllSuffix(CharSequence str, CharSequence suffix) { + if (isEmpty(str) || isEmpty(suffix)) { + return str(str); + } + + String str2 = str.toString(); + while (str2.endsWith(suffix.toString())) { + str2 = removeSuffix(str2, suffix); + } + return str2; + } + /** * 去掉指定后缀,并小写首字母