Home > Back-end >  Visual studio indent string
Visual studio indent string

Time:01-28

having this string

var commandAfterSerialize = $@"{{""LotId"":""00000000-0000-0000-0000-000000000000"",""AuctionId"":""00000000-0000-0000-0000-000000000000"",""DisableTotalSpendUpdate"":false,""AltInternetSurchargeRate"":null,""WinningBidders"":{{""{winningBidderKey}"":true,""{winningBidderKeySecond}"":false}},""Timestamp"":{timeStampUtc.Ticks},""TimestampUtc"":""{timeStampUtc:o}""}}";

is it possible to display it in better way in visual studio (for editing purpose) without modifing string itself ? without inserting \r\n into it ?

better formating like json

I am using visual studio 2022, c# 6.0

CodePudding user response:

C# 11

Allows you to use raw string literals

Example taken from the linked resource:

var xml = """
        <element attr="content">
            <body>
            </body>
        </element>
        """;

Older C# versions

You can do something like this:

var xml = "<element attr=\"content\">" 
          "  <body>" 
          "  </body>" 
          "</element>";

Nevermind the XML, this works of course with any string content.


Outside these, I'd like to second @DavidG's comment:

If you want to embed larger, more complex JSON in your app, better to put them in a resource file

Which has further advantages, as for example you could validate the json. You can have json-specific editor behavior, etc.

  • Related