How to replace empty position with a 0 value when converting string to array in C#? -


update: july 26, 2017

i have string inside values comma separated. cases coming double comma ,, @ in consecutive way. when using using string.split(',') it's returning me array doesn't have value on index. example

string str = "12,3,5,,6,54,127,8,,0,98,"  

it's breaking down the array way

str2[0] = 12 str2[1] = 3 str2[2] = 5 str2[3] = "" str2[4] = 6 str2[5] = 54 str2[6] = 127 str2[7] = 8 str2[8] = "" str2[9] = 0 str2[10] = 98 str2[11] = "" 

look here getting array 1 or more empty value. want put 0 in each empty position when splitting string. here have found skip empty values

str .split(',', stringsplitoptions.removeemptyentries)  

however did not found such solution put default value @ empty index. have gone through these previous questions q1, q2, these not effective mine. using c# web application in .net framework

while suggested solutions work perfectly, iterating twice input (once string split, once string replace or regex, once array replace).

here solution iterating once input:

var input = "12,3,5,,6,54,127,8,,0,98";  var result = new list<int>(); var currentnumeric = string.empty; foreach(char c in input) {     if(c == ',' && string.isnullorwhitespace(currentnumeric))     {         result.add(0);     }     else if(c == ',')     {         result.add(int.parse(currentnumeric));         currentnumeric = string.empty;     }     else     {         currentnumeric += c;     } }  if(!string.isnullorwhitespace(currentnumeric)) {     result.add(int.parse(currentnumeric)); } else if(input.endswith(",")) {     result.add(0); } 

Comments

Popular posts from this blog

python - Selenium remoteWebDriver (& SauceLabs) Firefox moseMoveTo action exception -

html - How to custom Bootstrap grid height? -

transpose - Maple isnt executing function but prints function term -