c# - getting string and numbers -
i got string
string newstring = "[17, appliance]";
how can put 17
, appliance
in 2 separate variables while ignoring ,
, [
, ]
?
i tried looping though loop doesn't stop when reaches ,
, not mention separated 1 & 7 instead of reading 17.
this may not performant method, i'd go ease of understanding.
string newstring = "[17, appliance]"; newstring = newstring.replace("[", "").replace("]",""); // remove square brackets string[] results = newstring.split(new string[] { ", " }, stringsplitoptions.removeemptyentries); // split string // if string going contain 1 number , 1 string: int num1 = int.parse(results[0]); string string1 = results[1];
you'd want include validation ensure first element indeed number (use int.tryparse
), , there indeed 2 elements returned after split string.
Comments
Post a Comment