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
To align string to the right or to the left use static method String.Format. 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).
This example shows how to align strings with spaces. The example formats text to table and writes it to console output. To align string to the right or to the left use static method String.Format.
Use %*d and % {int}d Notations to Align Columns in Table This article will demonstrate multiple methods about how to align columns in the printf function in C. Use % {integer}d Notation to Align Output in C printf is part of the standard I/O library, and it can be utilized to output formatted string to the stdout stream.
Use %*d Notation to Align Output in C Alternatively, the same formatting can be achieved using the %*d notation, which additionally requires the integer to be supplied as data arguments are - after the comma.
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".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With