如何在C# 11中保留三重引号内的缩进?

huangapple go评论72阅读模式
英文:

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

问题

In C#,使用三重引号("""),如何保留第二组三重引号内的缩进?这里有一个示例:https://dotnetfiddle.net/ypP8Xp

编辑:这只是一个示例,我始终需要在Text2每行之前保持相同的缩进,无论内部文本如何,所以不能修改内部文本以添加更多制表符。

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
";
英文:

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
	""";

如何在C# 11中保留三重引号内的缩进?

答案1

得分: 0

以下是翻译好的代码部分:

以下代码有效 - 如果您使用空格(由句点表示),而不是制表符进行缩进:

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 => @"
....我有4个空格
....{Text2} 
";
                
        public static string Text2 => @"
....我有8个空格
............我应该有12个,为什么没有?
";
                
        public static string Expected => @"
....我有4个空格
........我有8个空格
............我有12个空格
";

    }
}
英文:

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
""";

    }
}

答案2

得分: 0

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'));
英文:

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

得分: 0

你需要理解的是,一个Here-string,或者更正式地说是Raw String Literal,并不会真正影响这里正在发生的事情。字符串插值是在解释原始字符串之后发生的。

当你有一个原始字符串时,字符串会被重新解释,以便最后一行的空白字符缩进被计算,并从其他行中减去。

所以下面这三个字符串是完全相同的,原始字符串、文本字符串和普通字符串,正如这个示例所证明的那样:

string Text1 = """
        我有8个空格
            我应该有12个,为什么没有?
    """;

string Text2 = @"    我有8个空格
        我应该有12个,为什么没有?";

string Text3 = "    我有8个空格\n        我应该有12个,为什么没有?";

所以你的代码实际上等同于:

public static string Text => $@"    我有4个空格
    {Text2}         ";

public static string Text2 => @"    我有8个空格
        我应该有12个,为什么没有?";

现在显而易见,插值字符串不会发生任何"缩进",因为这只是普通的字符串插值。它只是一个字符串嵌套在另一个字符串中的简单拼接,相当于:

public static string Text => $@"    我有4个空格
        我有8个空格
        我应该有12个,为什么没有?         ";
英文:

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?         ";

huangapple
  • 本文由 发表于 2023年7月18日 04:51:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76707992.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定