From 2432ffdd8b5aa5aeb743f14ae663508318de7868 Mon Sep 17 00:00:00 2001 From: Glavo Date: Tue, 10 Sep 2024 22:50:41 +0800 Subject: [PATCH] Add StringUtils.endsWith and StringUtils.removeSuffix --- .../src/main/java/kala/text/StringUtils.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/kala-base/src/main/java/kala/text/StringUtils.java b/kala-base/src/main/java/kala/text/StringUtils.java index 356c5a9c..23398362 100644 --- a/kala-base/src/main/java/kala/text/StringUtils.java +++ b/kala-base/src/main/java/kala/text/StringUtils.java @@ -50,6 +50,22 @@ public static boolean startsWith(@NotNull String str, @NotNull CharSequence pref return prefix instanceof StringSlice ? startsWith(str, (StringSlice) prefix, toIndex) : startsWith(str, prefix.toString(), toIndex); } + public static boolean endsWith(@NotNull String str, @NotNull String prefix) { + return str.endsWith(prefix); + } + + public static boolean endsWith(@NotNull String str, @NotNull StringSlice prefix) { + if (str.length() < prefix.length()) { + return false; + } + + return str.regionMatches(str.length() - prefix.length(), prefix.source(), prefix.sourceOffset(), prefix.length()); + } + + public static boolean endsWith(@NotNull String str, @NotNull CharSequence prefix) { + return prefix instanceof StringSlice ? endsWith(str, (StringSlice) prefix) : endsWith(str, prefix.toString()); + } + public static String removePrefix(@NotNull String str, @NotNull String prefix) { return str.startsWith(prefix) ? str.substring(prefix.length()) : str; } @@ -62,4 +78,15 @@ public static String removePrefix(@NotNull String str, @NotNull CharSequence pre return startsWith(str, prefix) ? str.substring(prefix.length()) : str; } + public static String removeSuffix(@NotNull String str, @NotNull String prefix) { + return str.endsWith(prefix) ? str.substring(0, str.length() - prefix.length()) : str; + } + + public static String removeSuffix(@NotNull String str, @NotNull StringSlice prefix) { + return endsWith(str, prefix) ? str.substring(0, str.length() - prefix.length()) : str; + } + + public static String removeSuffix(@NotNull String str, @NotNull CharSequence prefix) { + return endsWith(str, prefix) ? str.substring(0, str.length() - prefix.length()) : str; + } }