Hello. Sorry for bothering and, maybe, stupid question, but I meet the problem, trying to resolve this challenge ([Challenge project - Work with variable data in C# - Training | Microsoft Learn])
I collect user’s input and divide his query to the array with independent characteristics. Further I use string.Contains() to search every characteristic through dog descriptions. But one of indicators of problem is that for query “male” program finds female dogs, because “female” also cotains “male”. I see also string methods like Equal and CompareTo, but CompareTo returns integer, and Equal, as I understand, check only full equality between two strings, so full dogDescription string never will be equal to the “characteristic” from user’s search query.
I also tried to use different checks with Substrings, like checking, is the next symbol - whitespace, but I understood, that this is unreliable. I don’t ask for solution, but, maybe, a hint, which methods I should learn to resolve this project.
P. S. Also I checked previous guided project ([Exercise - Add dog search - Training | Microsoft Learn]), both my own and etalon “final” versions, where I should give to user function of searcch one characteristic of the dog, and even in the final, etalon version, coded by course’s developers search gives also female dogs on the query “male”. So, am I do/did something wrong at this aspect of project?
Hi @solovevandrey96 , the current implementation uses this:
if (dogDescription.Contains(dogCharacteristic))
So it just checks if the description contains the keyword. To match the keyword correctly, you can convert the description to a list, which contains all the words. Then use Contains
. So the idea looks like this:
if (dogDescription.Split(" ").Contains(dogCharacteristic))
Just use the Split()
method to split the description to a list, then it can match the word per your need. In this case, female
won’t match male
.
If you have any questions, please reply to this thread. Thanks.
Hi there. I initially had the same problem as @solovevandrey96 had. With my solution, I got a match for male and female, when using the .Contains method to search for dog characteristics. Thanks to you, @funcodingnz for your help. I tried to implement this, but had the problem, that this is used on a string, while I have the user input converted into a string array, like the challenge asked us to do. After some trial and error and consulting the final solution of the challenge provided by Microsoft, I found out how to fix it for me.
I use a for iteration to loop through the search terms. So my search routine started as follow.
if (dogDescription.Contains(searchTerms[searchTermID].Trim()))
// some more code here....
This gave me the result of matching male AND female while using the array[index] method.
After that I changed this according to the final solution provided by Microsoft in their example file. When you add a space before and after the search term, then this is fixed. The code that finally worked for me in my case was as follow.
if (dogDescription.Contains(" " + searchTerms[searchTermID].Trim() + " "))
// some more code here....
By using it like this, i only got a match for female with dog ID d1, and a match for male with dog ID d2 which is correct.
So far so good, but there is another problem now. When I use a search term like for example “kisses” (which is a part of the Personality description of Lola the dog ID d1) then this term will not be found with this code. The same goes for the final solution from Microsoft in the example files. It seems to have to do with the fact, that a point follows after kisses in the dog description. Every search term i use in the console, that is followed by a point in the description fails.
Now up to finding out, how to trim the points, or to make sure, that the description is always entered without any punctuation at the end. In any case, this challenge was very demanding for me, but I learned a lot.
Hi @MarkusEicher , glad you found the solution. But from my perspective, the following code
dogDescription.Contains(" " + searchTerms[searchTermID].Trim() + " "))
is a bit messy. I would rather use an array to match the words. e.g.
dogDescription.Split(" ").Contains(searchTerms[searchTermID].Trim())
So you don’t need to worry about the spaces.
To remove the dot from the words, just replace these special characters to ""
, e.g.
searchWord.Replace(".", "").Replace("'", "")
You can replace more per your need. You can also make a method to do this:
public static void RemoveSpecialCharacters(this string word)
{
return word.Replace(".", "").Replace("'", "").Replace(...);
}
Then you can call it on your words
word.RemoveSpecialCharacters()
I typed the code here directly without IDE so let me know if you have any errors.
Thank you very much. I appreciate your support. I will work this out later today. It makes sense to do it that way, I guess. May you have a good time and see you later.