Formatting output question

gqstud79

Newcomer
Joined
Sep 28, 2002
Messages
11
I am trying to output elements of an array to a console window. Is there any built-in formatting command that will allow me to set the width between each of the elements as they are written to the console? I know that in C++ there is the "setw()" command, but is there something similar in C#?
 
Could someone possibly direct me to some examples of how to use these methods? I can't seem to figure out how to set them up with the proper arguments. Thank you.
 
C#:
// Fill an array for testing
string[] strings = new string[10];
for(int i=0; i<strings.Length; i++) strings[i] = (i * 13).ToString();

for(int i=0; i<strings.Length; i++)
	Console.Write("{0,10}", strings[i]);
Console.WriteLine();

You can use "{0,-10} in the Console.Write call to left-align.

You can use the same format with String.Format as with Console.WriteLine, so:
C#:
string s1 = string.empty;
// Fill an array for testing
string[] strings = new string[10];
for(int i=0; i<strings.Length; i++) strings[i] = (i * 13).ToString();

for(int i=0; i<strings.Length; i++)
	s1 += String.Format("{0,10}", strings[i]);

Console.WriteLine(s1);


-Nerseus
 
Back
Top