[Solved] If Statement not Working right [closed]


Assuming the statement you’re testing is actually ‘How is the weather’ then your if statement is working as expected. Your checks are seeing if the statement contains ‘weather’ and ‘what’ OR contains the word ‘how’ (note the lower case).

As your phrase doesn’t contain the word ‘what’ the first check (for the words ‘weather’ AND ‘what’) will be false. Also, as the word ‘How’ starts with a capital ‘H’ it won’t match against ‘how’, so will also return false and therefore not enter the if statement.

If you want your search to be case insensitive then you will need to consider the language as well, as words all in upper case in some languages mean different things to the same word in all lower case. Here’s a similar question and answer, accepted answer detailed below for completeness:

To test if the string paragraph contains the string word (thanks
@QuarterMeister) culture.CompareInfo.IndexOf(paragraph, word,
CompareOptions.IgnoreCase) >= 0

Where culture is the instance of CultureInfo describing the language
that the text is written in.

This solution is transparent about the definition of
case-insensitivity, which is language dependent. For example, the
English language uses the characters I and i for the upper and lower
case versions of the ninth letter, whereas the Turkish language uses
these characters for the eleventh and twelfth letters of its 29
letter-long alphabet. The Turkish upper case version of ‘i’ is the
unfamiliar character ‘İ’.

Thus the strings tin and TIN are the same word in English, but
different words in Turkish. As I understand, one means ‘spirit’ and
the other is an onomatopoeia word. (Turks, please correct me if I’m
wrong, or suggest a better example)

To summarise, you can only answer the question ‘are these two strings
the same but in different cases’ if you know what language the text is
in. If you don’t know, you’ll have to take a punt. Given English’s
hegemony in software, you should probably resort to
CultureInfo.InvariantCulture, because it’ll be wrong in familiar ways.

1

solved If Statement not Working right [closed]