ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

RemoveRightIfPresent

Removes end of string if it equals to parameter, otherwise returns origin string

Source

public static string RemoveRightIfPresent(this string s, string remove, bool ignoreCase = false)
{
    int rlen = remove.Length;
    if (s.EndsWith(remove, ignoreCase, System.Globalization.CultureInfo.CurrentCulture))
    {
        return s.Substring(0, s.Length - rlen);
    }
    else
    {
        return s;
    }
}

Example

string stoTest = "abc@mail.ru";
string res = "abc";
Assert.AreEqual(res,stoTest.RemoveRightIfPresent("@mail.ru"));

Author: Dmitry

Submitted on: 17 mei 2013

Language: C#

Type: string

Views: 4353