views:

500

answers:

3

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?

+3  A: 

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

RedFilter
+15  A: 

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

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

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);
Chris Persichetti
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.
JeffH
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.
Chris Persichetti