Skip to content

Commit b1a3dc1

Browse files
feat: add RemovePrefix methods with a char overload
1 parent f14ae5c commit b1a3dc1

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/WouterVanRanst.Utils/Extensions/StringExtensions.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,32 @@ public static string RemovePrefix(this string s, string prefix, StringComparison
3333
return s;
3434
}
3535

36+
/// <summary>
37+
/// Removes the specified character prefix from the beginning of the string, if it exists, using ordinal comparison
38+
/// </summary>
39+
public static string RemovePrefix(this string s, char prefix)
40+
{
41+
if (s.Length > 0 && s[0] == prefix)
42+
return s[1..];
43+
44+
return s;
45+
}
46+
47+
/// <summary>
48+
/// Removes the specified character prefix from the beginning of the string, if it exists, using case-insensitive comparison
49+
/// </summary>
50+
public static string RemovePrefix(this string s, char prefix, bool ignoreCase)
51+
{
52+
if (s.Length > 0)
53+
{
54+
var firstChar = s[0];
55+
if (ignoreCase ? char.ToUpperInvariant(firstChar) == char.ToUpperInvariant(prefix) : firstChar == prefix)
56+
return s[1..];
57+
}
58+
59+
return s;
60+
}
61+
3662
/// <summary>
3763
/// Trim the given value from the end of the string
3864
/// </summary>

0 commit comments

Comments
 (0)