简体   繁体   English

C#文本对齐

[英]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. {4}参数需要向右格式化,以便它们全部垂直对齐。

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 您可以尝试string.PadLeftstring.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) . 要将字符串对齐到左侧(右侧的空格),请使用带逗号(,)的格式化patern,后跟负数字符: String.Format(“{0,-10}”,text) To right alignment use a positive number: {0,10}. 要右对齐,请使用正数:{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. 或者你可以在{4}之前添加一个标签,以便对齐它们。 Two tabs may be necessary. 可能需要两个标签。

I just did the following in Visual Studio 2012 我刚刚在Visual Studio 2012中执行了以下操作

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

It right aligned the "SomeText" in a 200 character field. 它右对齐200字符字段中的“SomeText”。 You can left align with "{0,-200}"; 你可以左对齐“{0,-200}”;

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

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

The format item A format item has this syntax: 格式项A格式项具有以下语法:

{ 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 (要在格式字符串中包含文字开头或右括号,请参阅“复合格式”一文中的“转义大括号”部分。)例如,格式化货币值的格式项可能如下所示: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. 如果此参数为null,则字符串中此位置将包含空字符串。

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 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. 如果省略formatString,则调用相应参数的无参数ToString方法以生成其字符串表示形式。 If you specify formatString, the argument referenced by the format item must implement the IFormattable interface. 如果指定formatString,则format项引用的参数必须实现IFormattable接口。 Types that support format strings include: 支持格式字符串的类型包括:

Use the alignment in the format item, as in {4,10} . 使用格式项中的对齐方式,如{4,10} This makes your {4} column 10 characters wide and aligns the content to the right. 这使您的{4}列宽10个字符,并将内容与右侧对齐。 Full syntax: { index[,alignment][ :formatString] } 完整语法: { index[,alignment][ :formatString] }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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