{0,4} {1,4} {2,4} {3,4} {4,4}

image_pdfimage_print

using System;

public class MainClass {
public static void Main() {
Random rnd = new Random();
int[,] m = new int[3, 5];
for (int i = 0; i < m.GetLength(0); i++) for (int j = 0; j < m.GetLength(1); j++) m[i, j] = rnd.Next(1000); for (int i = 0; i < m.GetLength(0); i++) Console.WriteLine("{0,4} {1,4} {2,4} {3,4} {4,4}", m[i, 0], m[i, 1], m[i, 2], m[i, 3], m[i, 4]); } } [/csharp]

left justify and align a set of strings to improve the appearance of program output

image_pdfimage_print

using System;

class Class1 {
public static void Main(string[] args) {
string[] names = {“CA “,” SA”,”J A”,”SA”,” SA “};

foreach (string s in names) {
Console.WriteLine(“This is the name '{0}' before”, s);
}
string[] sAlignedNames = TrimAndPad(names);

foreach (string s in sAlignedNames) {
Console.WriteLine(“This is the name '{0}' afterwards”, s);
}
}

public static string[] TrimAndPad(string[] strings) {
string[] stringsToAlign = new String[strings.Length];

for (int i = 0; i < stringsToAlign.Length; i++) { stringsToAlign[i] = strings[i].Trim(); } int nMaxLength = 0; foreach (string s in stringsToAlign) { if (s.Length > nMaxLength) {
nMaxLength = s.Length;
}
}
for (int i = 0; i < stringsToAlign.Length; i++) { stringsToAlign[i] = stringsToAlign[i].PadRight(nMaxLength + 1); } return stringsToAlign; } } [/csharp]

Control the width

image_pdfimage_print
   
  

using System;
public class TestConsoleApp {
    public static void Main(string[] args) {
        Console.WriteLine("
{0,-10}{1,-3}", "Name", "Salary");
        Console.WriteLine("{0,-10}{1,6}", "Bill", 123456);
        Console.WriteLine("{0,-10}{1,6}", "Polly", 7890);
    }
}

   
     


Format a string

image_pdfimage_print
   
  

using System;
using System.Collections.Generic;
using System.Text;

class Program {
    static void Main(string[] args) {
        int theInt = 90;
        double theDouble = 9.99;
        bool theBool = true;

        Console.WriteLine("Int is: {0}
Double is: {1}
Bool is: {2}",theInt, theDouble, theBool);
    }
}