Formatting strings with spaces

I need to do some kind of manual formatting in C # with only spaces. Here's what I am currently showing:

Conc2_CO   ( Y? = 170.2; Y? = 2; delta = -15)
atns_UreaMassFlowDemand   ( Y? = 0; Y? = 0; delta = 0)

      

And this is what I would like to have:

Conc2_CO                 ( Y? = 170.2; Y? = 2;     delta  = -15)
atns_UreaMassFlowDemand  ( Y? = 0;     Y? = 0;     delta  = 0)

      

I've tried playing with the length of the string using new string(' ', x)

, but it's a big pain and seems to work randomly since all characters are not the same length (i.e. l

shorter w

) .. Is there a better option?

Edit:

The resulting string is a construct with the concatenation of the name (left) and information that I add (right) in the library, so I cannot use string.format (), since I can only play with the right side.

I need to display this information in ZedGraph Legend using WinForms (but I doubt it will change anything).

Edit 2

Using paddings, this is what I have:

enter image description here

What is not what I want.

+3


source to share


2 answers


You can change your font to use monospace font .



This way you can use String.Format

and work with the spacers.

+2


source


I could delete the previous answer. Try the following:

 string s = String.Format("{0,-12}({1,8})", "Example", 223);

      



{0, -12} -0 - value index, -12 left align 12 characters {1,8} - 1 - value index, 8 - right 8 characters

More details: http://msdn.microsoft.com/pl-pl/library/system.string.format%28v=vs.110%29.aspx : Format Element

0


source







All Articles