Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Char/String comparison

Tags:

c#

I'm trying to have a suggestion feature for the search function in my program eg I type janw doe in the search section and it will output NO MATCH - did you mean jane doe? I'm not sure what the problem is, maybe something to do with char/string comparison..I've tried comparing both variables as type char eg char temp -->temp.Contains ...etc but an error appears (char does not contain a definition for Contains). Would love any help on this! 8)

if (found == false)
        {
            Console.WriteLine("\n\nMATCH NOT FOUND");
            int charMatch = 0, charCount = 0;
            string[] checkArray = new string[26];
            //construction site  /////////////////////////////////////////////////////////////////////////////////////////////////////////////
            for (int controlLoop = 0; controlLoop < contPeople.Length; controlLoop++)
            {
                foreach (char i in userContChange)
                {
                    charCount = charCount + 1;
                }
                for (int i = 0; i < userContChange.Length; )
                {
                    string temp = contPeople[controlLoop].name;
                    string check=Convert.ToString(userContChange[i]);
                    if (temp.Contains(check))
                    {
                        charMatch = charMatch + 1;
                    }
                }
                int half = charCount / 2;
                if (charMatch >= half)
                {
                    checkArray[controlLoop] = contPeople[controlLoop].name;
                }
            }
///////////////////////////////////////////////////////////////////////////////////////////////////////////
                Console.WriteLine("Did you mean: ");
                for (int a = 0; a < checkArray.Length; a++)
                {
                    Console.WriteLine(checkArray[a]);
                }
///////////////////////////////////////////////////////////////////////////////////////////////////
like image 202
Quigg15405 Avatar asked Oct 16 '12 01:10

Quigg15405


1 Answers

A string is made up of many characters. A character is a primitive, likewise, it doesn't "contain" any other items. A string is basically an array of characters.

For comparing string and characters:

char a = 'A';
String alan = "Alan";
Debug.Assert(alan[0] == a);

Or if you have a single digit string.. I suppose

char a = 'A';
String alan = "A";
Debug.Assert(alan == a.ToString());

All of these asserts are true

But, the main reason I wanted to comment on your question, is to suggest an alternative approach for suggesting "Did you mean?". There's an algorithm called Levenshtein Distance which calculates the "number of single character edits" required to convert one string to another. It can be used as a measure of how close two strings are. You may want to look into how this algorithm works because it could help you.

Here's an applet that I found which demonstrates: Approximate String Matching with k-differences

Also the wikipedia link Levenshtein distance

like image 115
Alan Avatar answered Sep 29 '22 11:09

Alan