-5

I need some help with some operators in the If statement...

I am running a code like this

if (statement.Contains("weather") &&
    (
        statement.Contains("what") || statement.Contains("how")
    )
   )
{
    statement = ("It's " + Weather.Get_Weather("condition") + " outside");
}

but it's not working... can any one help me with this, because I cannot find any mistake in this code and I don't even have that much experience with this type of operators like || and && because I can use If Statements inside If Statements.

And I want the statement to have Weather and What or How so I can confirm It's a Question, Or the user is asking for the weather... You can Also gimme more ideas on this...

'how is the weather' this is not going through the If statement it contains How and weather that's enough...

I am Really Sorry for my Question, It was My Problem I did not know that Contains() is Case-Sensitive...

Raja Bilal
  • 87
  • 1
  • 12
  • 1
    What is Wheater for a class? What doesn't work? Give us more information to help you! – Mafii Mar 17 '16 at 13:18
  • 1
    Your statement looks right, what exactly is the problem? – FKutsche Mar 17 '16 at 13:19
  • Show us, what actually happen to statement variable before if statement, maybe problem lies there – Arkadiusz K Mar 17 '16 at 13:19
  • 1
    What is the value of `statement` that isn't working? Maybe you need case insensitive search? – juharr Mar 17 '16 at 13:19
  • Define 'it's not working'. Are you getting an error? Is the wrong statement being shown? What? – sr28 Mar 17 '16 at 13:20
  • 1
    Also, try to use `ToLower().Contains("weather")` – Valentin Mar 17 '16 at 13:23
  • Updated `'how is the weather' this is not going through the If statement it contains How and weather that's enough...` – Raja Bilal Mar 17 '16 at 13:26
  • 'how is the weather' this is not going through the If statement it contains How and weather that's enough... -> Is your test supposed to be case insensitive? – vc 74 Mar 17 '16 at 13:26
  • @vc74 Nope it should not be – Raja Bilal Mar 17 '16 at 13:27
  • @RajaBilal Then 'how is the weather'.Contains('How') is going to return false – vc 74 Mar 17 '16 at 13:27
  • It is going through the if statement, and it sets the `statement`. How about you change it to `Console.WriteLine` and see – libertylocked Mar 17 '16 at 13:29
  • I tested, and it works. Try apply a lowercase function on your 'statement' string. – Neyoh Mar 17 '16 at 13:30
  • What is the exact statement? You say it's 'how is the weather', but then say it contains 'How' and 'weather', which should be enough. The statement does NOT contain 'How', it contains 'how'. As for 'weather' this will only work if the statement also contains 'what', which it doesn't. Smells like a case sensitivity issue to me... – sr28 Mar 17 '16 at 13:33

1 Answers1

3

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.

Community
  • 1
  • 1
sr28
  • 4,728
  • 5
  • 36
  • 67
  • I was doing it wrong I changed my use of this If statement I mean it was first just `if (statement.Contains("weather"))` but then I added these What and How but forgot to change the variable `statement` you should also have noticed it xD, But it is okay I figured it out... – Raja Bilal Mar 17 '16 at 13:53