Problem with String.contains

For some reason when I type in the correct words that are in my "contains" statement, the else statement is displayed. This is from the question bot in the swift guide. Here's my code, do you see anything wrong with it?

struct MyQuestionAnswerer {
    func responseTo(question: String) -> String {
          let lowercaseQuestion = question.lowercased()
        
        //Herobrine Question
        if lowercaseQuestion.contains("Herobrine") && lowercaseQuestion.contains("Who"){
        return "Herobrine began as a screenshot of a distant figure posted on minecraft forums. The picture was accompanied by the story of a Minecraft player finding someone or something else strange being present. Herobrine is feared by minecrafters to this day."
        //Surprised Pikachu Question
    } else if lowercaseQuestion.contains("Surprised Pikachu") && lowercaseQuestion.contains("What") {
        return "'Surprised Pikachu’ is primarily used as a sarcastic reaction image. The meaning behind using it is to convey a sarcastic sense of shock at an outcome that was fairly obvious. 'Surprised Pikachu' is used by many people today on the internet. The origin of the meme is from a very old pokemon episode where a butterfly pokemon faints"
         //Le Monke Question
    } else if lowercaseQuestion.contains("Le Monke") || lowercaseQuestion.contains("Uh Oh stinky"){
        return "Le Monke is a photograph of an obese orangutan. Le monke was originally posted in 2016. It has recently regained popularity in 2019 for it’s famous words UH OH STINKY"
        } else {
          
      
          return "Error"
        
        }
        

        
    } //end of func
//end of struct
}

As a more concise answer, you’ve lowercased the input string, but the literal target strings you’re looking for with contains have capital letters, so they will never match. @eskimo's suggestion is complete, and good, but simply lowercasing the target strings to match the lowercased input string would also solve the problem from what I can see.

I would follow Quinn's advice, adding .diacriticInsensitive to the options, then you shouldn't need to worry about either case or accents. Don't forget that some "English" words can have accents; like café or resumé

2 Likes

Take care with .diacriticInsensitive to make sure it is actually what you want, as it destroys some minimal pairs even in English. As a user, I get very annoyed when I search for resumés (prior work experience, e.g. “two resumés”) and get hits for resumes (to continue, e.g. “it resumes”). On the other hand, I do not expect the search to know that café and cafe are the same any more than I expect it to recognize colour and color are the same. Diacritics are best handled at the same level as other types of synonyms in a “smart” search.

3 Likes

As further support, your search for “resumĂ©s” probably ought to include “rĂ©sumĂ©s” unless you’re intentionally filtering out pedants. :)

6 Likes

:wink: I saw a tweet from a developer who cautioned against using Google Translate to localise an app with a button to resume the game - translated as Curriculum Vitae :crazy_face:

3 Likes