yonan2236
yonan2236

Reputation: 13649

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

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 string and enter each word in the multiline textBox?

Expected output:

The  
quick    
brown  
fox  
jumps  
over  
the   
lazy   
dog  

My .NET framework version is 2.0

Upvotes: 0

Views: 4534

Answers (5)

RyanTimmons91
RyanTimmons91

Reputation: 460

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(' ');

Upvotes: 0

sathishkumar
sathishkumar

Reputation: 11

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

Upvotes: 0

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236268

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

Upvotes: 2

Brad Christie
Brad Christie

Reputation: 101614

mytextbox.Text=String.Join(Environment.NewLine,str.Split(new[]{' '},StringSplitOptions.RemoveEmptyEntries));

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

Upvotes: 4

Nickmaovich
Nickmaovich

Reputation: 512

string str = "The quick brown fox     jumps over the lazy dog";
string[] splits = str.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

Hope that helps

Upvotes: 0

Related Questions