C Sharp replace tab characters

From Regex Regular Expression Encyclopedia

Jump to: navigation, search

This wiki is for replacing tab characters in a string with a different character using C#. In this recipe, we use a pipe (|) to replace the tab.

[edit] code

using System;
using System.IO;
using System.Text.RegularExpressions;
public class Recipe
{
    private static Regex _Regex = new Regex( @"\t" );
    public void Run(string fileName)
    {
        String line;
        String newLine;
        using (StreamReader sr = new StreamReader(fileName))
        {
            while(null != (line = sr.ReadLine()))
            {
                newLine = _Regex.Replace(line, @"|");
                Console.WriteLine("New string is: '{0}', original was: '{1}'",
                    newLine,
                    line));
            }
        }
    }
    public static void Main( string[] args )
    {
        Recipe r = new Recipe();
        r.Run(args[0]);
    }
}

[edit] How It Works

Breaking the recipe down yields simply the following:

Regular Expression Description
\t is a tab, replaced by . . .
| a pipe character.

By default, the regular expression object in the .NET Framework replaces each occurrence of the text that matches the search regex. However, the VBScript and JavaScript regular expres- sion objects work differently because an option must be specified to replace each match. You can find more about these options in the “Syntax Overview” section of this book.

[edit] Variations

Since this is such a simple recipe, it has an extensive number of variations. You could replace the character class representing a tab with other character classes, especially the \s character class. The JavaScript variation /\s/g would replace each instance of whitespace with |.

One variation on the previous recipe is to use a qualifier after the character class to replace more than one instance of a tab at once. For instance, if you want to replace two tabs, you could use something such as /\t{2}/g in the JavaScript recipe.

Personal tools