Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

int.ToString(format) not inserting " literal

I have the following format var format = "0\"";

I then use it like this 1.ToString(format);

I'm expecting it to return 1" but it returns 1

How do I make it insert the double quote (")?

I have tried...

var format = "0\u0022";
var format = @"0""";

and can't get it to work.

It does work if I use string.Format...

var format = "{0}\"";
string.Format(format, 1)

that will give me 1" as required.

Does anyone know how to get .ToString() method to insert the double quote?

like image 872
Phil Avatar asked Jan 31 '19 12:01

Phil


Video Answer


1 Answers

When you want to add characters into format string as the they are (not as a part of the format string), wrap them into apostrophes '...':

 // 0 - specifies format
 // '\"' - will be preserved as it is - " 
 string result = 1.ToString("0'\"'");

 Console.Write(result);

Outcome:

 1"
like image 80
Dmitry Bychenko Avatar answered Sep 28 '22 08:09

Dmitry Bychenko