Here is the code:

string answer = Console.ReadLine(); if (answer.IndexOf("окей")) { Console.WriteLine("ну окей, так окей"); } 

I need the code to find the word OK in the answer variable that the user enters into the console. Please correct me. What did I write wrong here?

  • 2
    if (answer.IndexOf("окей") != -1) - Alexey Shimansky
  • @ AlekseyShimansky thank you very much - smily_prg

1 answer 1

String.IndexOf returns an index, starting from zero, of the first occurrence of the value of the specified string in the given instance. You need to check if the result is equal to a value greater than -1.

It's easier to use String.Contains :

Returns a value indicating whether the specified string contains the value of the substring passed as a parameter.

C # demo code :

 string answer = Console.ReadLine(); if (answer.Contains("окей")) { Console.WriteLine("ну окей, так окей"); } 
  • one
    and if the answer is "not ok"? - tCode pm
  • @tCode: So what? The user entered "not okay", the line contains "okay", it means that it is necessary to output "well, okay, so okay" :) This is a car that you can say to her, she will do. - Wiktor Stribiżew
  • 2
    Yes, I'm so ... just boring) - tCode
  • 2
  • Thank you very much! It suits me even more than IndexOf. - smily_prg