❔ if you use a string command in an if command, can you make it case insensitive
so i'm really new to this, and decide to venture a bit away from the tutorial and I want to make it work.
So what I have currently is just a basic
Console.Write("Enter your name:");
string name = Console.ReadLine();
etc.
but I'm stuck at a part where I did
Console.Write("Hello " + *name *+ " you are " + *age* + ". is this correct:");
string *correct* = Console.ReadLine();
if (*correct *== "YES")
{
Console.WriteLine("Great :D");
Console.ReadLine();
return;
}
if (*correct *== "NO")
{
Console.Write("Do you want to try again?");
The code works, but it's case and space sensitive, which makes it really annoying to answer the question. Is there a fix for this?16 Replies
I put asterisks around the string names in this message in an attempt at making it easier to read lol
just incase it causes confusion
for future reference, it's best to share unmodified code and put it in a code block or on a paste site $paste
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
what's a code block
alright hold on
but what you're looking for is the string's
Compare
method
it takes arguments that can let you set it to be case insensitive
actually, Equals
can do it too$code
To post C# code type the following:
```cs
// code here
```
Get an example by typing
$codegif
in chat
If your code is too long, post it to: https://paste.mod.gg/
this is the full code.
yeah, so for example you could replace
correct == "YES"
with correct.Equals("yes", StringComparison.InvariantCultureIgnoreCase)
How to compare strings - C# Guide
Learn how to compare and order string values, with or without case, with or without culture specific ordering
so now I have this, but it doesn't work.
Also I think I might be far too new to understand most of this, but it helped a bit so thanks. :D
you have some syntax errors, the condition of an if statement has to be in parentheses
for example this is right
if (fail == "NO")
ah alright, I thought the parentheses were pushed back a bit because of the
.Equals
. my bad lol
Thank you so much for your help @jimmacle I appreciate it a lot.Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.