How to replace space as a newline in a textBox with Multiline property set to true?

后端 未结 5 924
闹比i
闹比i 2021-01-28 23:14

Suppose I have this string:

string str = \"The quick brown fox     jumps over the lazy dog\";

How can I replace or ignore the spaces in the str

相关标签:
5条回答
  • 2021-01-28 23:49
    textBox.Text = String.Join(Environment.NewLine, str.Split(new char[] {' ' }, StringSplitOptions.RemoveEmptyEntries));
    

    UPDATE: Of course, StringSplitOptions.RemoveEmptyEntries should be used.

    UPDATE2: alternative version via regular expression

    textBox.Text = Regex.Replace(str, @"\s+", Environment.NewLine);
    
    0 讨论(0)
  • 2021-01-28 23:51
    mytextbox.Text=String.Join(Environment.NewLine,str.Split(new[]{' '},StringSplitOptions.RemoveEmptyEntries));
    

    Would be my guess, if I understand the question correctly.

    0 讨论(0)
  • 2021-01-28 23:53
    string str = "The quick brown fox     jumps over the lazy dog";
    string[] splits = str.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
    

    Hope that helps

    0 讨论(0)
  • 2021-01-28 23:59

    If you want compact and simple this may be the best bet:

    Textbox.Lines = MyString.Split(' ');
    

    If you're wanting to split the text already in the box this may work:

    Textbox.Lines = Textbox.Text.Split(' ');
    
    0 讨论(0)
  • 2021-01-29 00:15
    string str = "The quick brown fox     jumps over the lazy dog";
    string[] ab = str.Split(' ');
    if (ab != null && ab.Length > 0)
    {
        string de = ab[0].Trim();
        for (int i = 1; i < ab.Length; i++)
        {
            de += "\n" + ab[i];
        }
    }
    
    0 讨论(0)
提交回复
热议问题