From d7852e45c9045f1ffb2843750cb7274c1eab73d2 Mon Sep 17 00:00:00 2001 From: ig Date: Mon, 20 Mar 2023 12:53:03 +0100 Subject: [PATCH] add missing EnsureStartsWith --- csharp/Lib/Utils/StringUtils.cs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/csharp/Lib/Utils/StringUtils.cs b/csharp/Lib/Utils/StringUtils.cs index e0ee21746..46372e381 100644 --- a/csharp/Lib/Utils/StringUtils.cs +++ b/csharp/Lib/Utils/StringUtils.cs @@ -352,6 +352,34 @@ public static class StringUtils } + public static String TrimStart(this String target, String trimString) + { + if (String.IsNullOrEmpty(trimString)) return target; + + var result = target; + while (result.StartsWith(trimString)) + result = result[trimString.Length..]; + + return result; + } + + + public static String TrimEnd(this String target, String trimString) + { + if (String.IsNullOrEmpty(trimString)) + return target; + + var result = target; + while (result.EndsWith(trimString)) + result = result[..^trimString.Length]; + + return result; + } + + public static String EnsureStartsWith(this String target, String prefix) + { + return $"{prefix}{target.TrimStart(prefix)}"; + } } \ No newline at end of file