title: Acting on the indentation of a c# multiline string
body: <p>I want to write some Html from c# (html is an example, this might be other languages..)</p>
  
  <p>For example:</p>
  
  <pre><code>    string div = @"&lt;div class=""className""&gt;
                        &lt;span&gt;Mon text&lt;/span&gt;
                     &lt;/div&gt;";
  </code></pre>
  
  <p>will produce:</p>
  
  <pre><code>&lt;div class="className"&gt;
              &lt;span&gt;Mon text&lt;/span&gt;
           &lt;/div&gt;
  </code></pre>
  
  <p>that's not very cool from the Html point of view...</p>
  
  <p>The only way to have a correct HTML indentation will be to indent the C# code like this :</p>
  
  <pre><code>            string div = @"&lt;div class=""className""&gt;
      &lt;span&gt;Mon text&lt;/span&gt;
  &lt;/div&gt;";
  </code></pre>
  
  <p>We get the correctly indented Html: </p>
  
  <pre><code>&lt;div class="className"&gt;
      &lt;span&gt;Mon text&lt;/span&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>But indenting the C# like this really broke the readability of the code...</p>
  
  <p>Is there a way to act on the indentation in the C# language ? </p>
  
  <p>If not, does someone have a tip better than :</p>
  
  <pre><code>string div = "&lt;div class=\"className\"&gt;" + Environment.NewLine +
               "  &lt;span&gt;Mon text&lt;/span&gt;" + Environment.NewLine +
               "&lt;/div&gt;";
  </code></pre>
  
  <p>and better than </p>
  
  <pre><code>var sbDiv = new StringBuilder();
  sbDiv.AppendLine("&lt;div class=\"className\"&gt;");
  sbDiv.AppendLine("    &lt;span&gt;Mon text&lt;/span&gt;");
  sbDiv.AppendLine("&lt;/div&gt;");
  </code></pre>
  
  <hr>
  
  <h2>What i use as a solution:</h2>
  
  <p>Greats thanks to @Yotam for its answer.</p>
  
  <p>I write a little extension to make the alignment "dynamic" :</p>
  
  <pre><code>    /// &lt;summary&gt;
      /// Align a multiline string from the indentation of its first line
      /// &lt;/summary&gt;
      /// &lt;remarks&gt;The &lt;/remarks&gt;
      /// &lt;param name="source"&gt;The string to align&lt;/param&gt;
      /// &lt;returns&gt;&lt;/returns&gt;
      public static string AlignFromFirstLine(this string source)
      {
          if (String.IsNullOrEmpty(source)) {
              return source;
          }
  
          if (!source.StartsWith(Environment.NewLine)) {
              throw new FormatException("String must start with a NewLine character.");
          }
  
          int indentationSize = source.Skip(Environment.NewLine.Length)
                                  .TakeWhile(Char.IsWhiteSpace)
                                  .Count();
  
          string indentationStr = new string(' ', indentationSize);
          return source.TrimStart().Replace($"\n{indentationStr}", "\n");
      }
  </code></pre>
  
  <p>Then i can use it like that :</p>
  
  <pre><code>private string GetHtml(string className)
  {
      return $@"
              &lt;div class=""{className}""&gt;
                  &lt;span&gt;Texte&lt;/span&gt;
              &lt;/div&gt;".AlignFromFirstLine();
  }
  </code></pre>
  
  <p>That return the correct html :</p>
  
  <pre><code>&lt;div class="myClassName"&gt;
      &lt;span&gt;Texte&lt;/span&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>One limitation is that it will only work with space indentation...</p>
  
  <p>Any improvement will be welcome !</p>