Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are .NET string operations case sensitive?

Are .NET string functions like IndexOf("blah") case sensitive?

From what I remember they aren't, but for some reason I am seeing bugs in my app where the text in the query string is in camel case (like UserID) and I'm testing for IndexOf("userid").

like image 705
mrblah Avatar asked Sep 01 '09 20:09

mrblah


2 Answers

Yes, string functions are case sensitive by default. They typically have an overload that lets you indicate the kind of string comparison you want. This is also true for IndexOf. To get the index of your string, in a case-insensitive way, you can do:

string blaBlah = "blaBlah";
int idx = blaBlah.IndexOf("blah", StringComparison.OrdinalIgnoreCase);
like image 161
driis Avatar answered Nov 15 '22 18:11

driis


One thing I'd like to add to the existing answers (since you were originally asking about ASP.NET):

Some name/value collections, such as the Request.QueryString and probably also Request.Form are not case-sensitive. For example if I navigate to an ASPX page using the following URL

http://server/mypage.aspx?user=admin

then both of the following lines will return "admin":

var user1 = Request.QueryString["user"];
var user2 = Request.QueryString["USER"];
like image 32
M4N Avatar answered Nov 15 '22 17:11

M4N