Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# How to be able to put "text" like TeXT and it still runs the If

I have a book for C# (CSharp) I only know a little bit but I am doing Console.Writeline stuff

I have this simple code here

Console.WriteLine("Please enter your name");

string name = Console.ReadLine();
if (name.Contains("no"))
{
    Console.WriteLine("\nFine, don't put your name in");
}
else
{
    Console.WriteLine("\nHello, " + name);
}

At the If part If you put "no" obviously it runs ("\nFine, don't put your name in") but if you put "no" as "No" "NO" "nO" it doesn't is there a code like name.Contains where it doesn't matter how you put the text it runs it anyway

Like SmallBasic Text.ConvertToLowerCase would convert your text to lowercase and then Run the IF

Thanks!

like image 901
Harrison Howard Avatar asked Nov 29 '22 00:11

Harrison Howard


2 Answers

You should change Contains to Equals, if not a name like Noel will output "Fine don't put your name in"

if(name.Equals("no", StringComparison.CurrentCultureIgnoreCase))
{

}
like image 162
Jaime Macias Avatar answered Dec 04 '22 14:12

Jaime Macias


You can convert the input to lower case characters:

if (name.ToLower().Contains("no"))

But why don't you like names like "Tino" or "Nora"? Better compare the whole string instead of just checking if it contains "no":

if (name.Equals("no", StringComparison.InvariantCultureIgnoreCase))
like image 38
René Vogt Avatar answered Dec 04 '22 14:12

René Vogt