Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiline formatting for verbatim strings in c# (prefix with @)

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?

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?

like image 427
Brady Moritz Avatar asked Aug 24 '11 15:08

Brady Moritz


People also ask

How do you assign a multi line string to a variable in C#?

You can use @"...". Replace(Environment. NewLine,"") if you like.

How do I store multiple lines in a string?

There are three ways to create strings that span multiple lines: By using template literals. By using the + operator – the JavaScript concatenation operator. By using the \ operator – the JavaScript backslash operator and escape character.

Which characters are used for multiline strings?

Three single quotes, three double quotes, brackets, and backslash can be used to create multiline strings.

Can double quote strings span multiple lines?

A verbatim string literal consists of an @ character followed by a double-quote character, zero or more characters, and a closing double-quote character. Not only does this allow multiple lines, but it also turns off escaping.


2 Answers

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
like image 89
Cymen Avatar answered Oct 12 '22 13:10

Cymen


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:

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
like image 5
Digitrance Avatar answered Oct 12 '22 12:10

Digitrance