简体   繁体   中英

C# text alignment

reading some string from a text file and the writing them to a text file there only a small problem and that's with the alignment of the text. The {4} parameter is what needs to be formatted to the right so that they are all vertically aligned.

while (recordIn != null)
{
    fields = recordIn.Split(DELIM);
    emp.accNumber = Convert.ToInt32(fields[0]);
    emp.lastName = fields[1];
    emp.firstName = fields[2];
    emp.funds = Convert.ToDouble(fields[3]);
    double money = Convert.ToDouble(fields[3].ToString());


    if (money < 0)
    {
        Console.WriteLine("{0},{1},{2}, {3, 2}, {4}", emp.accNumber, emp.lastName, emp.firstName, emp.funds.ToString("F2"), creditOutput);
    }
    else
    {
        Console.WriteLine("{0},{1},{2}, {3, 2} {4}", emp.accNumber, emp.lastName, emp.firstName, emp.funds.ToString("F2"), debitOutput);            
    }
    recordIn = reader.ReadLine();
}

You can try string.PadLeft or string.PadRight

Also you can do it like this:

To align string to the left (spaces on the right) use formatting patern with comma (,) followed by a negative number of characters: String.Format("{0,–10}", text) . To right alignment use a positive number: {0,10}.

You could separate all values with a tab:

Console.WriteLine("{0} \t {1} \t etc... ", emp.accNumber, emp.last name...

Or you could just add a tab before the {4} just to align these. Two tabs may be necessary.

I just did the following in Visual Studio 2012

Console.WriteLine("{0,200}", "SomeText");

It right aligned the "SomeText" in a 200 character field. You can left align with "{0,-200}";

Console.WriteLine("{0,-200}", "SomeText");

From: http://msdn.microsoft.com/en-us/library/system.string.format(v=vs.110).aspx

The format item A format item has this syntax:

{ index[,alignment][ :formatString] }

Brackets denote optional elements. The opening and closing braces are required. (To include a literal opening or closing brace in the format string, see the "Escaping Braces" section in the Composite Formatting article.) For example, a format item to format a currency value might appears like this: C#C++VB

String.Format("{0,-10:C}", 126347.89m);         

A format item has the following elements:

index

The zero-based index of the argument whose string representation is to be included at this position in the string. If this argument is null, an empty string will be included at this position in the string.

alignment

Optional. A signed integer that indicates the total length of the field into which the argument is inserted and whether it is right-aligned (a positive integer) or left-aligned (a negative integer). If you omit alignment, the string representation of the corresponding argument is inserted in a field with no leading or trailing spaces.

formatString

Optional. A string that specifies the format of the corresponding argument's result string. If you omit formatString, the corresponding argument's parameterless ToString method is called to produce its string representation. If you specify formatString, the argument referenced by the format item must implement the IFormattable interface. Types that support format strings include:

Use the alignment in the format item, as in {4,10} . This makes your {4} column 10 characters wide and aligns the content to the right. Full syntax: { index[,alignment][ :formatString] }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM