简体   繁体   中英

Comparing strings in a List of reference types

I have Implemented code in C# that compares 2 strings character by character and returns the percentage difference between the 2 strings. Below is the code.

public static double percentage(string a, string b)
    {  
        double percent;

        if (a == b) //Same string, no iteration needed.
            percent = 100;
        if ((a.Length == 0) || (b.Length == 0)) //One is empty, second is not
        {
            percent = 0;
        }
        double maxLen = a.Length > b.Length ? a.Length : b.Length;
        int minLen = a.Length < b.Length ? a.Length : b.Length;
        int sameCharAtIndex = 0;
        for (int i = 0; i < minLen; i++) //Compare char by char
        {
            if (a[i] == b[i])
            {
                sameCharAtIndex++;
            }
        }
        percent = sameCharAtIndex / maxLen * 100;
        Console.WriteLine("Difference {0}", percent.ToString());
        return percent;
      }  

I have fetched data from 2 tables in my database and stored the data in 2 lists as below

                //ListOfPerson
                while (reader.Read())
                {
                    //var person = new Person();
                    person.ID = Convert.ToInt32(reader["ID"]);
                    person.firstName = reader["FirstName"].ToString();
                    person.middleName = reader["MiddleName"].ToString();
                    person.lastName = reader["LastName"].ToString();

                    ListOfPerson.Add(person);
                    Console.WriteLine("{0} {1} {2} {3}", person.ID, person.firstName, person.middleName, person.lastName);
                } 


                //ListOfEmployee
                while (reader1.Read())
                {
                    //var employee = new Employee();

                    employee.firstName = reader1["FirstName"].ToString();
                    employee.lastName = reader1["LastName"].ToString();

                    ListOfEmployee.Add(employee);

                    Console.WriteLine("{0} {1}", employee.firstName, employee.lastName);
                }

I want to compare person.firstName (in the entire ListOfPerson)and employee.firstName (in the entire ListOfEmployee) character by character and get the percentage. I have tried doing this :

               foreach (var p in ListOfPerson)
                {
                    for (int i = 0; i < ListOfEmployee.Count(); i++)
                    {
                        clsCompare.percentage(p.firstName, ListOfEmployee[i].firstName);
                    }
                }

but its only looping through the last name in ListOfPerson and ListOfEmployee. How can i achieve this? looping through all the names comparing character by character in the 2 Lists.

The code is incorrect. Add return statements like this:

if (a == b) //Same string, no iteration needed.
    return percent = 100;
if ((a.Length == 0) || (b.Length == 0)) //One is empty, second is not
{
    return percent = 0;
}

The new statements are required. You have marked them as comments. Without the new statements, you are not allocating any new memory for the new values. You are saving all the values in the same object thus overwriting the previous values.

You also need to do something with the returned value. You are just calling the function. You should probably store that value in a variable or print it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM