0

我是一名 C++ 开发人员,最近转向 C#。我正在开发一个 WPF 应用程序,我需要在其中检查 CASE Sensitive。我在 C++ 中完成了如下操作:

// BoardName and Board Version are of tye String
if(m_boardName->compareIgnoreCase("S1010012") == 0)
{   
    m_voltageChannels = redhookChannels;
}
else if(m_boardName->compareIgnoreCase("S1010018") == 0)//For bavaria board
{
    if(m_boardVersion->compareIgnoreCase("001A") == 0 || 
       m_boardVersion->compareIgnoreCase("001B") == 0 || 
       m_boardVersion->compareIgnoreCase("001C") == 0 )
    {
        m_voltageChannels = bavaria1Channels;
    }
    else if(m_boardVersion->compareIgnoreCase("002B") == 0)
    {
        m_voltageChannels = bavaria2Channel;
    }   
}

在我的 WPF 应用程序中,我这样做了:

string redhookboardname = "S1010012";
    string redhookboardnameCase = "s1010012";

    string bavariaboardnameCase = "s1010018";
    string bavaria1boardversion1Case = "001a";
    string bavaria1boardversion2Case = "001b";
    string bavaria1boardversion3Case = "001c";

    string bavariaboardname = "S1010018";
    string bavaria1boardversion1 = "001A";
    string bavaria1boardversion2 = "001B";
    string bavaria1boardversion3 = "001C";

    string bavaria2boardversion = "002B";
    string bavaria2boardversionCase = "002b";

    public void OnChannel()
    {
        if (redhookboardnameCase.Equals(redhookboardname, StringComparison.InvariantCultureIgnoreCase))
        {
            mVoltageStruct = redhookboard;
        }

        else if (bavariaboardnameCase.Equals(bavaria1boardversion1, StringComparison.InvariantCultureIgnoreCase))
        {
            if (bavaria1boardversion1Case.Equals(bavariaboardname, StringComparison.InvariantCultureIgnoreCase)
                || bavaria1boardversion2Case.Equals(bavaria1boardversion2, StringComparison.InvariantCultureIgnoreCase)
                || bavaria1boardversion3Case.Equals(bavaria1boardversion3, StringComparison.InvariantCultureIgnoreCase))
            {
                mVoltageStruct = bavaria1board;
            }
            else if (bavaria2boardversionCase.Equals(bavaria2boardversion, StringComparison.InvariantCultureIgnoreCase))
            {
                mVoltageStruct = bavaria2board;
            }
        } 
    }

[更新代码]:这可以像我上面那样实现吗???

4

2 回答 2

1

您可以使用 string.Equals 的重载

String.Equals 方法(字符串、字符串比较)

例如:

string str1 = "s101001b";
string str2 = "S101001B";
if (str1.Equals(str2, StringComparison.InvariantCultureIgnoreCase))
{
    //equal
}

您可能还会看到一篇有趣的文章:土耳其 İ 问题以及您应该关心的原因

于 2012-10-12T05:32:21.377 回答
0

您也可以使用 string.Compare

if(string.Compare(str1, str2, StringComparison.InvariantCultureIgnoreCase)==0)
{
//equal
}
于 2012-10-12T05:49:08.177 回答