4

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!

Moshe D
  • 768
  • 1
  • 4
  • 13
Harrison Howard
  • 345
  • 3
  • 13

8 Answers8

13

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))
{

}
Jaime Macias
  • 857
  • 6
  • 11
7

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))
René Vogt
  • 43,056
  • 14
  • 77
  • 99
2

Just use name.ToLower() method which will make the text all lower case. Therefore when you make your comparison to "no" or "yes" you don't have to worry about the case sensitivity of the input.

Dave S
  • 973
  • 9
  • 17
1

Use ToLower()

    string name = Console.ReadLine();
    if (name.ToLower().Contains("no"))
    {
        Console.WriteLine("\nFine, don't put your name in");
    }
    else
    {
        Console.WriteLine("\nHello, " + name);
    }
Tormund Giantsbane
  • 355
  • 1
  • 4
  • 12
1

I will answer the original question:

is there a code like name.Contains where it doesn't matter how you put the text it runs it anyway

Yes:

bool contains = name.IndexOf("no", StringComparison.OrdinalIgnoreCase) >= 0;

See more information and a case-insensitive StringExtension here: Case insensitive 'Contains(string)'

However, as others have pointed out, Contains() will give you many false positives. You should use .Equals() instead.

Community
  • 1
  • 1
Forklift
  • 949
  • 8
  • 20
1

Use Equals method to compare your value instead of checking for Contains. You have three variations to choose the equality from while ignoring cases, which are current culture case ignore, invariant culture case ignore and ordinal case ignore.

Your problem does not seem to be culture specific so you can stick with StringComparison.CurrentCultureIgnoreCase. In the mean time you can learn differences of using it here

Bibek Adhikari
  • 181
  • 1
  • 12
0

The easiest way is to do something like this:

if (name.ToUpper().Contains("NO"))
joe_coolish
  • 7,201
  • 13
  • 64
  • 111
  • Not using `ToLower` is more of an issue with languages where doing `ToLower` and then `ToUpper` gives a different result. In this case since you not only know the language, but the word you are comparing to, either would be fine. Either way you shouldn't use `Contains` at all. – juharr Feb 13 '17 at 17:14
0

If you use .Contains("no") it would trigger if someone's name contains "no"

You can use this:

if(name.ToLower().Equals("no"))
{
    Console.WriteLine("\nFine, don't put your name in");
}
ghostwar 88
  • 547
  • 1
  • 6
  • 17