4

I have this code which output some value from array, plus - in new line under value a[i]

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

So it looks like this

a
-

Now i have more of Console.Write(a[i+1] + "\n-"); codes and it outputs like this

a
-b
-c
-d
-

I know why this is the case but how can I return one line up after every new line \n? So I can get this

abcd
----
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
gagro
  • 371
  • 7
  • 18
  • Can't you do it in 2 loops? The first writes all the letters, then the second writes all the `-`. – vyrp Mar 29 '17 at 11:56
  • @vyrp no, because it's now always case where dasher are under value, it could be oposite. I wrote it simple here because of understandind – gagro Mar 29 '17 at 12:05
  • @gagro: has the final edit changed your question - or do you really want all characters underlined - rather than the original with some "over-lined"? – PaulF Mar 29 '17 at 12:28

6 Answers6

8

You have to output the values first, then the dashes:

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]

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • I get your point but I can't do it like this because of **if else** statements. I edit my code so now it described better what I want. – gagro Mar 29 '17 at 12:19
  • 2
    That is something entirely different that your previous question. Ask a new question and put in there the conditions too. It is impossible to answer the new question without totally rewriting all answers. – Patrick Hofman Mar 29 '17 at 12:21
  • @PatrickHofman and fubo, you're right, sorry. I'm posting a new question :) – gagro Mar 29 '17 at 12:32
2

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

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
fubo
  • 44,811
  • 17
  • 103
  • 137
1

Their are two ways.
The first way is mentioned above.The general idea is to print two lines seperately and that gives you the absolutely same result as what you want in a very simple way.
The second way is to set the cusor position using SetCursorPosition(int Left ,int Top) right under the current position and Write('_') and then move the cusor back again.

0

You don't "go one line up". You output the first line and then output the second line. So first you'd output your whole a array, then output a line of hyphens the length of that array. Maybe something like this:

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

The first line would join all elements of the a array into a single string, the second line uses kind of a trick with LINQ to "query" the a array but return a hyphen instead of anything from that array, then outputs the same way.

David
  • 208,112
  • 36
  • 198
  • 279
0

One liner with Linq:

Console.Write(String.Concat(a) + "\n" + new String('-', a.Length));
Peter
  • 483
  • 7
  • 16
0

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 is needed because you don't know in which line you are going to start.

Output:

abc-
--CD
vyrp
  • 890
  • 7
  • 15