Split comma separated strings 5

968 views Asked by At

I have 5 values in datagridview column with comma like abc,xyz,asdf,qwer,mni

how to split into string and display in textbox

abc
xyz
asdf
qwer
mni
3

There are 3 answers

4
Adil On BEST ANSWER

You do not need Split here, Just replace the comma in the string, string.Replace

str = str.Replace(",", " ");

Edit

string []arr = str.Split('c');
txt1.Text = arr[0];
txt2.Text = arr[1];
txt3.Text = arr[2];
txt4.Text = arr[3];
txt5.Text = arr[4];
0
Jon Koivula On

First replace comma with space:

str = str.Replace(',', '');

Then add it back to textbox:

textbox.Text = str;
0
Raging Bull On

OP Says, he has 5 textboxes to display the words. So you can use String.Split();

Example:

string str="abc,xyz,asdf,qwer,mni";

textbox1.Text = str.Split(',')[0];
textbox2.Text = str.Split(',')[1];
textbox3.Text = str.Split(',')[2];
textbox4.Text = str.Split(',')[3];
textbox5.Text = str.Split(',')[4];

OR

You can use an array:

string[] strarray = str.Split(',');
textbox1.Text = strarray[0];
textbox2.Text = strarray[1];
textbox3.Text = strarray[2];
textbox4.Text = strarray[3];
textbox5.Text = strarray[4];