6

Output is to the word document (docx).

I would like to make outuput like this using C# 4.0 string.Format:

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

I am using string.Format("{0,-10} {1,-10}",string1,string2) but it does nothing. Is there a way how to align strings using string.Format()? I saw '\t' solution but how to add it into string.Fornmat()?

Here is my code and my output:

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++));

Output: Title: Author: aaa Geändert an: 19.04.2013

Title: second Author: aaa Geändert an: 18.04.2013

Title: slika Author: aaa Geändert an: 18.04.2013

Title: d m Author: aaa Geändert an: 18.04.2013

Title: Mathias Author: aaa Geändert an: 19.04.2013

Title: QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQqq Author: aaa Geändert an: 19.04.2013

12
  • It would be helpful if you would post what you have tried. Its not even clear what you want to be honest. Please verify all code in your post because string.Fornmat does not exist. Commented Apr 19, 2013 at 12:16
  • You can add \t into your format string anywhere (e.g. string.Format("\t{0}\t{1}", ...)). What is the significant part of your output? How do you want your strings to be aligned? Commented Apr 19, 2013 at 12:17
  • Look at Here Commented Apr 19, 2013 at 12:18
  • 1
    What kind of application / control ? Answer won't be the same for a console app or winform or web... Commented Apr 19, 2013 at 12:20
  • 1
    Oh, okay. But then there should be some other kind of table structure available to you. Getting your output to "look good" by basically just adding or removing spaces is next to impossible if you use variable width fonts. Commented Apr 19, 2013 at 13:14

5 Answers 5

10

Try using PadLeft and PadRight methods. For example:

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

Also you can use a character to fill the spaces. From 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".
Sign up to request clarification or add additional context in comments.

Comments

4

Formatting values like that works fine, but you need to specify a length that is longer than the string values, otherwise it won't add any spaces.

However, if you want a comma after a value and add padding after it, you need to put the comma in the value and then format it.

Example:

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));

Output:

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

Comments

2

The import part of the below sample is

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

Please have a look at the following example of 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);
        }
    }

Comments

1

If you are writing to DOCX format, you should be using the Office Open XML format. (Also see here for more details.)

Then to align the data in a tabular format, you should insert a table into the document, and populate the table with your desired data.

Here is some sample code that demonstrates how to programmatically insert a table into a DOCX file.

You should be able to hide the borders of the table so that you are only left with the text in the table cells visible.

Comments

0

you can see the second code sample http://msdn.microsoft.com/en-us/library/system.string.format.aspx.

I also used to use the padLeft and padRight methods

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.