CSharp examples for System:String SubString
String extension method which returns the substring between two given strings.
using System.Text.RegularExpressions; using System.Text; using System.Security.Cryptography; using System.Linq; using System.Globalization; using System.Collections.Generic; using System;//ww w.j a v a2 s. co m public class Main{ /// <summary> /// String extension method which returns the substring between two given strings. /// </summary> /// <param name="input">The string on which the method is invoked.</param> /// <param name="startString">Start of the substring.</param> /// <param name="endString">End of the substring.</param> /// <param name="startFrom">Index to start search from (optional).</param> /// <returns>Returns the substring between startString and endString. If the substrings are not cointained in the input string - returns an empty string.</returns> public static string GetStringBetween(this string input, string startString, string endString, int startFrom = 0) { input = input.Substring(startFrom); startFrom = 0; if (!input.Contains(startString) || !input.Contains(endString)) { return string.Empty; } var startPosition = input.IndexOf(startString, startFrom, StringComparison.Ordinal) + startString.Length; if (startPosition == -1) { return string.Empty; } var endPosition = input.IndexOf(endString, startPosition, StringComparison.Ordinal); if (endPosition == -1) { return string.Empty; } return input.Substring(startPosition, endPosition - startPosition); } }