Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to align strings in C# using string.Format() function?

Tags:

string

c#

.net

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

like image 923
Antun Tun Avatar asked Apr 19 '13 12:04

Antun Tun


People also ask

How to align string to the right or to the left?

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

How to align strings with spaces in JavaScript?

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.

How to align columns in printf in C?

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.

How do you align an integer in C with a comma?

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.


1 Answers

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".
like image 52
Hossein Narimani Rad Avatar answered Nov 02 '22 23:11

Hossein Narimani Rad