C
C#2y ago
jouri

❔ 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
jouri
jouriOP2y ago
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
Jimmacle
Jimmacle2y ago
for future reference, it's best to share unmodified code and put it in a code block or on a paste site $paste
MODiX
MODiX2y ago
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!
jouri
jouriOP2y ago
what's a code block
Jimmacle
Jimmacle2y ago
jouri
jouriOP2y ago
alright hold on
Jimmacle
Jimmacle2y ago
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
333fred
333fred2y ago
$code
MODiX
MODiX2y ago
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/
jouri
jouriOP2y ago
namespace console
{
internal class Program
{
public static void Main(string[] args)
{
while (true) {

Console.Write("Enter your name:");
string name = Console.ReadLine();
Console.Write("Enter your age:");
string age = Console.ReadLine();
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?");
string fail = Console.ReadLine();
if (fail == "YES")
{
continue;
}
if (fail == "NO")
{
Console.WriteLine("why would u do that bro");
Console.ReadLine();
return;
}
}
}
}
}
}
namespace console
{
internal class Program
{
public static void Main(string[] args)
{
while (true) {

Console.Write("Enter your name:");
string name = Console.ReadLine();
Console.Write("Enter your age:");
string age = Console.ReadLine();
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?");
string fail = Console.ReadLine();
if (fail == "YES")
{
continue;
}
if (fail == "NO")
{
Console.WriteLine("why would u do that bro");
Console.ReadLine();
return;
}
}
}
}
}
}
this is the full code.
Jimmacle
Jimmacle2y ago
yeah, so for example you could replace correct == "YES" with correct.Equals("yes", StringComparison.InvariantCultureIgnoreCase)
Jimmacle
Jimmacle2y ago
How to compare strings - C# Guide
Learn how to compare and order string values, with or without case, with or without culture specific ordering
jouri
jouriOP2y ago
if correct.Equals("yes", StringComparison.InvariantCultureIgnoreCase)
{
Console.WriteLine("Great :D");
Console.ReadLine();
return;
}
if correct.Equals("no", StringComparison.InvariantCultureIgnoreCase)
{
Console.Write("Do you want to try again?");
string fail = Console.ReadLine();
if correct.Equals("yes", StringComparison.InvariantCultureIgnoreCase)
{
continue;
}
if (fail == "NO")
{
Console.WriteLine("why would u do that bro");
Console.ReadLine();
return;
if correct.Equals("yes", StringComparison.InvariantCultureIgnoreCase)
{
Console.WriteLine("Great :D");
Console.ReadLine();
return;
}
if correct.Equals("no", StringComparison.InvariantCultureIgnoreCase)
{
Console.Write("Do you want to try again?");
string fail = Console.ReadLine();
if correct.Equals("yes", StringComparison.InvariantCultureIgnoreCase)
{
continue;
}
if (fail == "NO")
{
Console.WriteLine("why would u do that bro");
Console.ReadLine();
return;
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
Jimmacle
Jimmacle2y ago
you have some syntax errors, the condition of an if statement has to be in parentheses for example this is right if (fail == "NO")
jouri
jouriOP2y ago
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.
Accord
Accord2y ago
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.
Want results from more Discord servers?
Add your server