Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a built-in function for horizontal string concatenation?

Tags:

c#

Given two files:

File1

a a a
b b b
c c c

File2

d d
e e

Bash has a command that will horizontally concatenate these files:

paste File1 File2

a a a d d
b b b e e
c c c

Does C# have a built-in function that behaves like this?

like image 461
Rainbolt Avatar asked Aug 20 '15 15:08

Rainbolt


1 Answers

public void ConcatStreams(TextReader left, TextReader right, TextWriter output, string separator = " ")
{
    while (true)
    {
        string leftLine = left.ReadLine();
        string rightLine = right.ReadLine();
        if (leftLine == null && rightLine == null)
            return;

        output.Write((leftLine ?? ""));
        output.Write(separator);
        output.WriteLine((rightLine ?? ""));
    }
}

Example use:

StringReader a = new StringReader(@"a a a
b b b
c c c";
StringReader b = new StringReader(@"d d
e e";

StringWriter c = new StringWriter();
ConcatStreams(a, b, c);
Console.WriteLine(c.ToString());
// a a a d d
// b b b e e
// c c c 
like image 171
poke Avatar answered Sep 29 '22 09:09

poke