6

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?

Michael Myers
  • 188,989
  • 46
  • 291
  • 292
  • Why was the title edited to include Regex? There's more then one way to do text replacement. I usually try to avoid Regex because it's slow so I don't think the title should have been edited to to include the "assumed" answer. – Crispy May 19 '09 at 21:01
  • 1
    @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.) – Michael Myers May 19 '09 at 21:58

4 Answers4

23

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
  • 10,152
  • 8
  • 68
  • 104
4

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);
Crispy
  • 5,557
  • 3
  • 30
  • 35
  • 2
    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 May 20 '09 at 19:00
  • 1
    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. – Crispy May 20 '09 at 23:40
  • @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. – Claudio Santos Jun 13 '15 at 15:49
3

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

D'Arcy Rittich
  • 167,292
  • 40
  • 290
  • 283
0

C# console Application

static void Main(string[] args)

        {
            Console.Write("Please input your comment: ");
            string str = Console.ReadLine();
            string[] str2 = str.Split(' ');
            replaceStringWithString(str2);
            Console.ReadLine();
        }
        public static void replaceStringWithString(string[] word)
        {
            string[] strArry1 = new string[] { "good", "bad", "hate" };
            string[] strArry2 = new string[] { "g**d", "b*d", "h**e" };
            for (int j = 0; j < strArry1.Count(); j++)
            {
                for (int i = 0; i < word.Count(); i++)
                {
                    if (word[i] == strArry1[j])
                    {
                        word[i] = strArry2[j];
                    }
                    Console.Write(word[i] + " ");
                }
            }
        }
demongolem
  • 9,474
  • 36
  • 90
  • 105
Coder
  • 39
  • 7