How to format a string with fixed width fields

14,874

Solution 1

You can try using \t in your strings which will insert a tab or you can try padding each portion so they always take up the same amount space.

For example:

string fileName = file.Name.PadRight(50);

will ensure that the string fileName is at least 50 characters long. I say at least because you could always have a file name that is larger than 50 characters.

Solution 2

foreach (RemoteFileInfo file in MySession.EnumerateRemoteFiles(directory.RemoteDirectory, directory.RemoteFiles, EnumerationOptions.None))
{
    int lines= File.ReadAllLines(Path.Combine(directory.LocalDirectory, file.Name)).Length.ToString();
    string appending = String.Format("{0,2}.- {1,-18} RecordCount: {3}", file.Name, lines);
    BodyMessage.Append(appending);
    index++;
}

See MSDN: String.Format Method.

Solution 3

Firstly, use string.Format, rather than concatenation:

int lineCount = File.ReadAllLines(Path.Combine(directory.LocalDirectory, file.Name)).Length.ToString();
string message = string.Format("{0}. {1}\t Record Count: " {2}\n", (index + 1), file.Name, lineCount);

To answer your question, you can align text within a formatted string using the following syntax:

string message = string.Format("{0}. {1,-10}\t Record Count: " {2}\n", (index + 1), file.Name, lineCount);

The additional -10 will ensure that the inserted text is left-padded to 10 characters.

Share:
14,874
Javier Salas
Author by

Javier Salas

I like to learn, travel and discover new things and places. Music is my passion and programming has become one of the things that I like to do!

Updated on July 30, 2022

Comments

  • Javier Salas
    Javier Salas over 1 year

    I'm was wondering if there is a way to format a string to format a string, what I mean is, I have a foreach loop that get a information from some files and how many records has each file, as the length of each file is different the format is change.

    My example is, I have 3 files:

     1.- MyFile1.txt   RecordCount: 5
     2.- anotherfile.txt    RecordCount: 8
     3.- MyTestFile.doc   RecordCount: 17
    

    As you can see are not formated, I want something like this:

     1.- MyFile1.txt        RecordCount: 5
     2.- anotherfile.txt    RecordCount: 8
     3.- MyTestFile.doc     RecordCount: 17
    

    does not matter the length of the file, RecordCount will be in the same place.

    What I have is this:

     foreach (RemoteFileInfo file in MySession.EnumerateRemoteFiles(directory.RemoteDirectory, directory.RemoteFiles, EnumerationOptions.None))
     {
         BodyMessage.Append((index + 1) + ". " + file.Name + "        Record Count: " + File.ReadAllLines(Path.Combine(directory.LocalDirectory, file.Name)).Length.ToString() + "\n");
         index++;
     }
    

    Any idea?

  • Jonathan Lidbeck
    Jonathan Lidbeck over 2 years
    You can also use the slightly more concise interpolated string format, as long as your C# version is up to date: string message = $"{index+1,5}. {file.Name,-10}\t Record Count: {lineCount}\n"; Note that ,5 will left-pad the numbers to width 5, while ,-10 will right-pad the filenames.