通过在winform中使用split,我的代码有问题 [英] It has something wrong in my code by using split in winform

查看:68
本文介绍了通过在winform中使用split,我的代码有问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I want output is : 
hello
mynameis
lam 
But it only show "lam" in the textBox1. I don't know why. Can you help me to fix it. Thank you so much!!





我的尝试:



我有这段代码:



What I have tried:

I have this code:

private void Form1_Load(object sender, EventArgs e)
{
    string s="hello\amynameis\alam";
    string[] arr = s.Split('\a');
    foreach (string str in arr)
    {
        textBox1.Text = str + "\n";
    }
}

推荐答案

试用:



Try it with :

private void Form1_Load(object sender, EventArgs e)
{
string s="hello\amynameis\alam";
string[] arr = s.Split('\a');
foreach (string str in arr)
{
textBox1.Text += str + "\n"; 
}
} 


查看你的代码:

Look at your code:
foreach (string str in arr)
    {
    textBox1.Text = str + "\n";
    }



每次循环,你用当前字符串覆盖文本框的内容,丢弃所有以前的数据。

您可以尝试:


Each time round the loop, you overwrite the content of the textbox with the current string, discarding all previous data.
You could try:

foreach (string str in arr)
    {
    textBox1.Text += str + "\n";
    }

但它会忽略换行符。

如果它是一个多行文本框,那么你最好这样做:

But it will ignore the newline.
If it's a Multiline text box, then you're better off doing this:

foreach (string str in arr)
    {
    textBox1.AppendText(str + "\n" );
    }


这适用于普通和多行文本框。

This will work for both normal and multi-line textbox.
string s = "hello\amynameis\alam";
string[] arr = s.Split('\a');
textBox1.Text = string.Join(" "+Environment.NewLine, arr);


这篇关于通过在winform中使用split,我的代码有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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