.NET 字符串操作区分大小写吗?
像 IndexOf("blah")
这样的 .NET 字符串函数是否区分大小写?
据我所知,它们不是,但出于某种原因,我在我的应用程序中看到了错误,其中查询字符串中的文本采用驼峰式大小写(如 UserID),并且我正在测试 IndexOf("userid")
。
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")
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我想在现有答案中添加一件事(因为您最初询问 ASP.NET):
一些名称/值集合,例如 Request.QueryString 以及可能还有 Request.Form不区分大小写。例如,如果我使用以下 URL 导航到 ASPX 页面
,则以下两行都将返回“admin”:
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
then both of the following lines will return "admin":
.NET 字符串比较确实区分大小写。在比较之前,您可以使用 ToUpper() 之类的方法来规范化它们。
.NET string comparisons are indeed case sensitive. You could use things like ToUpper() to normalize things before comparing them.
默认情况下,它们区分大小写,但大多数(如果不是全部)包括 IndexOf 都有一个采用 StringComparison 参数的重载。例如,如果您将
StringComparison 参数传递给 IndexOf ,它将(顾名思义)忽略大小写差异
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
as the StringComparison argument to IndexOf it will (as the name implies) ignore case differences
是的,字符串函数默认区分大小写。它们通常有一个重载,可以让您指示所需的字符串比较类型。对于 IndexOf 也是如此。要以不区分大小写的方式获取字符串的索引,您可以执行以下操作:
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: