204
1
using System;using System.Diagnostics;
2
3
public static class Extensions
4
{
5
/// <summary>
6
/// Get a substring between two anchor strings, minimal span
7
/// </summary>
8
/// <param name="s">source string</param>
9
/// <param name="from">search from end of this string</param>
10
/// <param name="to">to beginning of this string, searching backwards, from end to start of s</param>
11
/// <returns>a substring between from and to, maximal span</returns>
12
public static string GetFirstStringBetweenStringsMinSpanCleanup(this string s, string from, string to)
13
{
14
if (string.IsNullOrEmpty(s) || string.IsNullOrEmpty(from) || string.IsNullOrEmpty(to)) return string.Empty;
15
16
int idxFrom = s.IndexOf(from);
17
int idxStart = idxFrom + from.Length; //we filter "not found" -1, never race condtn
18
19
if (idxFrom == -1 || idxStart >= s.Length - 1)
20
return string.Empty;
21
22
int idxEnd = s.IndexOf(to, idxStart); //Exact definition, but intuitively next line meets likely expectations -> YOU CHOOSE
23
//int idxEnd = s.IndexOf(to, idxStart + 1); //Start next position after, leaving a space for 1 character to be returned
24
Cached Result