c# - Extract all strings between two strings -


i'm trying develop method match strings between 2 strings:

i've tried returns first match:

string extractstring(string s, string start,string end)         {             // should check errors in real-world code, omitted brevity              int startindex = s.indexof(start) + start.length;             int endindex = s.indexof(end, startindex);             return s.substring(startindex, endindex - startindex);         } 

let's suppose have string

string text = "a1firststringa2a1secondstringa2akslakhflkshdflhksdfa1thirdstringa2" 

i c# function doing following :

public list<string> extractfromstring(string text,string start, string end) {     list<string> matched = new list<string>();     .     .     .     return matched;  } // example of use   extractfromstring("a1firststringa2a1secondstringa2akslakhflkshdflhksdfa1thirdstringa2","a1","a2")      // return :     // firststring     // secondstring     // thirdstring 

thank !

private static list<string> extractfromstring(     string text, string startstring, string endstring) {                 list<string> matched = new list<string>();     int indexstart = 0, indexend=0;     bool exit = false;     while(!exit)     {         indexstart = text.indexof(startstring);         indexend = text.indexof(endstring);         if (indexstart != -1 && indexend != -1)         {             matched.add(text.substring(indexstart + startstring.length,                  indexend - indexstart - startstring.length));             text = text.substring(indexend + endstring.length);         }         else             exit = true;     }     return matched; } 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -