Usually, we can easily get a String value between two characters. My question is, how do I get the value between two same characters.
For example:
String full_value = "http://stackoverflow.com/questions/9367119/title-goes-here";
In this example, how can I extract the value 9367119
from the entire string?
The solution that I use doesn't work since 9367119
has the same /
characters to the right and left of it.
Here's what I have so far:
This works for values that doesn't have two same characters to the left and right. Such as: /dog\ I can easily replace /
and \
with my solution
public static string Between(string full_value, string a, string b)
{
int posA = full_value.IndexOf(a);
int posB = full_value.LastIndexOf(b);
if (posA == -1)
{
return "";
}
if (posB == -1)
{
return "";
}
int adjustedPosA = posA + a.Length;
if (adjustedPosA >= posB)
{
return "";
}
return full_value.Substring(adjustedPosA, posB - adjustedPosA);
}