比较两个字符串,忽略在C#中的情况下 [英] Comparing two strings, ignoring case in c#

查看:123
本文介绍了比较两个字符串,忽略在C#中的情况下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/6120154/what-is-difference-between-different-string-compare-methods\">What之间是不同的字符串比较方法

以下哪两个是更有效率? (或者,也许有那最好还是有第三种选择?)

Which of the following two is more efficient? (Or maybe is there a third option that's better still?)

string val = "AStringValue";

if (val.Equals("astringvalue", StringComparison.InvariantCultureIgnoreCase))

if (val.ToLowerCase() == "astringvalue")

推荐答案

第一种是正确的,恕我直言更有效的,因为第二个'解决方案'实例化一个新的字符串实例。

The first one is the correct one, and imho the more efficient one, since the second 'solution' instantiates a new string instance.

这篇关于比较两个字符串,忽略在C#中的情况下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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