Fajar Ahmas Ahmas
Fajar Ahmas Ahmas

Reputation: 19

Convert multiline string into a single line

I would like to do the following thing but I can't - Textbox1.Text

line 1 = 1 2
line 2 = 3 4
line 3 = 1 7
line 4 = 4 9

I want to return this to look like this: yes, and with doubles like 1 1 and so on

Textbox1.Text >Expected Output

line 1 = 1
line 2 = 2
line 3 = 3
line 4 = 4
line 5 = 1
line 6 = 7
line 7 = 4
line 8 = 9

each digit must be on a separate line. that is, on each line that digit, there are now 2 digits on a line.

Code:

source = source.Replace(vbLf, "").Replace(vbCr, "")

Upvotes: 0

Views: 561

Answers (2)

Fabio
Fabio

Reputation: 32455

You can split original text into values and then combine them together into new lines of text

textbox.Lines = textbox.Lines.
    SelectMany(Function(ln) ln.Split(" "c, StringSplitOptions.RemoveEmptyEntries).
    ToArray()

Upvotes: 2

Idle_Mind
Idle_Mind

Reputation: 39132

each digit must be on a separate line. that is, on each line that digit, there are now 2 digits on a line.

I'm assuming you don't actually have line x = in your TextBox.

Rather you have:

1 2
3 4
1 7
4 9

and want:

1
2
3
4
1
7
4
9

If so, here's a one-liner that can do it for you...

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    TextBox1.Lines = String.Join(" ", TextBox1.Lines).Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
End Sub

Upvotes: 1

Related Questions