There is no built-in function in C #, but you can write your own extensions that behave exactly as you expect.
Note that with IndexOf / LastIndexOf you can choose whether it is case sensitive / culture or not.
I also used the "repeat cropping" function.
There is one TrimStr(..) function that works with both TrimStr(..) and three functions that implement .TrimStart(...) , .TrimEnd(...) and .Trim(..) for compatibility with .NET cropping :
Try it in DotNetFiddle
public static class Extension { public static string TrimStr(this string str, string trimStr, bool trimEnd = true, bool repeatTrim = true, StringComparison comparisonType = StringComparison.OrdinalIgnoreCase) { int strLen; do { if (!(str ?? "").EndsWith(trimStr)) return str; strLen = str.Length; { if (trimEnd) { var pos = str.LastIndexOf(trimStr, comparisonType); if ((!(pos >= 0)) || (!(str.Length - trimStr.Length == pos))) break; str = str.Substring(0, pos); } else { var pos = str.IndexOf(trimStr, comparisonType); if (!(pos == 0)) break; str = str.Substring(trimStr.Length, str.Length - trimStr.Length); } } } while (repeatTrim && strLen > str.Length); return str; } // the following is C#6 syntax, if you're not using C#6 yet // replace "=> ..." by { return ... } public static string TrimEnd(this string str, string trimStr, bool repeatTrim = true, StringComparison comparisonType = StringComparison.OrdinalIgnoreCase) => TrimStr(str, trimStr, true, repeatTrim, comparisonType); public static string TrimStart(this string str, string trimStr, bool repeatTrim = true, StringComparison comparisonType = StringComparison.OrdinalIgnoreCase) => TrimStr(str, trimStr, false, repeatTrim, comparisonType); public static string Trim(this string str, string trimStr, bool repeatTrim = true, StringComparison comparisonType = StringComparison.OrdinalIgnoreCase) => str.TrimStart(trimStr, repeatTrim, comparisonType) .TrimEnd(trimStr, repeatTrim, comparisonType); }
Now you can just use it as
Console.WriteLine("Sammy".TrimEnd("my")); Console.WriteLine("moinmoin gibts gips? gips gibts moin".TrimStart("moin", false)); Console.WriteLine("moinmoin gibts gips? gips gibts moin".Trim("moin").Trim());
which creates the output
Sam
moin gibts gips? gips gibts moin
gibts gips? gips gibts