How to format a string with fixed width fields in C#

2 Answers

0 votes
using System;

class Program
{
    static void Main()
    {
        String str = "CSharp";

        Console.Write("{0, 3},{1, 10},{2, 4}", str, "abcd", "123");
    }
}


/*
run:

CSharp,      abcd, 123

*/

 



answered Apr 12, 2025 by avibootz
0 votes
using System;

class Program
{
    static void Main()
    {
        String fileName = "abc.cs";
        
        String str = String.Format("{0, 3} {1, 8}", fileName, "17MB");

        Console.Write(str);
    }
}


/*
run:

abc.cs     17MB

*/

 



answered Apr 12, 2025 by avibootz
...