拆分逗号分隔的字符串 5

Split comma separated strings 5

我在 datagridview 列中有 5 个带逗号的值 abc,xyz,asdf,qwer,mni

如何拆分成字符串并显示在文本框中

abc
xyz
asdf
qwer
mni

这里不需要拆分,直接替换字符串中的逗号即可,string.Replace

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

编辑

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];

先把逗号替换成space:

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

然后将其添加回文本框:

textbox.Text = str;

OP 说,他有 5 个文本框来显示单词。所以你可以使用 String.Split();

示例:

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];

您可以使用数组:

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