C#Windows窗体中的String Builder [英] String Builder in C# Windows form

查看:88
本文介绍了C#Windows窗体中的String Builder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好
我正在使用C#Windows窗体,在下面的字符串生成器中有数据,我给出了模式

Hi All
I am working on C# Windows form where I have data in String Builder below I give the pattern

String Builder sb = val1,val2,val3,val4,val5,val6,val7;


我也有四个文本框
现在我的问题是如何将sb数据放入文本框,如下所示:


I have also four Text Box
Now my question is how can I put the sb data to Text Box like following

TextBox1.text = val2;
TextBox2.text = val3;
TextBox3.text = val5;
TextBox4.text = val7;


谢谢&问候
Indrajit Dasgupta


Thanks & Regards
Indrajit Dasgupta

推荐答案


您已经有一个字符串.仅在需要附加字符串的情况下才建议使用StringBuilder.否则,最好直接使用string对象.因为在这里您要为StringBuilder对象分配额外的内存.最重要的是,您需要方法ToString()将其转换为string对象.相反,最好直接使用string对象(对于当前方案更好).
Hi,
You already have a string. StringBuilder is recommended only in case where you need to append the string. Otherwise it is better to use string object directly. Because here you are allocating extra memory to StringBuilder object. And on top of that you need a method ToString() to convert that to a string object. Instead its better to use string object directly (it is better for the current scenario).
string s = "val1,val2,val3,val4,val5,val6,val7";
var sSplit = s.Split(',');
txtBox1.Text = sSplit[1];
txtBox2.Text = sSplit[2];
txtBox3.Text = sSplit[4];
txtBox4.Text = sSplit[6];


嗨...尝试以下代码:
hi... try below code:
string[] splitted = sb.ToString().Split(',');


现在,数组拆分将在索引0处具有元素val1,在索引1处具有元素val2,依此类推.然后,您可以分配给文本框:


Now array splitted will have elements val1 at index 0, val2 at index 1, and so on. You can then assign to Textboxes:

txtBox1.Text = splitted[1];
txtBox2.Text = splitted[2];
txtBox3.Text = splitted[3];
txtBox4.Text = splitted[4];


尝试类似的方法,

Try Something Like this,

StringBuilder sb = new StringBuilder();
            sb.Append("val1,val2,val3,val4,val5,val6,val7");    

            string[] Splits = (sb.ToString()).Split(',');
            textBox1.Text = Splits[1];
            textBox2.Text = Splits[2];
            textBox3.Text = Splits[4];
            textBox4.Text = Splits[6];




问候
sarva




regards
sarva


这篇关于C#Windows窗体中的String Builder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆