C#:混淆ToUpper的()和tolower() [英] C#: Confusion about ToUpper() and ToLower()

查看:107
本文介绍了C#:混淆ToUpper的()和tolower()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我做这样的事情...

if I do something like this...

String myVar = "in";
if(myVar.ToUpper() == "in")
{
    //do something
}

这是不会的如果块..right里面去?

This is not going to go inside "if" block ..right?

是不是要检查既为in和IN,做任何有里面,如果?如果是这样,这是为什么?是不是应该跳过什么是里面的如果块?

Is it going to check BOTH for "in" AND "IN" and do whatever is there inside that if? If so, why is that ? Isn't it supposed to skip what's inside of "if" block?

同样的困惑是关于 ToLower将()

修改:那么检查这两种情况下,我需要写:

Edit: So to check for both cases, I need to write:

if((myVar.ToUpper().Equals("in"))&&(myVar.Equals("in")))

像this..right?

Like this..right?

推荐答案

而不是转换为大写字母,然后比较,你应该使用它可以做得不区分大小写的相等比较。例如:

Rather than converting to upper case and then comparing, you should use an equality comparison which can be made case-insensitive. For example:

if (myVar.Equals("in", StringComparison.OrdinalIgnoreCase))
{
     ...
}

您应该仔细考虑究竟哪些规则是适当的 - 序,目前的文化,文化不变,或者可能完全另一种文化(例如,使用 StringComparer.Create(文化,真))。

You should consider carefully exactly which rules are appropriate - ordinal, the current culture, the invariant culture, or possibly another culture entirely (e.g. using StringComparer.Create(culture, true)).

有关解决此更多详细信息,请阅读MSDN 最佳实践在.NET Framework中使用字符串的文章。

For more details around this, read the MSDN Best Practices for Using Strings in the .NET Framework article.

这篇关于C#:混淆ToUpper的()和tolower()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆