如何让C#的switch语句使用IGNORECASE [英] How to make the C# Switch Statement use IgnoreCase

查看:232
本文介绍了如何让C#的switch语句使用IGNORECASE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个开关case语句,其中在交换机的对象是字符串,是有可能这样做呢IGNORECASE比较?

If I have a switch-case statement where the object in the switch is string, is it possible to do anyway ignoreCase compare?

我有,例如:

string s = "house";
switch (s)
{
  case "houSe": s = "window";
}

威尔小号获取价值的窗口。如何重写的switch-case语句,以便它将使用IGNORECASE比较字符串?

Will s get value "window". How to override the switch-case statement so it will compare the strings using ignoreCase?

推荐答案

你似乎知道,lowercasing两个字符串,并比较它们是不一样的做一个忽略大小写的比较。有很多原因。例如,统一code标准,用变音符号的文字是EN codeD多种方式。有些字符既包括基本字符和一个code点的音调符号。这些字符也可能是psented为基数字符后跟一个组合音调符号的字符重新$ P $。这两个重presentations是人人平等的目的,并在.NET Framework中的文化感知的字符串比较将会正确识别它们作为平等的,不管是用的CurrentCulture或InvariantCulture的(有或没有IGNORECASE)。一个序号比较,而另一方面,将错误地把它们看作是不平等的。

As you seem to be aware, lowercasing two strings and comparing them is not the same as doing an ignore-case comparison. There are lots of reasons for this. For example, the Unicode standard allows text with diacritics to be encoded multiple ways. Some characters includes both the base character and the diacritic in a single code point. These characters may also be represented as the base character followed by a combining diacritic character. These two representations are equal for all purposes, and the culture-aware string comparisons in the .NET Framework will correctly identify them as equal, with either the CurrentCulture or the InvariantCulture (with or without IgnoreCase). An ordinal comparison, on the other hand, will incorrectly regard them as unequal.

不幸的是,开关没有做任何事情,但一个序号比较。序号比较是罚款,某些种类的应用,如解析与严格的定义,codeS的ASCII文件,但序号字符串比较是错误的大多数其他用途。

Unfortunately, switch doesn't do anything but an ordinal comparison. An ordinal comparison is fine for certain kinds of applications, like parsing an ASCII file with rigidly defined codes, but ordinal string comparison is wrong for most other uses.

我在过去做的,以获得正确的行为只是嘲笑了我自己的switch语句。有很多方法可以做到这一点。一种方法是创建一个列表< T> 对案字符串和代表的。该列表可以使用正确的字符串比较进行搜索。当找到匹配的则相关联的委托可以被调用。

What I have done in the past to get the correct behavior is just mock up my own switch statement. There are lots of ways to do this. One way would be to create a List<T> of pairs of case strings and delegates. The list can be searched using the proper string comparison. When the match is found then the associated delegate may be invoked.

另一个选择是做,如果语句明显的链条。这通常被证明是没有听起来那么糟糕,因为结构非常有规律的。

Another option is to do the obvious chain of if statements. This usually turns out to be not as bad as it sounds, since the structure is very regular.

关于这个伟大的事情是,有没有真正对字符串进行比较时,嘲讽你自己的交换机功能的任何性能损失。该系统是不会让一个O(1)跳表它能与整数的方式,所以这将是每串一次比较反正

The great thing about this is that there isn't really any performance penalty in mocking up your own switch functionality when comparing against strings. The system isn't going to make a O(1) jump table the way it can with integers, so it's going to be comparing each string one at a time anyway.

如果有许多情况进行比较和性能是一个问题,那么列表&LT; T&GT; 上述选项可以用一个排序的字典或哈希表来代替。然后,性能可能会潜在地达到或超过switch语句选项。

If there are many cases to be compared, and performance is an issue, then the List<T> option described above could be replaced with a sorted dictionary or hash table. Then the performance may potentially match or exceed the switch statement option.

下面是代表名单的例子:

Here is an example of the list of delegates:

delegate void CustomSwitchDestination();
List<KeyValuePair<string, CustomSwitchDestination>> customSwitchList;
CustomSwitchDestination defaultSwitchDestination = new CustomSwitchDestination(NoMatchFound);
void CustomSwitch(string value)
{
    foreach (var switchOption in customSwitchList)
        if (switchOption.Key.Equals(value, StringComparison.InvariantCultureIgnoreCase))
        {
            switchOption.Value.Invoke();
            return;
        }
    defaultSwitchDestination.Invoke();
}

当然,你可能会想添加一些标准参数和可能的返回类型为CustomSwitchDestination委托。而且你要做出更好的名字!

Of course, you will probably want to add some standard parameters and possibly a return type to the CustomSwitchDestination delegate. And you'll want to make better names!

如果您的每一个案件的行为是不服从委托调用以这种方式,例如,如果参数不同的充是必要的,那么你坚持链接如果 statments。我也做过这几次。

If the behavior of each of your cases is not amenable to delegate invocation in this manner, such as if differnt parameters are necessary, then you’re stuck with chained if statments. I’ve also done this a few times.

    if (s.Equals("house", StringComparison.InvariantCultureIgnoreCase))
    {
        s = "window";
    }
    else if (s.Equals("business", StringComparison.InvariantCultureIgnoreCase))
    {
        s = "really big window";
    }
    else if (s.Equals("school", StringComparison.InvariantCultureIgnoreCase))
    {
        s = "broken window";
    }

这篇关于如何让C#的switch语句使用IGNORECASE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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