5

I want to do something like

String.Format("Completed {0:9} of ",0) + xlsx.totalCount.ToString();

except instead of hardcoding a 9 I want the alignment to be whatever xlsx.totalCount is. Any thoughts?

1
  • 4
    I'm thinking either string.PadLeft or new string(c, count)... Commented Jun 17, 2011 at 20:21

3 Answers 3

4

Try it like this:

string formatString = "{0:" + xlsx.totalCount.ToString() + "}";
String.Format("Completed " + formatString + " of ", 0) + xlsx.totalCount.ToString();
Sign up to request clarification or add additional context in comments.

Comments

3

The string doesn't have to be a compile time constant, you can build the string during runtime (using a StringBuilder, operator+ or even a nested String.Format). This, for instance will produce the needed string with xlsx.totalCount replacing the "9":

String.Format("Completed {0:" + xlsx.totalCount + "} of "...

Comments

0

I'd assumed that he wanted a count of 9s depending on the value of xlsx.totalCount.

   StringBuilder sb = new StringBuilder();
   sb.Append( '9', xlsx.totalCount );
   String.Format( "Completed {0:" + sb.ToString() + "} of ",0) + xlsx.totalCount.ToString();

Again, there feels like there should be an easier way of building a chain of 9s, but not in 3 minutes of thinking, apparently.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.