Hello,
Really easy question for everyone,
what the the code to have the user input none case sensative example: user can input, yes YES yEs and still equal to whatever in a if and else statement?
Thank you
Hello,
Really easy question for everyone,
what the the code to have the user input none case sensative example: user can input, yes YES yEs and still equal to whatever in a if and else statement?
Thank you

at last! someone to help
you can use this function "ToLower()"
this code will return true :
}Code:bool compare() { if("YEs".ToLower()=="yes".ToLower())return true; else return false; }
use the .ToUpper() method and compare with YES
dang! Amr beat me to it!
CodeCall Blog | CodeCall Wiki | Shareware
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

lol, youve helped lots of people including me...let me share the burden![]()
Hey, I'm glad people are helping each other. It's a GOOD thing.
CodeCall Blog | CodeCall Wiki | Shareware
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog
Not saying that doesn't work but I've never seen it done like that:
I'm guessing "yes".ToLower() returns a string but I though the proper method was to use the String.Compare function? I could be wrong:Code:if("YEs".ToLower()=="yes".ToLower())return true;
It is odd that 0 = true but anything above 0 or below is false.Code:string s1 = "YEs"; string s2 = "YES"; if(string.compare(s1.ToLower(),s2.ToLower())==0) { // They Match }
Rather than using .ToLower() you can pass a third argument to string.compare to ignore case:
You have other options as well:Code:string s1 = "YEs"; string s2 = "YES"; if(string.compare(s1, s2, true)==0) { // They Match }
String.CompareOrdinal() will return the numeric Unicode values for each character compared
String.Equals() returns a boolan value similar to using "=="
Now he has the option to choose. My method with passing the third param looks neater as well. Your post is irrelevant to his problem. -rep
OK, here's my code then:
Your code:Code:string s1 = "YEs"; string s2 = "YES"; bool match = (s1.ToLower() == s2.ToLower()) ? true : false;
My one is neater. ---repCode:string s1 = "YEs"; string s2 = "YES"; if(string.compare(s1, s2, true)==0) { // They Match }
You'll still need an if statement in yours to diagnose "match" later, making your code one line longer than mine. Your code is inferior to mine not to mention slower.
Fail.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks
Algorithms and Data Structures
Java tutorials
Algorithms Forum