I need something in c# which work like the setw() in c++
-
12-12-2019 - |
Question
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
You could do use String.PadRight
innerString.PadRight(10);
OTHER TIPS
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
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));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow