C# 比较不同大小写的字符串

发布于 2024-09-07 18:39:09 字数 600 浏览 2 评论 0原文

我正在读取用户名,然后检查是否存在于另一个数据库表中,问题是虽然用户名相同,但大小写可能不同,并且阻止它找到匹配示例 jsmith 和 JSmith 或 JSMITH。

我该如何解决这个问题?在写入第一个数据库时是否应该降低大小写,或者在比较两者时可以更改下面的代码吗?

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

更新:

仍在努力解决这个问题,下面的代码可以编译,但没有给出正确的结果,当查看已注册的用户时,我看到那些未注册的用户,当查看那些未注册的用户时,我看到 1 已注册,但他们的用户名大小写每个数据库中都是相同的。我是否正确格式化了下面的代码?

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

谢谢 杰米

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

不美如何 2024-09-14 18:39:09

您需要计算 Equals 方法,它采用 StringComparison 参数。

例如:

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

如果x.Username可以为null,则应调用静态 Equals 方法

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

否则,x.Username.Equals 可能会抛出 NullReferenceException

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.

羁〃客ぐ 2024-09-14 18:39:09

执行此操作的首选方法是通过使用类似

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

进行相等性检查的方式来指定字符串比较,而不是“==”

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 "=="

苏璃陌 2024-09-14 18:39:09

你试过这个吗?

string userName = (string)drUser["Username"];
bool enrolled = enrolledUsers.Exists(x =>
  string.Equals(x.Name, userName, StringComparison.CurrentCultureIgnoreCase));

您使用 FindIndex 来代替有什么原因吗?

Have you tried this?

string userName = (string)drUser["Username"];
bool enrolled = enrolledUsers.Exists(x =>
  string.Equals(x.Name, userName, StringComparison.CurrentCultureIgnoreCase));

Is there a reason you are using FindIndex instead?

仙女 2024-09-14 18:39:09

尝试 string.compare 方法。
所有重载

或更多 具体一个

如果没有别的,我希望它能有所启发。

try the string.compare method.
all overloads

Or a more specific one

If nothing else, I hope it educates.

烈酒灼喉 2024-09-14 18:39:09

使用 ToUpper() 怎么样。

 if(!(dr["Enrolled"] == null || dr["Username"] == null))
 {
    if(dr["Enrolled"].ToString().ToUpperInvariant()== dr["Username"].ToString().ToUpperInvariant())
    {
        //Do Something
    }
}

How about using ToUpper().

 if(!(dr["Enrolled"] == null || dr["Username"] == null))
 {
    if(dr["Enrolled"].ToString().ToUpperInvariant()== dr["Username"].ToString().ToUpperInvariant())
    {
        //Do Something
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文