how to preserve indent inside triple quote in C# 11?

95 Views Asked by At

In C#, using triple quotes ("""), how could I preserve the inner indentation of the second triple quotes? here it is a fiddle https://dotnetfiddle.net/ypP8Xp

EDIT: This is only an example, I always need the same indent before each line in Text2, no matter the inner text, so is not an option to modify inner text with more tabs.

public static void Main(string[] args){
    Console.WriteLine(Text);
    Console.WriteLine("\n Expected:\n");
    Console.WriteLine(Expected);
}
    
public static string Text => $"""
        I have 4 spaces
        {Text2}         
    """;
        
public static string Text2 => """
        I have 8 spaces
            I should have 12, why not?
    """;
        
public static string Expected => """
        I have 4 spaces
            I have 8 spaces
                I have 12 spaces
    """;

enter image description here

3

There are 3 best solutions below

0
cflorenciav On BEST ANSWER

After some comments I realized that Text2 is "pasted" with its own indent, I thought that some already functions could be used to formatted it, so, until some other efficient answer appears, I will add the indent manually to children as I could know how many spaces I need from parents.

public static string Text => $"""
    I have 4 spaces
    {Text2.InnerIndent(4)}          
""";
            
public static string Text2 => """
    I have 8 spaces
        I should have 12, why not?
""";
            
public static string InnerIndent(this string value, int indent) =>
    string.Join($"\n{new string(' ', indent)}", value.Split('\n'));
3
bdcoder On

The following works - IF you use spaces (illustrated by periods) and NOT tabs for indenting:

using System;
namespace RawStringTester{
    public class Program{
        public static void Main(string[] args){
            Console.WriteLine(Text);
            Console.WriteLine("\n Expected:\n");
            Console.WriteLine(Expected);
        }
        
        public static string Text => $"""
....I have 4 spaces
....{Text2} 
""";
                
        public static string Text2 => """
....I have 8 spaces
............I should have 12, why not?
""";
                
        public static string Expected => """
....I have 4 spaces
........I have 8 spaces
............I have 12 spaces
""";

    }
}
0
Charlieface On

What you need to understand is that a Here-string, or more officially a Raw String Literal, has no real bearing on what is going on here. The string interpolation is happening after interpreting the Raw string.

When you have a raw string, the string is reinterpreted so that the indentation of whitespace characters on the final line is counted up, and then taken away from the other lines.

So the following three are exactly the same, Raw, Verbatim and Normal strings, as evidenced by this fiddle:

string Text1 = """
        I have 8 spaces
            I should have 12, why not?
    """;

string Text2 = @"    I have 8 spaces
        I should have 12, why not?";

string Text3 = "    I have 8 spaces\n        I should have 12, why not?";

So all that is happening is that your code is equivalent to

public static string Text => $@"    I have 4 spaces
    {Text2}         ";
        
public static string Text2 => @"    I have 8 spaces
        I should have 12, why not?";

And now it's obvious that there is no way any "indenting" of the interpolated string is going to happen, as it's normal string interpolation. It's just a straight concatenation of one string inside the other, the equivalent of

public static string Text => $@"    I have 4 spaces
        I have 8 spaces
        I should have 12, why not?         ";