12

I need to format a variable with string interpolation, and the format string is another variable:

here is my sample code:

static void Main(string[] args)
{
    int i = 12345;

    Console.WriteLine($"Test 1: {i:N5}");

    var formatString = "N5";

    Console.WriteLine($"Test 2: {i:formatString}");
}

Test 1 works, Test 2 don't work.

What's the exact syntax for Test 2 ?

6 Answers 6

12

The shortest way you can do this 'syntactically' without String.Format, is using ToString:

$"Test 2: {i.ToString(formatString)}"
Sign up to request clarification or add additional context in comments.

Comments

8

Your code is equivalent to:

Console.WriteLine(String.Format("Test 2: {0:formatString}", i));

As the formatString is in the format string, you would nest String.Format calls to put the value in the format string:

Console.WriteLine(String.Format(String.Format("Test 2: {{0:{0}}}", formatstring), i));

This isn't supported with string interpolation.

7 Comments

Hard to believe, because if so, would be a big limitation compared with the old C# string.format
interesting by your explanation this can be done with half interpolation and half string format.Console.WriteLine($"Test 2: {{0:{formatString}}}", i);
but another problem arise if I assign it to a string, rather than writing to a console. StyleCop will signal a warning: here's my code.var test = string.Format($"Test 2: {{0:{formatString}}}", i); the warning is "SA1127 : CSharp.Readability : Use string interpolation rather than string format."
@user2289427: I would suggest that you format the value using ToString instead: $"Test 2: {i.ToString(formatString)}".
@Guffa, what if type is object rather than int ?
|
1

C# has no syntax that will do what you want.

Comments

1

I have tested this piece of code and it seems to work:

static void Main(string[] args)
{
    int i = 12345;

    Console.WriteLine("Test 1: {0:N5}",i);

    var formatString = "N5";

    Console.WriteLine("Test 2: {0:" + formatString + "}", i);

    Console.ReadLine();
}

4 Comments

It's not string interpolation though.
It's exactly what I would like to avoid, the old syntax of C#
And could you do with a double string.Format? Console.WriteLine(string.Format("Test 2: {{0:{0}}}",formatString), i);
It would be even worse as you're using twice the syntax he wants to avoid.
0

The string interpolation happens in the compilation stage. You cannot use variables in the format strings because of that.

3 Comments

The fact that it runs at compile-time has nothing to do with that limitation. The compiler could easily concatenate your string into the format string.
@SLaks It could do whatever, but it doesn't. Would a better answer be "because it's done that way"?
A better answer would just remove your first sentence; it isn't relevant at all.
-1

You can make a simple extension method that allows you to call a formattable ToString method on any object. The IFormattable interface is the same way that string.Format or interpolated strings would use to format an object of unknown type.

public static string ToString(this object value, string format, IFormatProvider provider = null)
    => (value as IFormattable)?.ToString(format, provider) ?? value.ToString();

And to use:

object i = 12345;
var formatString = "N5";

Console.WriteLine($"Test 2: {i.ToString(formatString)}");

3 Comments

You are creating a static method on object, for which a similar implementation already exists on the Int32 type, which is used in the question. Also you're boxing/unboxing the int which is unnecessary.
You didn't realize that the object is of UNKNOWN type. Yes, I used an int as example. But the intent is to format an unknown type object.
Then its better to make a static method on ’IFormattable’ not ’object’ to show the intent and supported types.

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.