如何动态地分割字符串 [英] how to split string in dynamicaly
本文介绍了如何动态地分割字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hai Friends,
我想在文本框中显示数据,如
id:111
名称:venkat
颜色:绿色
因此,在此之前,我仅显示诸如
之类的数据
111
venkat
绿色
.................
现在我要这样显示
Hai Friends,
i want to dispaly data in textbox like
id:111
name:venkat
color:green
so befor i display only for data like
111
venkat
green
.................
now i want to display like this
id:111
name:venkat
color:green
这是我的代码
here is my code
string str2 = "";
string[] strListItems;
int intNumItems = comboBox1.Items.Count;
strListItems = new string[intNumItems];
for (int i = 0; i < comboBox1.Items.Count; i++)
{
strListItems[i] = ds.Tables[0].Rows[comboBox1.SelectedIndex][i].ToString();
str2 = str2 + "\r\n " +strListItems[i];
}
textBox1.Text = str2;
请给我任何想法.
谢谢
Venkat.S
Please give me any idea.
Thanks
Venkat.S
推荐答案
更改代码,如
Change your code like
string str2 = "";
string[] strListItems = { "id", "name", "color" };
for (int i = 0; i < comboBox1.Items.Count; i++)
{
str2 += strListItems[i] + ":" + ds.Tables[0].Rows[comboBox1.SelectedIndex][i].ToString() + "\r\n ";
}
textBox1.Text = str2;
那是什么问题?什么对您不起作用?在我看来,您需要添加代码以插入列名,这对我来说似乎是一项微不足道的任务.您可以将它们存储在数组中,以便也按索引读取它们,或者可以编写不使用循环的代码来对列名称进行硬编码.无论哪种方式,对我来说似乎都是一种差劲的方法,但这行得通.
So what is the problem ? What is not working for you ? It looks to me like you need to add code to insert your column names, which seems like a trivial task to me. You could store them in an array so you read them by index, too, or you could write code that does not use a loop, to hard code the column names. Either way, it seems like a poor approach to me, but that would work.
这篇关于如何动态地分割字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文