c# - How can i parse a text between two strings? -


i have method:

private static string extractfromstring(string text, string startstring, string endstring)         {             string matched= "";             int index = 0;             int index1 = 0;              while (true)             {                 if (index == -1)                 {                     break;                 }                 index = text.indexof(startstring);                 index1 = text.indexof(endstring, index);                 matched = text.substring(index + startstring.length,                                          index1 - index - startstring.length);                 index = text.indexof(startstring, index1 + 1);             }              return matched;         } 

and using this:

result = extractfromstring(this.richtextbox1.text, textbox2.text, textbox3.text); 

result global string var.

the problem it's working fine first time when try again text it's getting stuck inside while loop nonstop , program freeze.

code (variant 1, substing between first occurence of startstring , last occurence of endstring):

private static string extractfromstring(string text, string startstring, string endstring) {     // argument control     if (text == null) throw new argumentnullexception("text");     if (string.isnullorempty(startstring)) throw new argumentnullexception("startstring");     if (string.isnullorempty(endstring)) throw new argumentnullexception("endstring");      // index of last occurence of startstring , last occurence of endstring     int indexstart = text.indexof(startstring);     int indexend = text.lastindexof(endstring);      // check indexes     if (indexstart < 0 || indexend < 0) return string.empty;     indexstart += startstring.length;     if (indexstart >= indexend) return string.empty;      // return result     return text.substring(indexstart, indexend - indexstart); } 

code (variant 1, substing between first occurence of startstring , succeeding occurence of endstring):

private static string extractfromstring(string text, string startstring, string endstring) {     // argument control     if (text == null) throw new argumentnullexception("text");     if (string.isnullorempty(startstring)) throw new argumentnullexception("startstring");     if (string.isnullorempty(endstring)) throw new argumentnullexception("endstring");      // index of first occurence of startstring     int indexstart = text.indexof(startstring);     if (indexstart < 0) return string.empty;      // index of first occurence of endstring after startstring     indexstart += startstring.length;     int indexend = text.indexof(endstring, indexstart);     if (indexend < 0) return string.empty;      // return result     return text.substring(indexstart, indexend - indexstart); } 

usage:

string result = extractfromstring("x   aaa  text  bbb x", "aaa", "bbb"); 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

spring cloud - How to configure SpringCloud Eureka instance to point to https on non standard port -

javascript - Bootstrap Popover: iOS Safari strange behaviour -