1

I'm working on formatting C# code in HTML. I'm trying to replace tabs/indents with 4 spaces.

Here's an example.

protected void Page_Load(object sender, EventArgs e)
{
    Response.Write("Hello World");
}

I need to replace the tab before Response.Write with 4 spaces.

I tried things like ^\t, with different variations, tried ^\s\s\s\s. I thought this would be simple, but nothing I've tried seemed match the tabs.

What am I doing wrong?

Thank you!

Edit

I copy directly from VS into TextBox1.

enter image description here

As you can see, there are no actual tabs (\t) in the TextBox value, which is the root of the problem. As noted in my comments, the spaces with ^ did work (for the first line only).

So my final regex will look like... this: "\s\s\s\s".

4
  • Try \n\t replaced with \n or check you have Multiline mode on in your Regex Commented Jul 29, 2012 at 21:46
  • What should happen with tabs in other places in the code? Left unchanged? Commented Jul 29, 2012 at 21:46
  • 2
    Is the tab always at the beginning of the string? Commented Jul 29, 2012 at 21:47
  • Yes - the tab should always be at the beginning (for now anyway), but we can forget that for a moment - plain "\t" doesn't work either. Commented Jul 29, 2012 at 21:50

2 Answers 2

4

This should do what you want:

Regex regex = new Regex(@"^\t+", RegexOptions.Multiline);
s = regex.Replace(s, m => new string(' ', 4 * m.Value.Length));

See it online: ideone


Update

Here's a version for ASP.NET Web Forms that runs in Visual Web Developer 2010 Express:

Default.aspx

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"></asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
    <asp:TextBox ID="TextBox1" runat="server" Height="99px" Width="500px" TextMode="MultiLine"></asp:TextBox>
</asp:Content>

Default.aspx.cs

using System;
using System.Text.RegularExpressions;

namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Button1_Click(object sender, EventArgs e)
        {
            string code = "protected void Page_Load(object sender, EventArgs e)\n{\n\tResponse.Write(\"Hello World\");\n}";
            Regex regex = new Regex(@"^\t+", RegexOptions.Multiline);
            TextBox1.Text = regex.Replace(code, m => new string('*', 4 * m.Value.Length));
        }
    }
}

Result after clicking button:

protected void Page_Load(object sender, EventArgs e)
{
****Response.Write("Hello World");
}

The asterisks are there only to make it easy to see that the tabs have been replaced correctly with spaces. Change the '*' to ' ' to get spaces instead of asterisks.

Sign up to request clarification or add additional context in comments.

18 Comments

@Rivka: Probably your compiler or editor settings. It does work, but note that it's using "*" instead of " " (so you can see the difference).
I'm copying/pasting straight from VS 2012 - doesn't work. I did notice in debugging mode, the value is " protected void Page_Load(object sender, EventArgs e)\r\n {\r\n Response.Write(\"Hello World\");\r\n }" (with 4 white spaces after the "\r\n"). When I substitute "\s\s\s\s" instead of "\t" it works for the first line.
@Rivka: Probably an editor misconfiguration or "helpful" plugin (do you use Resharper?) interfering with the copy+paste. Try reinstalling Visual Studio and it should work. I have a clean VS here and it works.
Reinstall VS? Seriously? No, I don't use Resharper. Edit: I'm actually using VWD 2010 - could that be it? When you copy you get "\t" in place of the tabs?
@Rivka: You might find it easier to understand if you enable "show whitespace" in Visual Studio. The keyboard shortcut chord is "Ctrl+R Ctr+W". Each space character shows up as a dot. Each tab character shows up as an arrow. Notice that (depending on your settings) when you press the Tab key either tabs or spaces might be inserted. I suspect this might be why you are confused. I nearly always have "show whitespace" enabled (except when pair programming because it drives my colleagues crazy looking at all those dots).
|
0
string text = "\tHello World";
string replacedTabWith4Spaces = text.Replace("\t", "    ");

1 Comment

This worked with the static "\t" but doesn't help in my case.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.