C# help

VeeDubbs

Limp Gawd
Joined
Dec 9, 2005
Messages
398
Hi all -

I'm extremely new to this stuff - so take it easy please.

Anyways...I am working on a website, and at one point before a user can continue to a certain page they must answer three questions correctly. My question is what is the best way for me to do this where case is not an issue? I don't want it to matter if the user does all lowercase, all uppercase or if they only capitalize the first letter. Is there a simple way to do this? I've done some googling and can't come across anything that really makes much sense to me. Thanks for any help!
 
I don't know much about C# but have you tried using an If-Else statement?

-Good Luck
TopGun
 
i know in java, you can do something like (i forget the correct syntax)


String a; //String that the user inputs

a.equalToIgnoreCase("Baseball");


Dont know if thats in C# (though it should be)

furthermore, you can do:

String a; //String that the user inputs
String b; //String that the user inputs
String c; //String that the user inputs

if (a.equalToIgnoreCase("Baseball") && b.equalToIgnoreCase("Football") && c.equalToIgnoreCase("Basketball");


that way the if block will only be executed if all three conditions are true
 
You might not have a case-insensitive string equality, but a quick google on "C# string methods" shows that Strings have a ToLower and ToUpper methods.
 
I have to disagree, amromousa. I think that it leaves a few stones unturned.

While System.String does have ToLower() and ToUpper() methods, it's important to remember that these methods produce new strings.

I think it's a bit better to use the String.Compare() or String.CompareTo() methods, which take a flag indicating case is not to be considered when performing the comparison. These don't create a temporary object.
 
Thanks guys. The ToUpper() seemed to do what I needed it to do.

Mike - would you be willing to give me a small example of how to use the String.Compare? I searched a little on it and it kind of confused me.

Thanks!
 
Thanks guys. The ToUpper() seemed to do what I needed it to do.

Mike - would you be willing to give me a small example of how to use the String.Compare? I searched a little on it and it kind of confused me.

Thanks!

It'd be something like

Code:
if(String.Compare(str_1, str_2, true) == 0) // true for case insensitive? check the reference
{
   ....
}
 
If you have str and you want compare it to the string literal "something", you might code

Code:
if ( str.ToUpper() == "SOMETHING" )
{
}

This creates a copy of str, though. To do the same thing without creating a copy of str, you can use

Code:
if ( String.Compare( str, "SOMETHING", true ) )
{
}
 
I thought String.Compare returned 0 if the items were equal (it's not an equality comparison, but a lexical comparison, no?). Meaning he'd likely need to put a ! in front of "String.Compare( str, "SOMETHING", true )" if he wanted the case to be the affirmative.

If you have str and you want compare it to the string literal "something", you might code

Code:
if ( str.ToUpper() == "SOMETHING" )
{
}

This creates a copy of str, though. To do the same thing without creating a copy of str, you can use

Code:
if ( String.Compare( str, "SOMETHING", true ) )
{
}
 
I thought String.Compare returned 0 if the items were equal (it's not an equality comparison, but a lexical comparison, no?). Meaning he'd likely need to put a ! in front of "String.Compare( str, "SOMETHING", true )" if he wanted the case to be the affirmative.
Oh, yeah; sorry. I'm still moving house, so I don't have everything setup yet and can't quickly test code before I post it. Indeed, you'll want to see if the return from CompareTo() is zero, which indicates equality. I'd test with ==0 instead of using the bang operator, though.
 
OP - Is this, from a business rules perspective, something that could be validated client-side before hitting the server? Perhaps using a Regular Expression Validator or a Custom Validator (which might even call a JS method) could be a solution that saves a hit on the web server.
 
Oh, yeah; sorry. I'm still moving house, so I don't have everything setup yet and can't quickly test code before I post it. Indeed, you'll want to see if the return from CompareTo() is zero, which indicates equality. I'd test with ==0 instead of using the bang operator, though.

A lot of my string comparisons (when applicable) will use the OrdinalIgnoreCase flag, which gets the job done.

Code:
if (0 == String.Compare(stringOne, stringTwo, StringComparison.OrdinalIgnoreCase))
{
 
}

I've seen this method recommended on several popular websites, so I figured I'd put it up here too. :)

202276
 
A lot of my string comparisons (when applicable) will use the OrdinalIgnoreCase flag, which gets the job done.
It'll depend on what "the job" is. OrdinalIgnoreCase ignores locale rules, which might or might not be the right thing for the OP.
 
Mike:

Yeah, I should've specified that and linked the page to the enum for the reader's edification:

The String.Compare static method can be found here, and the specific comparison I used can be found here.
The StringComparison enumeration can be found here.

From the StringComparison enumeration:
MSDN said:
An operation that uses ordinal sort rules performs a comparison based on the numeric value (Unicode code point) of each Char in the string. An ordinal comparison is fast, but culture-insensitive. Using ordinal sort rules a string that starts with some Unicode character, U+xxxx, comes before a string that starts with some Unicode character, U+yyyy, if the value of xxxx is numerically less than yyyy.

202276
 
Back
Top