C#:关于 ToUpper() 和 ToLower() 的混淆

发布于 2024-09-19 17:42:46 字数 421 浏览 1 评论 0原文

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

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

这不会进入“if”块......对吧?

或者

它会检查“in”和“IN”并执行其中的任何操作吗?如果是这样,那是为什么呢?它不是应该跳过“if”块内部的内容吗?

同样的困惑也发生在 ToLower()

编辑:所以要检查这两种情况,我需要写:

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

像这样..对吧?

if I do something like this...

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

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

or

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?

Same confusion is about ToLower() too

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

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

Like this..right?

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

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

发布评论

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

评论(5

瑶笙 2024-09-26 17:42:46

您应该使用不区分大小写的相等比较,而不是转换为大写字母然后进行比较。例如:

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

您应该仔细考虑哪些规则是合适的 - 序数、当前区域性、不变区域性或可能完全是另一种区域性(例如使用 StringComparer.Create(culture, true))。

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

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))
{
     ...
}

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)).

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

百变从容 2024-09-26 17:42:46

表达式 something.ToUpper().Equals("lowercaseletters") 永远不会为 true,因此在您的示例中,if 块将不会被执行。当然,这也适用于 ToLower; something.ToLower().Equals("UPPERCASE") 也永远不会是 true。

The expression something.ToUpper().Equals("lowercaseletters") will never be true, so in your example the if-block will not be executed. And of course, this applies to ToLower as well; something.ToLower().Equals("UPPERCASE") will never be true, either.

李白 2024-09-26 17:42:46

“IN”不等于“in” - 因此它不执行 if 块。在 toLower() 的情况下,它将执行 if 块,因为“in”等于“in”。

"IN" is not equal to "in" - so it does not execute the if block. In the case of toLower() it would execute the if block as "in" equals "in"..

梦旅人picnic 2024-09-26 17:42:46

首先,如果要比较字符串,请使用 .Equals()

myVar.toUpper().Equals("in")

其次,首先执行 if 内的所有代码,仅在测试返回之后。

所以

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

不要“做某事”。

First if you want to compare strings use .Equals()

myVar.toUpper().Equals("in")

Second first all code inside the if is executed, only after that the return is tested.

so

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

don't "do something".

牵你手 2024-09-26 17:42:46

如果您执行像您所说的那样的操作,它将不会进入 if 块,原因如下:

运算符应用于左侧的对象。所以你的代码与这样写是一样的:

String myVar="in";
String testVar = myVar.ToUpper();
if(testVar=="in") //This will never be true
{
  //do something
}

在你的编辑中,你仍然没有测试你的字符串是否 == "IN",你正在做 2 个测试来看看你的字符串是否 == "in"。

如果您将原始更改为这样,它将起作用:

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

您的编辑应该像这样来测试这两种情况:

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

编辑:史蒂文评论中的更多解释:

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

该代码示例进行了 2 次比较,但如果 myVar 只会是 in 的混合大小写版本(即:in In iN IN)则不需要第二次比较。一旦我将字符串转换为 ToUpper(),您只需检查它是否等于 IN。所以我会将该行替换为:

if(myVar.ToUpper().Equals("IN"))

或者

if(myVar.ToUpper() == "IN")

我个人会使用 == 而不是 .Equals 方法。

If you do something like you said, it will not go into the if block, and here is why:

Operators are applied to the object on the left. So your code is the same as writing this:

String myVar="in";
String testVar = myVar.ToUpper();
if(testVar=="in") //This will never be true
{
  //do something
}

In your edit, you still aren't testing if your string is == "IN", you are doing 2 tests to see if your string is == "in".

If you changed your original to this it would work:

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

Your edit should be like this to test both cases:

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

EDIT: Some more explanation from Steven's Comment:

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

That code sample does 2 comparisons, but if myVar will only ever be mixed case versions of in (IE: in In iN IN) then the second comparison is not necessary. Once I have converted the string to ToUpper(), you only need to check if it is equal to IN. So I would replace that line with:

if(myVar.ToUpper().Equals("IN"))

or

if(myVar.ToUpper() == "IN")

I would personally use the == not the .Equals method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文