Can you make if statement based on a string for example: you have a String named "string1" and you want to test if the person got the text right or aomethibg like that | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
0

Can you make if statement based on a string for example: you have a String named "string1" and you want to test if the person got the text right or aomethibg like that

25th Oct 2016, 9:36 PM
Kert Mänd
Kert Mänd - avatar
3 ответов
+ 1
When "==" is used with objects, it checks whether the two or more objects being compared point at the same location in memory whereas equals(), compareTo() methods can be used to check whether the user has entered the correct details(string content).
25th Oct 2016, 10:02 PM
Ousmane Diaw
+ 1
Officially, you should use the methods of System.StringComparison library and avoid using operators like "==" when comparing strings. You can find examples of all kinds of operations you may need to compare strings in MSDN guide pages here: > https://msdn.microsoft.com/en-us/library/cc165449.aspx
26th Oct 2016, 2:19 AM
Hanike T.
Hanike T. - avatar
- 3
Yes you can. A program using this would look something like this: String string1 = Console.ReadLine (); if (string1 == "correct"){ Console.WriteLine ("Good Job!"); } else { Console.WriteLine ("Wrong!"); }
25th Oct 2016, 9:45 PM
Matthew Tomlin
Matthew Tomlin - avatar