Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Comparing strings with different case [duplicate]

I'm reading a username and then checking to see if exists in another database table, the problem is whilst the username is the same the case maybe different and is preventing it from finding a match example jsmith and JSmith or JSMITH.

How can I fix this? Should I lower the case when writing to the first database or can I alter my code below when I'm comparing the two?

drUser["Enrolled"] = 
    (enrolledUsers.FindIndex(x => x.Username == (string)drUser["Username"]) != -1);

UPDATE:

Still struggling with this, the code below compiles but doesn't give the correct result, when viewing enrolled users I see those that aren't enrolled, when viewing those that are not enrolled I see 1 that is enrolled but their username case is the same in each datababse. Have I formatted the code below correctly?

drUser["Enrolled"] = (enrolledUsers.FindIndex(x => x.Username.Equals((string)drUser["Username"], StringComparison.OrdinalIgnoreCase)));

Thanks Jamie

like image 974
Jamie Avatar asked Jun 25 '10 15:06

Jamie


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C language basics?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


2 Answers

You need to cal the Equals method, which takes a StringComparison parameter.

For example:

x.Username.Equals((string)drUser["Username"], StringComparison.OrdinalIgnoreCase)

If x.Username can be null, you should call the static Equals method:

String.Equals(x.Username, (string)drUser["Username"], StringComparison.OrdinalIgnoreCase)

Otherwise, x.Username.Equals can throw a NullReferenceException.

like image 117
SLaks Avatar answered Oct 12 '22 11:10

SLaks


The preferred way to do this, is to specify the string comparison by using something like

string.Equals(x.Username, (string)drUser["Username"], StringComparison.OrdinalIgnoreCase

to do the equality check, instead of "=="

like image 30
Rob Levine Avatar answered Oct 12 '22 12:10

Rob Levine