c#中的逐字符串的多行格式(带@的前缀)

时间:2022-12-25 15:53:37

I love using the @"strings" in c#, especially when I have a lot of multi-line text. The only annoyance is that my code formatting goes to doodie when doing this, because the second and greater lines are pushed fully to the left instead of using the indentation of my beautifully formatted code. I know this is by design, but is there some option/hack way of allowing these lines to be indented, without adding the actual tabs/spaces to the output?

我喜欢在c#中使用@“字符串”,特别是当我有很多多行文字时。唯一令人烦恼的是我的代码格式在做这个时会变成doodie,因为第二行和更多行被完全推到左边而不是使用我格式精美的代码的缩进。我知道这是设计的,但是有一些选项/ hack方法允许这些行缩进,而不是在输出中添加实际的制表符/空格吗?

adding example:

添加示例:

        var MyString = @" this is 
a multi-line string
in c#.";

My variable declaration is indented to the "correct" depth, but the second and further lines in the string get pushed to the left margin- so the code is kinda ugly. You could add tabs to the start of line 2 and 3, but the string itself would then contain those tabs... make sense?

我的变量声明缩进到“正确”深度,但字符串中的第二行和更多行被推到左边缘 - 所以代码有点难看。您可以在第2行和第3行的开头添加制表符,但字符串本身将包含这些制表符......有意义吗?

3 个解决方案

#1


9  

How about a string extension? Update: I reread your question and I hope there is a better answer. This is something that bugs me too and having to solve it as below is frustrating but on the plus side it does work.

字符串扩展怎么样?更新:我重读了你的问题,我希望有更好的答案。这也是让我烦恼的事情,并且必须解决它,因为下面是令人沮丧的,但从好的方面来说它确实有效。

using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
    public static class StringExtensions
    {
        public static string StripLeadingWhitespace(this string s)
        {
            Regex r = new Regex(@"^\s+", RegexOptions.Multiline);
            return r.Replace(s, string.Empty);
        }
    }
}

And an example console program:

一个示例控制台程序:

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string x = @"This is a test
                of the emergency
                broadcasting system.";

            Console.WriteLine(x);

            Console.WriteLine();
            Console.WriteLine("---");
            Console.WriteLine();

            Console.WriteLine(x.StripLeadingWhitespace());

            Console.ReadKey();
        }
    }
}

And the output:

并输出:

This is a test
                of the emergency
                broadcasting system.

---

This is a test
of the emergency
broadcasting system.

And a cleaner way to use it if you decide to go this route:

如果您决定采用这条路线,可以使用它更简洁的方法:

string x = @"This is a test
    of the emergency
    broadcasting system.".StripLeadingWhitespace();
// consider renaming extension to say TrimIndent() or similar if used this way

#2


5  

Cymen has given the right solution. I use a similar approach as derived from Scala's stripMargin() method. Here's what my extension method looks like:

Cymen给出了正确的解决方案。我使用类似于Scala的stripMargin()方法的方法。这是我的扩展方法的样子:

public static string StripMargin(this string s)
{
    return Regex.Replace(s, @"[ \t]+\|", string.Empty);
}

Usage:

用法:

var mystring = @"
        |SELECT 
        |    *
        |FROM
        |    SomeTable
        |WHERE
        |    SomeColumn IS NOT NULL"
    .StripMargin();

Result:

结果:

SELECT 
    *
FROM
    SomeTable
WHERE
    SomeColumn IS NOT NULL

#3


2  

I can't think of an answer that would completely satisfy your question, however you could write a function that strips leading spaces from lines of text contained in a string and call it on each creation of such a string.

我无法想到一个完全满足你的问题的答案,但是你可以编写一个函数来从字符串中包含的文本行中去除前导空格,并在每次创建这样的字符串时调用它。

var myString = TrimLeadingSpacesOfLines(@" this is a 
    a multi-line string
    in c#.");

Yes it is a hack, but you specified your acceptance of a hack in your question.

是的,这是一个黑客,但你在你的问题中指明了你接受黑客攻击。

#1


9  

How about a string extension? Update: I reread your question and I hope there is a better answer. This is something that bugs me too and having to solve it as below is frustrating but on the plus side it does work.

字符串扩展怎么样?更新:我重读了你的问题,我希望有更好的答案。这也是让我烦恼的事情,并且必须解决它,因为下面是令人沮丧的,但从好的方面来说它确实有效。

using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
    public static class StringExtensions
    {
        public static string StripLeadingWhitespace(this string s)
        {
            Regex r = new Regex(@"^\s+", RegexOptions.Multiline);
            return r.Replace(s, string.Empty);
        }
    }
}

And an example console program:

一个示例控制台程序:

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string x = @"This is a test
                of the emergency
                broadcasting system.";

            Console.WriteLine(x);

            Console.WriteLine();
            Console.WriteLine("---");
            Console.WriteLine();

            Console.WriteLine(x.StripLeadingWhitespace());

            Console.ReadKey();
        }
    }
}

And the output:

并输出:

This is a test
                of the emergency
                broadcasting system.

---

This is a test
of the emergency
broadcasting system.

And a cleaner way to use it if you decide to go this route:

如果您决定采用这条路线,可以使用它更简洁的方法:

string x = @"This is a test
    of the emergency
    broadcasting system.".StripLeadingWhitespace();
// consider renaming extension to say TrimIndent() or similar if used this way

#2


5  

Cymen has given the right solution. I use a similar approach as derived from Scala's stripMargin() method. Here's what my extension method looks like:

Cymen给出了正确的解决方案。我使用类似于Scala的stripMargin()方法的方法。这是我的扩展方法的样子:

public static string StripMargin(this string s)
{
    return Regex.Replace(s, @"[ \t]+\|", string.Empty);
}

Usage:

用法:

var mystring = @"
        |SELECT 
        |    *
        |FROM
        |    SomeTable
        |WHERE
        |    SomeColumn IS NOT NULL"
    .StripMargin();

Result:

结果:

SELECT 
    *
FROM
    SomeTable
WHERE
    SomeColumn IS NOT NULL

#3


2  

I can't think of an answer that would completely satisfy your question, however you could write a function that strips leading spaces from lines of text contained in a string and call it on each creation of such a string.

我无法想到一个完全满足你的问题的答案,但是你可以编写一个函数来从字符串中包含的文本行中去除前导空格,并在每次创建这样的字符串时调用它。

var myString = TrimLeadingSpacesOfLines(@" this is a 
    a multi-line string
    in c#.");

Yes it is a hack, but you specified your acceptance of a hack in your question.

是的,这是一个黑客,但你在你的问题中指明了你接受黑客攻击。