You can use IndexOfto do the same without regular expressions.
This will return the first occurrence of the line between the two "**" with truncated spaces. It also has non-existence checks for a string that matches this condition.
public string FindTextBetween(string text, string left, string right)
{
int beginIndex = text.IndexOf(left);
if (beginIndex == -1)
return string.Empty;
beginIndex += left.Length;
int endIndex = text.IndexOf(right, beginIndex);
if (endIndex == -1)
return string.Empty;
return text.Substring(beginIndex, endIndex - beginIndex).Trim();
}
string str = "The texts starts here ** Get This String ** Some other text ongoing here.....";
string result = FindTextBetween(str, "**", "**");
I usually prefer not to use regex when possible.
source
share