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

RightOf

Return the remainder of a string s after a separator c.

Source

/// <summary>
/// Return the remainder of a string s after a separator c.
/// </summary>
/// <param name="s">String to search in.</param>
/// <param name="c">Separator</param>
/// <returns>The right part of the string after the character c, or the string itself when c isn't found.</returns>
public static string RightOf(this string s, char c)
{
    int ndx = s.IndexOf(c);
    if (ndx == -1)
        return s;
    return s.Substring(ndx + 1);
}

Example

/// <summary>
///A test for RightOf
///</summary>
[TestMethod()]
public void RightOfTest()
{
    string s = "XYZ,1234";
    char c = ',';
    string expected = "1234";
    string actual;
    actual = StringExtensions.RightOf(s, c);
    Assert.AreEqual(expected, actual);

    s = "XYZ,1234,ABC";
    c = ',';
    expected = "1234,ABC";
    actual = StringExtensions.RightOf(s, c);
    Assert.AreEqual(expected, actual);

    s = "XYZ";
    c = ',';
    expected = "XYZ";
    actual = StringExtensions.RightOf(s, c);
    Assert.AreEqual(expected, actual);

}

Author: Gaston Verelst

Submitted on: 21 jun. 2010

Language: C#

Type: System.String

Views: 5616