using System;using System.Diagnostics;
namespace GetBetweenStrings_Blog
public static class Extensions
public static string GetFirstStringBetweenStringsCleanup(this string s, string from, string to)
if (string.IsNullOrEmpty(s) || string.IsNullOrEmpty(from) || string.IsNullOrEmpty(to)) return string.Empty;
int idxFrom = s.IndexOf(from);
int idxStart = idxFrom + from.Length;
if (idxFrom == -1 || idxStart >= s.Length - 1)
int idxEnd = s.LastIndexOf(to);
if (idxEnd == -1 || idxEnd <= idxStart)
return s.Substring(idxStart, idxEnd - idxStart);
public static string GetFirstStringBetweenStrings(this string s, string from, string to)
Console.Write("args from {0} to {1} = ", from, to);
if (string.IsNullOrEmpty(s) || string.IsNullOrEmpty(from) || string.IsNullOrEmpty(to)) return string.Empty;
int idxFrom = s.IndexOf(from);
int idxStart = idxFrom + from.Length;
else if (idxStart >= s.Length - 1)
Console.WriteLine("r1. idxStart={0} >= (s.Length - 1)={1}", idxStart, s.Length - 1);
int idxEnd = s.LastIndexOf(to);
else if (idxEnd <= idxStart)
Console.WriteLine("r3. idxEnd={0} <= idxStart={1}", idxEnd, idxStart);
return s.Substring(idxStart, idxEnd - idxStart);
public static void Main(string[] args)
Console.WriteLine("=================");
Console.WriteLine(test.GetFirstStringBetweenStrings(null, null));
Console.WriteLine(test.GetFirstStringBetweenStrings("", ""));
Console.WriteLine(test.GetFirstStringBetweenStrings(test, test));
Console.WriteLine(test.GetFirstStringBetweenStrings("", test));
Console.WriteLine(test.GetFirstStringBetweenStrings(test,""));
Console.WriteLine(test.GetFirstStringBetweenStrings("a", "a"));
Console.WriteLine(test.GetFirstStringBetweenStrings("a", "b"));
Console.WriteLine(test.GetFirstStringBetweenStrings("a", "c"));
Console.WriteLine(test.GetFirstStringBetweenStrings("a", "d"));
Console.WriteLine(test.GetFirstStringBetweenStrings("a", "e"));
Console.WriteLine(test.GetFirstStringBetweenStrings("e", "e"));
Console.WriteLine(test.GetFirstStringBetweenStrings("ab", "a"));
Console.WriteLine(test.GetFirstStringBetweenStrings("ab", "b"));
Console.WriteLine(test.GetFirstStringBetweenStrings("ab", "c"));
Console.WriteLine(test.GetFirstStringBetweenStrings("ab", "d"));
Console.WriteLine(test.GetFirstStringBetweenStrings("ab", "a"));
Console.WriteLine(test.GetFirstStringBetweenStrings("ab", "b"));
Console.WriteLine(test.GetFirstStringBetweenStrings("ab", "c"));
Console.WriteLine(test.GetFirstStringBetweenStrings("ab", "d"));
Console.WriteLine(test.GetFirstStringBetweenStrings("abc", "a"));
Console.WriteLine(test.GetFirstStringBetweenStrings("abc", "b"));
Console.WriteLine(test.GetFirstStringBetweenStrings("abc", "c"));
Console.WriteLine(test.GetFirstStringBetweenStrings("abc", "d"));
Console.WriteLine(test.GetFirstStringBetweenStrings(test, "a"));
Console.WriteLine(test.GetFirstStringBetweenStrings(test, "b"));
Console.WriteLine(test.GetFirstStringBetweenStrings(test, "c"));
Console.WriteLine(test.GetFirstStringBetweenStrings(test, "d"));
Console.WriteLine(test.GetFirstStringBetweenStrings("a","abc"));
Console.WriteLine(test.GetFirstStringBetweenStrings("b", "abc"));
Console.WriteLine(test.GetFirstStringBetweenStrings("c", "abc"));
Console.WriteLine(test.GetFirstStringBetweenStrings("d", "abc"));
Console.WriteLine("=================");
Console.WriteLine(test.GetFirstStringBetweenStrings(null, null));
Console.WriteLine(test.GetFirstStringBetweenStrings("", ""));
Console.WriteLine(test.GetFirstStringBetweenStrings(test, test));
Console.WriteLine(test.GetFirstStringBetweenStrings("", test));
Console.WriteLine(test.GetFirstStringBetweenStrings(test, ""));
Console.WriteLine(test.GetFirstStringBetweenStrings("a", "a"));
Console.WriteLine(test.GetFirstStringBetweenStrings("a", "b"));
Console.WriteLine(test.GetFirstStringBetweenStrings("a", "c"));
Console.WriteLine(test.GetFirstStringBetweenStrings("a", "d"));
Console.WriteLine(test.GetFirstStringBetweenStrings("a", "e"));
Console.WriteLine(test.GetFirstStringBetweenStrings("e", "e"));
Console.WriteLine(test.GetFirstStringBetweenStrings("ab", "a"));
Console.WriteLine(test.GetFirstStringBetweenStrings("ab", "b"));
Console.WriteLine(test.GetFirstStringBetweenStrings("ab", "c"));
Console.WriteLine(test.GetFirstStringBetweenStrings("ab", "d"));
Console.WriteLine(test.GetFirstStringBetweenStrings("ab", "a"));
Console.WriteLine(test.GetFirstStringBetweenStrings("ab", "b"));
Console.WriteLine(test.GetFirstStringBetweenStrings("ab", "c"));
Console.WriteLine(test.GetFirstStringBetweenStrings("ab", "d"));
Console.WriteLine(test.GetFirstStringBetweenStrings("abc", "a"));
Console.WriteLine(test.GetFirstStringBetweenStrings("abc", "b"));
Console.WriteLine(test.GetFirstStringBetweenStrings("abc", "c"));
Console.WriteLine(test.GetFirstStringBetweenStrings("abc", "d"));
Console.WriteLine(test.GetFirstStringBetweenStrings(test, "a"));
Console.WriteLine(test.GetFirstStringBetweenStrings(test, "b"));
Console.WriteLine(test.GetFirstStringBetweenStrings(test, "c"));
Console.WriteLine(test.GetFirstStringBetweenStrings(test, "d"));
Console.WriteLine(test.GetFirstStringBetweenStrings("a", "abc"));
Console.WriteLine(test.GetFirstStringBetweenStrings("b", "abc"));
Console.WriteLine(test.GetFirstStringBetweenStrings("c", "abc"));
Console.WriteLine(test.GetFirstStringBetweenStrings("d", "abc"));
Console.WriteLine(test.GetFirstStringBetweenStrings("abc", "d"));
Console.WriteLine("=================");
Console.WriteLine(test.GetFirstStringBetweenStrings("a", "a"));
Console.WriteLine(test.GetFirstStringBetweenStrings("aa", "a"));
Console.WriteLine(test.GetFirstStringBetweenStrings("aaa", "a"));
Console.WriteLine(test.GetFirstStringBetweenStrings("aaaa", "a"));
Console.WriteLine(test.GetFirstStringBetweenStrings("a", "a"));
Console.WriteLine(test.GetFirstStringBetweenStrings("a", "aa"));
Console.WriteLine(test.GetFirstStringBetweenStrings("a", "aaa"));
Console.WriteLine(test.GetFirstStringBetweenStrings("a", "aaaa"));
test = "aaaaaaaaaaaaaaaaaaaaaaaaaaaabbbb";
Console.WriteLine("=================");
Console.WriteLine(test.GetFirstStringBetweenStrings("bbbb", "aaaa"));
string result = string.Empty;
Stopwatch sw = new Stopwatch();
result = test.GetFirstStringBetweenStringsCleanup("aaaaaaa", "bb");
Console.WriteLine(result);
Console.WriteLine(" in " + sw.ElapsedTicks + " ticks.");