How can I replace a specific word in C#?

11,316

Solution 1

Here's how you'd use a regex, which would handle any word boundaries:

Regex r = new Regex(@"\bThe\b");
s = r.Replace(s, "@@");

Solution 2

I made a comment above asking why the title was changed to assume Regex was to be used.

I personally try to not use Regex because it's slow. Regex is great for complex string patterns, but if string replacements are simple and you need some performance out of it, I'll try and find a way without using Regex.

Threw together a test. Running a million replacments with Regex and string methods.

Regex took 26.5 seconds to complete, string methods took 8 seconds to complete.

        //Using Regex. 
        Regex r = new Regex(@"\b[Tt]he\b");

        System.Diagnostics.Stopwatch stp = System.Diagnostics.Stopwatch.StartNew();

        for (int i = 0; i < 1000000; i++)
        {
            string str = "The man is old. The is the Good. Them is the bad.";
            str = r.Replace(str, "@@");
        }

        stp.Stop();
        Console.WriteLine(stp.Elapsed);

        //Using String Methods.
        stp = System.Diagnostics.Stopwatch.StartNew();

        for (int i = 0; i < 1000000; i++)
        {
            string str = "The man is old. The is the Good. Them is the bad.";

            //Remove the The if the stirng starts with The.
            if (str.StartsWith("The "))
            {
                str = str.Remove(0, "The ".Length);
                str = str.Insert(0, "@@ ");
            }

            //Remove references The and the.  We can probably 
            //assume a sentence will not end in the.
            str = str.Replace(" The ", " @@ ");
            str = str.Replace(" the ", " @@ ");
        }

        stp.Stop();
        Console.WriteLine(stp.Elapsed);

Solution 3

s = s.Replace("The ","@@ ");

Share:
11,316

Related videos on Youtube

palantus
Author by

palantus

ASP.NET MVC and WPF by day, Java and C++ by night. Maybe someday I'll try one of the cool languages. If I've edited one of your posts for grammar, you can probably find my reasoning somewhere on English Language &amp; Usage. Common reasons are: Its vs. it's I in lowercase affect vs. effect

Updated on April 14, 2022

Comments

  • palantus
    palantus about 2 years

    Consider the following example.

    string s = "The man is old. Them is not bad.";
    

    If I use

    s = s.Replace("The", "@@");
    

    Then it returns "@@ man is old. @@m is not bad."
    But I want the output to be "@@ man is old. Them is not bad."

    How can I do this?

    • palantus
      palantus almost 15 years
      @Chris Persichetti: That's fair enough; I've removed "regex" from the title. (I had added it based on the tags, but "regex" apparently wasn't one of the original tags anyway.)
  • JeffH
    JeffH almost 15 years
    For the given data your more verbose solution works. But besides being less concise than using an (admittedly slow) regex, your code will fail or require updating if the OP wants to use it in a more general sense, whereas a regex can be written to find word breaks which will handle separators like punctuation marks instead of only spaces. See my comment on @auujay's post for specifics.
  • Crispy
    Crispy almost 15 years
    I know it's not a generic solution. Regex solution is a better solution for safety for general words. I was mostly trying to point out that regex is not always the solution just depends on what is needed exactly. I try and watch questions like this to pick up on tips for text replacement as my project does a lot of text replacement and I'm always looking for faster ways to do it. So I was disappointed when the title got changed to Regex because I don't want a regex soluiton for my project, so that's why I posted this answer.
  • Claudio Santos
    Claudio Santos almost 9 years
    @Crispy , you can't look only on performance times, software isn't only about run fast, if you don't bother with Legibility, Manutenibility you will create an faster software but very expansive to keep and to evolute.