开发者

string.compare and string for comparison

I have my code like this

private void btnStartAnalysis_Click(object send开发者_如何学运维er, EventArgs e)

            {

        //Checks for the selectedItem in the cmbOpearions dropdown and make call to appropriate functions.
        if((string) (cmbOperations.SelectedItem) == "PrimaryKeyTables")
            {
            //This is the function call for the primary key checking in DB
            GetPrimaryKeyTable();
            }

        //Checks for the selectedItem in the cmbOpearions dropdown and make call to appropriate functions.
        if((string) (cmbOperations.SelectedItem) == "NonPrimaryKeyTables")
            {
            //This is the function call for the nonPrimary key checking in DB
            GetNonPrimaryKeyTables();
            }

        //Checks for the selectedItem in the cmbOpearions dropdown and make call to appropriate functions.
        if((string) (cmbOperations.SelectedItem) == "ForeignKeyTables")
            {
            //This is the function call for the nonPrimary key checking in DB
            GetForeignKeyTables();
            }

        //Checks for the selectedItem in the cmbOpearions dropdown and make call to appropriate functions.
        if((string) (cmbOperations.SelectedItem) == "NonForeignKeyTables")
            {
            //This is the function call for the nonPrimary key checking in DB
            GetNonForeignKeyTables();
            }

        if((string) (cmbOperations.SelectedItem) == "UPPERCASEDTables")
            {
            //This is the function call for the nonPrimary key checking in DB
            GetTablesWithUpperCaseName();
            }

        if((string) (cmbOperations.SelectedItem) == "lowercasedtables")
            {
            //This is the function call for the nonPrimary key checking in DB
            GetTablesWithLowerCaseName();
            }
        }

But here using (string) makes problem in case sensitiveness.So I want to use string.comapare in place of (string).

Can anybody give me any hint how to use it.


I suggest you use:

// There's no point in casting it in every if statement
string selectedItem = (string) cmbOperations.SelectedItem;

if (selectedItem.Equals("NonPrimaryKeyTables",
                        StringComparison.CurrentCultureIgnoreCase))
{
    ...
}

Choosing the right string comparison can be tricky. See this MSDN article for a lot more information.

I wouldn't suggest using Compare as other people have suggested, simply because it's not the right emphasis - Compare is designed to be used for testing which order strings should appear in when they're sorted. That has the by-product of allowing you to test for equality, but it's not the main aim. Using Equals shows that all you care about is equality - if the two strings aren't equal, you don't care which would come first. Using Compare would work, but it doesn't leave your code expressing itself as clearly as it can.


try this:

if (string.Compare((string) cmbOperations.SelectedItem, 
        "NonForeignKeyTables", true) == 0) // 0 result means same
    GetNonForeignKeyTables();


For case insensitive comparison:

    string a = "text1";
    string b = "TeXt1";

    if(string.Compare( a, b, true ) == 0) {
        Console.WriteLine("Equal");
    }


MSDN have really good explanation about the string.compare method.. you just have to write

String.Compare (String, String) 

to compare your strings.. used with a Switch-case instruction it will be ok..

Use

String.Compare (String, String, boolean)

To set the case comparaison

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜