如何在c#中使用string.Format()函数对齐字符串

本文关键字:Format 函数 字符串 对齐 string | 更新日期: 2023-09-27 18:09:57

输出到word文档(docx)。

我想用c# 4.0字符串做这样的输出。格式:

string1: stringValue1,  string2:stringValue2
string4: stringValue4,  string5:stringValue5
string6: stringValue6,  string7:stringValue7
string8: stringValue8,  string9:stringValue9

我正在使用string.Format("{0,-10} {1,-10}",string1,string2),但它什么也不做。是否有一种方法如何使用string.Format()对齐字符串?我看到''t'解决方案,但如何将其添加到string. format ()?

下面是我的代码和输出:
string titel = HttpUtility.HtmlEncode(docItem.Title);
                    string href = docItem.FullURL;
                    string title = string.Format("{0}", titel);
                    string author = docItem.Author;
                    string date = docItem.ChangedDate.Date.ToShortDateString();
                    CreateTextBoxConentParagraph(doc, string.Format("Title: <a href='{0}'>{1}</a> Author: {2} Geändert an: {3}",href,title.PadRight(20),author.PadRight(20), date),string.Format("chunkId_{0}", i++));

输出:标题:作者:aaa Geändert an: 19.04.2013

标题:第二作者:aaa Geändert an: 18.04.2013

标题:slika作者:aaa Geändert an: 18.04.2013

标题:d m作者:aaa Geändert and: 18.04.2013

标题:Mathias作者:aaa Geändert an: 19.04.2013

标题:qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq作者:aaa Geändert an: 19.04.2013

如何在c#中使用string.Format()函数对齐字符串

尝试使用PadLeftPadRight方法。例如:

string.Format("{0} {1}",string1.PadRight(25),string2.PadRight(25)) 

也可以用一个字符来填充空格。从MSDN:

string str = "forty-two";
char pad = '.';
Console.WriteLine(str.PadRight(15, pad));    // Displays "forty-two......".
Console.WriteLine(str.PadRight(2,  pad));    // Displays "forty-two".

像这样格式化值是可以的,但是你需要指定一个长度大于字符串值的长度,否则它不会添加任何空格。

但是,如果您想在值后面加逗号并在其后面添加填充,则需要将逗号放在值中,然后对其进行格式化。

的例子:

string string1 = "asdf";
string string2 = "ffs";
string string3 = "What about me?";
string string4 = "gahah";
string string5 = "kjshdfkjh";
string string6 = "fg";
string string7 = "kkkdd";
string string8 = "asdasdf";
string string9 = "jjfjj";
Console.WriteLine(String.Format("string1: {0,-10} string2: {1,-10}", string1 + ",", string2));
Console.WriteLine(String.Format("string4: {0,-10} string5: {1,-10}", string4 + ",", string5));
Console.WriteLine(String.Format("string6: {0,-10} string7: {1,-10}", string6 + ",", string7));
Console.WriteLine(String.Format("string8: {0,-10} string9: {1,-10}", string8 + ",", string9));
输出:

string1: asdf,      string2: ffs
string4: gahah,     string5: kjshdfkjh
string6: fg,        string7: kkkdd
string8: asdasdf,   string9: jjfjj

下面示例的导入部分是

String.Format("{0,-12}{1,8}{2,12}{1,8}{2,12}{3,14}'n",
                                      "City", "Year", "Population", "Change (%)")

请看下面的MSDN

示例
public static void Main()
    {
        // Create array of 5-tuples with population data for three U.S. cities, 1940-1950.
        Tuple<string, DateTime, int, DateTime, int>[] cities = 
      { Tuple.Create("Los Angeles", new DateTime(1940, 1, 1), 1504277, 
                     new DateTime(1950, 1, 1), 1970358),
        Tuple.Create("New York", new DateTime(1940, 1, 1), 7454995, 
                     new DateTime(1950, 1, 1), 7891957),  
        Tuple.Create("Chicago", new DateTime(1940, 1, 1), 3396808, 
                     new DateTime(1950, 1, 1), 3620962),  
        Tuple.Create("Detroit", new DateTime(1940, 1, 1), 1623452, 
                     new DateTime(1950, 1, 1), 1849568) };
        // Display header 
        string header = String.Format("{0,-12}{1,8}{2,12}{1,8}{2,12}{3,14}'n",
                                      "City", "Year", "Population", "Change (%)");
        Console.WriteLine(header);
        string output;
        foreach (var city in cities)
        {
            output = String.Format("{0,-12}{1,8:yyyy}{2,12:N0}{3,8:yyyy}{4,12:N0}{5,14:P1}",
                                   city.Item1, city.Item2, city.Item3, city.Item4, city.Item5,
                                   (city.Item5 - city.Item3) / (double)city.Item3);
            Console.WriteLine(output);
        }
    }

如果写的是DOCX格式,那么应该使用Office Open XML格式。(详情请参阅此处)

然后,为了以表格格式对齐数据,您应该在文档中插入一个表格,并将所需的数据填充到表格中。

下面是一些示例代码,演示如何以编程方式将表插入到DOCX文件中。

您应该能够隐藏表格的边框,这样您就只剩下表格单元格中的文本可见。

可以看到第二个代码示例http://msdn.microsoft.com/en-us/library/system.string.format.aspx.

我还使用了padLeft和padRight方法