0

I've something like below.

var amount = "$1,000.99";
var formattedamount = string.Format("{0}{1}{0}", "\"", amount);

How can I achieve same using String interpolation?

I tried like below

var formattedamount1 = $"\"{amount}\"";

Is there any better way of doing this using string interpolation?

7
  • What is the desired output? Commented Jun 1, 2018 at 5:31
  • Gilead green, I want wrap in quotes so that amount will not populate in multiple cells in CSV file. Commented Jun 1, 2018 at 5:32
  • @PrasadKanaparthi that what you have in your first example, will not even compile. Commented Jun 1, 2018 at 5:34
  • @SeM, it is now. Please check Commented Jun 1, 2018 at 5:40
  • @PrasadKanaparthi You can check my answer below. Commented Jun 1, 2018 at 5:41

3 Answers 3

2

Update

Is there any better way of doing this using string interpolation

No, this is just string interpolation, you cant make the following any shorter and more readable really

var formattedamount1 = $"\"{amount}\"";

Original answer

$ - string interpolation (C# Reference)

To include a brace, "{" or "}", in the text produced by an interpolated string, use two braces, "{{" or "}}". For more information, see Escaping Braces.

Quotes are just escaped as normal

Example

string name = "Horace";
int age = 34;

Console.WriteLine($"He asked, \"Is your name {name}?\", but didn't wait for a reply :-{{");
Console.WriteLine($"{name} is {age} year{(age == 1 ? "" : "s")} old.");

Output

He asked, "Is your name Horace?", but didn't wait for a reply :-{
Horace is 34 years old.
Sign up to request clarification or add additional context in comments.

9 Comments

@GiladGreen there definitely was some confusion going on there though
@TheGeneral, what was the confusion.
@PrasadKanaparthi var formattedamount1 = $"{"\""{amount}{"\""}"; this line
@TheGeneral and this line - "How can I achieve same using String interpolation?"
@PrasadKanaparthi the question is why
|
1

Same thing you can achieve by doing:

var formattedamount1 = $"\"{amount}\"";

OR

var formattedamount1 = $@"""{amount}""";

It's basically allowing you to write string.Format(), but instead of using one string with "placeholders"({0}, {1}, .. {N}), you are directly writing/using your variable inside string.

Please read more about String Interpolation (DotNetPerls), $ - string interpolation to fully understand whats going on.

Comments

1

Just to give one more option, if you want to make sure you use the same quote at both the start and the end, you could use a separate variable for that:

string quote = "\"";
string amount = "$1,000.99";
string formattedAmount = $"{quote}{amount}{quote}";

I'm not sure I'd bother with that personally, but it's another option to consider.

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.