'I need something in c# which work like the setw() in c++
I am using a richTextBox in c#. I need to display strings of different length inside one richTextBox but these strings should be perfectly aligned.. this is an example..
abcd abcde abcde
ab abc abcdef
I know how to do this in c++ using the setw function.. but I could not find an equivalent in c#.
Solution 1:[1]
Solution 2:[2]
string varName=String.Format("{0,10:D}", 2);
This will format number 2 as a string of width 10 aligned right, use -5 to have it aligned left in the width of 5...
Source : http://answers.yahoo.com/question/index?qid=20100727164827AAqJ1Hn
Solution 3:[3]
I created a function for that purpose:
public string tab(string s, int w)
{
//w is the desired width
int stringwidth = s.Length;
int i;
string resultstring = s;
for(i=0;i<=(w-stringwidth)/8;i++)
{
resultstring = resultstring + "\t";
}
return resultstring;
}
Then, adding it to a ListBox, for example:
listBox.Items.Add(tab("MyFullNameHere",30)+ tab("MyContact - xxxxx",12));
listBox.Items.Add(tab("MyWifeFullNameHereVeryLong", 30) + tab("HerContact - xxxxx", 12));
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Julien |
Solution 3 | DiogoA. |