nate nate - 2 months ago 11
ASP.NET (C#) Question

How to keep spaces with escape sequences stringbuilder c#

I have a

stringbuilder
setup with escape sequences to show a ASCII like art to text. I am using ASP.NET Web Forms. Here is my code:

sb.AppendLine(@" _____ ______ ___ ___ ________ ");
sb.AppendLine(@" |\ _ \ _ \|\ \|\ \|\ ___ \ ");
sb.AppendLine(@" \ \ \\\__\ \ \ \ \\\ \ \ \_|\ \ ");
sb.AppendLine(@" \ \ \\|__| \ \ \ \\\ \ \ \ \\ \ ");
sb.AppendLine(@" \ \ \ \ \ \ \ \\\ \ \ \_\\ \ ");
sb.AppendLine(@" \ \__\ \ \__\ \_______\ \_______\ ");
sb.AppendLine(@" \|__| \|__|\|_______|\|_______| ");
sb.AppendLine(@" ");


And here is the output:

_____ ______ ___ ___ ________
|\ _ \ _ \|\ \|\ \|\ ___ \
\ \ \\\__\ \ \ \ \\\ \ \ \_|\ \
\ \ \\|__| \ \ \ \\\ \ \ \ \\ \
\ \ \ \ \ \ \ \\\ \ \ \_\\ \
\ \__\ \ \__\ \_______\ \_______\
\|__| \|__|\|_______|\|_______|


I use a textbox to display my stringbuilder
textbox1.Text = sb.ToString();
and print it on an ASP.NET page.

How can I keep my strings just as I typed them?

Answer

The output is a html webpage, and in html multiple spaces are displayed as one space.

You could solve this with css:

.myTextBox{
  white-space: pre;
}

Or using the <pre> tag if this isn't and form element (<input>, <textarea> etc):

<div>multiple    spaces     will     be    displayed    as    one space</div>
<pre>multiple    spaces     will     be    displayed    as     typed! </pre>