Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Friday 3 July 2015

STRING COMPARISON IN C#


Today in Engineer's World, we are discussing a very important topic of C# -String in very easy way. Posted By- +Manish Kumar Gautam +LIV WIRE  +ASP.NET SOLUTIONS

Click imaginationhunt.blogspot to see latest Blogs
 

COMPARE STRING

String comparison is a concept when we match one string with another. Matching is done character for character.
Comparing string is very useful topic, which everybody should know. The various examples, where we actually need the comparison of strings are URL, IP Address, File names, Path names and Email Address.

Some Basic Ways by which string can be compared.

Method1: By Double Equal sign (== ) operator

This is the most common and simple way to equate strings. Not only string this can also be used with many other data types like number, date, character, etc. And we get the decision in the form of bool return type i.e. True or False. If the output is True means string successfully matched and if False means the string doesn't match.

For ex- 
            string var1 = "imaginationhunt.blogspot.in";
            string var2 = "imaginationhunt.blogspot.com";
            bool decison = var1 == var2;
            Console.WriteLine(decison);

Output: False.
Explanation: As var1 uses ".in" (in domain part) and var2 uses ".com" which makes both string different. Hence, it returns False.

Method2: By String1.Equal(String2) method

This is the second very easy way to equalize string. Here the return type of method is bool. In this method we pass both strings as a parameter on which we need to perform the comparison. 

For ex- 
            string var1 = "imaginationhunt.blogspot.in";
            string var2 = "imaginationhunt.blogspot.com";
            bool decison1 = var1.Equals(var2);
            Console.WriteLine(decison1);

Output: False.
Explanation: Reason is same.

Method3: By String.Equal(String1, String2) method

Method2 can also be written like this.

For ex- 
            string var1 = "imaginationhunt.blogspot.in";
            string var2 = "imaginationhunt.blogspot.com";
            bool decison2 = Equals(var1, var2);
            Console.WriteLine(decison2);

Output: False.

But suppose we have two string which has similar characters, but different cases (lowercase and uppercase). Now, can we use the String.Equal() method to compare strings?
Yes. There is an overloaded constructor that contains three parameter, i.e. the two strings that need to be compared and the third parameter which tell us about what type of comparison we need to perform. Let's understand this by an example-

Practical Implementation:

using System;

namespace AspnetSolutions


{
    class stringComparison
    {
        public static void Main()
        {

            string url1 = "https://www.google.com";
            string url2 = "HTTPS://WWW.GOOGLE.COM";

            Console.WriteLine(string.Equals(url1, url2));
            Console.WriteLine(string.Equals(url1, url2,StringComparison.OrdinalIgnoreCase));

        }
    }
}


Output



Hence, by using the third parameter StringComparison.OrdinalIgnoreCase which ignore cases give us the result that we are expecting. 




There are several other properties of StringComparison that we'll discuss later.

To read STRINGS COMPARISON IN part2 - Click here.

Related Questions:

#newtoprgm try first program

Q-1 What is the correct way to find if contents of strings are equal or not?
A) (strvar1 == strvar2)
B) (strvar1 = strvar2)
C) strvar1.Equal(strvar2)
D) strvar1.Equals(strvar2)
Ans- Option(A and D).

Q-2 What will be the output of the code?

using System;

namespace AspnetSolutions


{
    class stringComparison
    {
        public static void Main()
        {

            string str = "Asp.net Solutions!";
            Console.WriteLine(String.Equals(str, "Asp.net Solutions!").GetType());

        }
    }
}


A) True 
B) False
C) System.String
D) System.Boolean
Ans- Option(D).

Click imaginationhunt.blogspot to see latest Blogs

Keep learning and sharing...

No comments:

Post a Comment

Featured post

Think that makes you rich and richer

 Napolean said: “You can think and grow rich, but if you can be brought up like most people with work and you won't starve, this wil...