ElektroStudios
ElektroStudios

Reputation: 20494

How to Join multiline strings from left to right

I have an array with 3 entries.

First entry of the array is a multiline string:

"My dog 
you            "

Second entry too:

" dont like that
               "

And hird entry too:

" ...       
 will die!     "

Now how I can combine all the multiline strings to obtain this joined string from left to right like this?:

My dog dont like that...       
you will die!

What I tried:

RichTextBox1.Text = String.Join(MyArray(1), MyArray(2))

Well another example more reallistic, what I really need is to combine multiline strings (which are ASCII letters) stored in a array, but when I try to join it all together I obtain a string joined from up to down:

enter image description here

The code that I've used :

RichTextBox1.Text = String.Join(" ", Characters(70), Characters(77), Characters(70), Characters(76))

Upvotes: 1

Views: 150

Answers (1)

Jaaromy Zierse
Jaaromy Zierse

Reputation: 569

This should get you what you need. The key was creating a list of lists and splitting on the newline character on each item in the original Array.

This should work for an arbitrary number of strings, but each string must have the same number of NewLine characters to break on.

For example:

"Yo \nDon't kill \nMan!"
"dude! \nme bro!       "

This will cause an exception because the second line only has one \n

To fix this the second line could be changed to:

"Yo \nDon't kill \nMan!"
"dude! \nme bro!     \n"

This should give you the proper formatting.

In VB:

Imports System.Text
Module Module1

    Sub Main()
        Dim items As List(Of String) = New List(Of String)()
        items.Add("My dog\nyou")
        items.Add(" dont like that\n")
        items.Add(" ...       \n will die!")
        Dim list As List(Of List(Of String)) = New List(Of List(Of String))
        Dim arg() As String = {"\n"}
        For Each listItem As String In items
            list.Add(listItem.Split(arg, StringSplitOptions.None).ToList())
        Next

        Dim sb As StringBuilder = New StringBuilder()

        For i As Integer = 0 To list(0).Count - 1
            For j As Integer = 0 To list.Count - 1
                sb.Append(list(j)(i))
            Next
            sb.Append(Environment.NewLine)
        Next

        Console.WriteLine(sb.ToString())
        Console.ReadKey()
    End Sub
End Module

And C# (my preference, but it's all good :) ):

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> items = new List<string>();
            items.Add("My dog\nyou");
            items.Add(" dont like that\n");
            items.Add(" ...       \n will die!");
            List<List<string>> list = new List<List<string>>();
            items.ForEach(f => list.Add(f.Split('\n').ToList()));
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < list[0].Count; i++)
            {
                for (int j = 0; j < list.Count; j++)
                {
                    sb.Append(list[j][i]);
                }
                sb.Append(Environment.NewLine);
            }

            Console.WriteLine(sb.ToString());
            Console.ReadKey();
        }
    }
}

Upvotes: 1

Related Questions