如何替换C#中的特定单词?

时间:2021-01-15 21:05:28

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."

然后它返回“@@ man is old。@@ m is not bad。”但我希望输出为“@@ man is old。他们还不错。”

How can I do this?

我怎样才能做到这一点?

4 个解决方案

#1


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, "@@");

#2


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.

我个人试图不使用正则表达式,因为它很慢。 Regex非常适合复杂的字符串模式,但是如果字符串替换很简单并且你需要一些性能,我会试着找到一种不使用Regex的方法。

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

扔了一个测试。使用Regex和字符串方法运行一百万次替换。

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

正则表达式需要26.5秒才能完成,字符串方法需要8秒才能完成。

        //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);

#3


3  

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

s = s.Replace(“The”,“@@”);

#4


0  

C# console Application

C#console应用程序

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] + " ");
                }
            }
        }

#1


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, "@@");

#2


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.

我个人试图不使用正则表达式,因为它很慢。 Regex非常适合复杂的字符串模式,但是如果字符串替换很简单并且你需要一些性能,我会试着找到一种不使用Regex的方法。

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

扔了一个测试。使用Regex和字符串方法运行一百万次替换。

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

正则表达式需要26.5秒才能完成,字符串方法需要8秒才能完成。

        //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);

#3


3  

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

s = s.Replace(“The”,“@@”);

#4


0  

C# console Application

C#console应用程序

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] + " ");
                }
            }
        }