hi guys,
Does anyone knows how to highlight the words that are wrong at string str2 when compared to string str1? Below is my coding. Thanks.

   string str1 = "This is an good example, i like it.";  
   string str2 = "This is a bad example, i dislike it.";  
   str1.Split(' ', ',', '.');  
   str2.Split(' ', ',', '.');  
   int i = 0, error = 0;  
   int intWordLength = str1.Split(' ', ',', '.').Length;  
   while (i < intWordLength)  
     {  
        if (!str1.Split(' ', ',', '.')[i].Equals(str2.Split(' ', ',', '.')[i]))  
           error++;  
       i++;  
   }  
    Response.Write(error.ToString());

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Hi fri, could you please use

put your code here

tags if you want to get reply from others . It maintains the formatting to make it easier to read your code.

string str1 = "This is an good example, i like it.";
string str2 = "This is a bad example, i dislike it.";
str1.Split(' ', ',', '.');
str2.Split(' ', ',', '.');
int i = 0, error = 0;
int intWordLength = str1.Split(' ', ',', '.').Length;
while (i < intWordLength)
{
if (!str1.Split(' ', ',', '.')[i].Equals(str2.Split(' ', ',', '.')[i]))
error++;
i++;
}
Response.Write(error.ToString());

Strings are an imuutable type. You dont directly change their value, you assign a new value to them. As such, their functions do not change their value. For example:

class Program
    {
        static void Main(string[] args)
        {
            string test = "This is a test";
            Console.WriteLine("Initial value: {0}", test);

            test.Replace(" ", "_");
            Console.WriteLine("Value after method: {0}", test);

            test = test.Replace(" ", "_");
            Console.WriteLine("Value after method and assignment: {0}", test);

            Console.ReadKey();
        }

    }

So when you call str2.Split it doesnt make any changes to str1. Split is a method which returns an array of strings. You need to store the array that each split method returns and compare the elements of the arrays.
Try rewriting it and let us know where you get to.

This article has been dead for over six months. Start a new discussion instead.