Skip to content

Commit

Permalink
Add StringUtils.endsWith and StringUtils.removeSuffix
Browse files Browse the repository at this point in the history
  • Loading branch information
Glavo committed Sep 10, 2024
1 parent 44ebfc2 commit 2432ffd
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions kala-base/src/main/java/kala/text/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
}

0 comments on commit 2432ffd

Please sign in to comment.