I need this all the time and am constantly frustrated that the Trim(), TrimStart() and TrimEnd() functions don\'t take strings as inputs. You call EndsWith() on a string, an
Trim(), TrimStart() and TrimEnd() are methods which replace all occurrences of the same character. That means you can only remove a series of blanks or a series of dots for example.
You could use a regular expression replace in order to accomplish this:
string s1 = "This is a sentence.TRIMTHIS";
string s2 = System.Text.RegularExpressions.Regex.Replace(s1, @"TRIMTHIS$", "");
You could wrap it in an extension method for convenience:
public static string TrimStringEnd(this string text, string removeThis)
{
return System.Text.RegularExpressions.Regex.Replace(s1, removeThis, "");
}
And call it this way
string s2 = (@"This is a sentence.TRIMTHIS").TrimStringEnd(@"TRIMTHIS");