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

Recommended Answers

All 4 Replies

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

ok thx dude.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.