Console two-line exit

I have this code that outputs some value from an array, plus -in a new line below the valuea[i]

Console.Write(a[i] + "\n-");

So it looks like

a
-

Now I have more codes Console.Write(a[i+1] + "\n-");and it is output as follows

a
-b
-c
-d
-

I know why this is so, but how can I return one line after each new line \n? So i can get it

abcd
----
+4
source share
6 answers

You should print the values ​​first, then the dash:

Console.WriteLine(string.Join("", a)); // make a string of all values and write a line end
Console.WriteLine(string.Join("", a.Select(v => "-"))); // write the dashes

[ Ideone ]

+7
source

Another approach with Concat:

char[] a = {'a','b','c','d'};
Console.WriteLine(string.Concat(a));
Console.WriteLine(new string('-',a.Length));

https://dotnetfiddle.net/IZdlX5

+2
source

.
. , , , , .
- cusor, SetCursorPosition (int Left, int Top) Write ('_'), .

+1

" ". , . , a, . , - :

Console.WriteLine(string.Join("", a));
Console.WriteLine(string.Join("", a.Select(_ => "-")));

The first line combines all the elements of the array ainto one line, the second line uses the LINQ trick to “query” the array a, but returns a hyphen instead of any of this array, then outputs the same path.

0
source

One linq with Linq:

Console.Write(String.Concat(a) + "\n" + new String('-', a.Length));
0
source

What you are looking for is Console.SetCursorPosition

var baseLine = Console.CursorTop;
char[] a = {'a', 'b', 'c', 'd'};
char[] b = {'A', 'B', 'C', 'D'};
string[] conditions = {"a", "a", "both", "b"}; // Which arrays to show
for (int i = 0; i < a.Length; i++)
{
    Console.SetCursorPosition(i, baseLine);
    Console.Write(conditions[i] != "b" ? a[i] : '-');
    Console.SetCursorPosition(i, baseLine + 1);
    Console.Write(conditions[i] != "a" ? b[i] : '-');
}

baseLine necessary because you don’t know which line you are going to start.

Conclusion:

abc-
--CD
0
source

Source: https://habr.com/ru/post/1673499/


All Articles