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

TakeFrom

Returns the remaining characters in a target string, starting from a search string. If the search string is not found in the target, it returns the full target string.

Source

/// <summary>
/// Returns the contents of a string starting with the location of the searchFor
/// </summary>
/// <param name="s">The string to search.</param>
/// <param name="searchFor">The string to search for.</param>
/// <returns></returns>
public static string TakeFrom(this string s, string searchFor)
{
	if (s.Contains(searchFor))
	{
		int length = Math.Max(s.Length, 0);

		int index = s.IndexOf(searchFor);

		return s.Substring(index, length - index);
	}

	return s;
}

Example

string s = "abcde";

Console.WriteLine (s.TakeFrom("d"));   // "de"

Author: Brett Veenstra

Submitted on: 18 sep 2009

Language: C#

Type: System.String

Views: 2508