Extends the string class.PHP Code:
using System;
using System.Collections.Generic;
public static class StringExtensions
{
public static List<String> GetAllStringsBetween(this String Input, String Start, String End)
{
List<String> Return = new List<String>();
Int32 Result = 0;
Int32 LResult = 0;
while ((Result = Input.GetOffsetBetween(Start, End, Result)) != -1)
{
Return.Add(Input.GetStringBetween(Start, End, LResult));
LResult = Result;
}
return Return;
}
public static String GetStringBetween(this String Input, String Start, String End)
{
return Input.GetStringBetween(Start, End, 0);
}
public static String GetStringBetween(this String Input, String Start, String End, Int32 Offset)
{
if (Offset < Input.Length)
{
Int32 StartPos = (Input.IndexOf(Start, Offset) + Start.Length);
if (StartPos > -1)
{
Int32 EndPos = Input.IndexOf(End, StartPos);
if (EndPos > -1)
{
return Input.Substring(StartPos, (EndPos - StartPos));
}
}
}
return String.Empty;
}
private static Int32 GetOffsetBetween(this String Input, String Start, String End, Int32 Offset)
{
if (Offset < Input.Length)
{
Int32 StartPos = Input.IndexOf(Start, Offset);
if (StartPos > -1)
{
Int32 EndPos = Input.IndexOf(End, (StartPos + Start.Length));
if (EndPos > -1)
{
return EndPos;
}
}
}
return -1;
}
}
Example usage;
String test = "test";
String result = test.GetStringBetween("t", "t");
result == "es"
Results 1 to 1 of 1
- 24 Dec. 2010 08:28am #1
- Age
- 30
- Join Date
- Nov. 2009
- Location
- Anaheim, California
- Posts
- 1,065
- Reputation
- 99
- LCash
- 63.58
[Release - C#] Streamlined GetStringBetween and GetAllStringBetween