Are .NET string operations case sensitive?

前端 未结 4 2388
时光说笑
时光说笑 2021-02-20 09:33

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

相关标签:
4条回答
  • 2021-02-20 09:57

    As default they are case sensitive but most of them (if not all) including IndexOf has an overload that takes a StringComparison argument. E.g. if you pass

    StringComparison.InvariantCultureIgnoreCase 
    

    as the StringComparison argument to IndexOf it will (as the name implies) ignore case differences

    0 讨论(0)
  • 2021-02-20 09:59

    .NET string comparisons are indeed case sensitive. You could use things like ToUpper() to normalize things before comparing them.

    0 讨论(0)
  • 2021-02-20 10:13

    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);
    
    0 讨论(0)
  • 2021-02-20 10:20

    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"];
    
    0 讨论(0)
提交回复
热议问题