Use String.Format() to align variable strings to the left and right of the fixed length string with overlap processing

170 views Asked by At

I'm trying to use format string for complex formatting with truncation.

Using format strings (stored in config file) would allow me to define different ways of truncating text without need to create more methods.

I need to:

  • set fix length for the string
  • align first variable to the left
  • align second variable to the right
  • if string from first and second variable overlap, second should override the first one.

Basic example is:

String.Format("{0} {1}", "Longword", " 10.00") // Longword 1000.00

Let's say I set the string to be 10 characters long. Then expected output will be Long 10.00.

For 20 characters expected output is Longword 10.00.

Is this possible to achieve using a single format string?

Best if the string length also can be a variable. But if it can't, workaround will be easy enough.

0

There are 0 answers