c# - Split string so that all + datain to one list and - data in to another -
i having string follows separate in such way should give me +
data in 1 list , -
in other list.
string inputstr = "dim h + qut of pip dim - dim l + xyz - abc";
my expected follows
string[] positives = "dim h qut of pip dim xyz" string[] negatives = "dim l abc"
and when match original string know whether +
or -
when match found.
public bool findmatchesinstringarray(string markup, string[] strarray, out string matchstring) { bool flag = false; matchstring = string.empty; (int = 0; < strarray.length; i++) { match match = regex.match(markup, strarray[i].trim(), regexoptions.ignorecase); if (match.success) { flag = true; matchstring = match.value; } } return flag; }
here in markup
send dim h
or other when match found should give me + dim h
or -abc
.
here non regex solution:
static list<string> listofpositive = new list<string>(); static list<string> listofnegative = new list<string>(); static bool initialpositivecheck = false; static void main(string[] args) { string inputstr = "dim h + qut of pip dim - dim l + xyz - abc"; addtolist(inputstr); listofpositive.reverse(); listofnegative.reverse(); console.writeline(string.join("", listofpositive)); console.writeline(string.join("", listofnegative)); } static void addtolist(string newstring) { if (!(newstring.trim().startswith("+")) && !initialpositivecheck) { newstring = "+ " + newstring; initialpositivecheck = true; } int indexofplus = newstring.lastindexof("+"); int indexofminus = newstring.lastindexof("-"); string str = ""; if (indexofminus == -1 && indexofplus == -1) return; if (indexofplus < indexofminus) { str = newstring.substring(indexofminus + 1); listofnegative.add(str); str = newstring.remove(indexofminus); addtolist(str); } else { str = newstring.substring(indexofplus + 1); listofpositive.add(str); str = newstring.remove(indexofplus); addtolist(str); } }
link .net fiddle
output:
dim h qut of pip dim xyz dim l abc
Comments
Post a Comment