Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Monday 6 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

Method6: Using String.Compare() method 

Compare() method is present in System.String class. Compare() method compares two user specified string/substring by giving out result in numerical values. This method performs various operations like ignorecase, StringComparison, CultureInfo and CompareOptions. It returns the output in integer type.

Range                  Description
outValue<=0, Means string1 less than string2 by sort order.
outValue=0,   Means string1 equal to string2 by sort order.
outValue>=0, Means string1 greater than string2 by sort order.

Compare() method contain 10 overloads:

Overload Method1: int Compare(string strA, string strB);
Overload Method2: int Compare(string strA, string strB, bool ignoreCase);
Overload Method3: int Compare(string strA, string strB, StringComparison comparisonType);
Overload Method4: int Compare(string strA, string strB, bool ignoreCase, CultureInfo culture);
Overload Method5: int Compare(string strA, string strB, CultureInfo culture, CompareOptions options);
Overload Method6: int Compare(string strA, int indexA, string strB, int indexB, int length);
Overload Method7: int Compare(string strA, int indexA, string strB, int indexB, int length, bool ignoreCase);
Overload Method8: int Compare(string strA, int indexA, string strB, int indexB, int length, StringComparison comparisonType);
Overload Method9: int Compare(string strA, int indexA, string strB, int indexB, int length, bool ignoreCase, CultureInfo culture);
Overload Method10: int Compare(string strA, int indexA, string strB, int indexB, int length, CultureInfo culture, CompareOptions options);

[Note]: You can find all the overload in the definition of String Class.
And how to reach there?
Step-1 Open a Console Application.
Step-2 Write a simple line of code in which you are just declaring a string variable.
For ex- 
using System;

namespace AspnetSolutions

{
    class stringComparison
    {
        public static void
Main()
         {
            string name;
        }
    }
}
Step-3 Take your mouse cursor on to the string keyword.
Step-4 Press F12.
Step-5 You will get String[from metadata] window. Now, press Ctrl+F(to open the find dialog box). Search for the keyword "compare".
Step-6 You will get all the 10 overloads of Compare() method,  which we have listed above. Now click the expand button to its respective code line. And there you will find all the information related to this command.

Click imaginationhunt.blogspot to see latest Blogs 

Let's understand the 2 most common overloads of Compare().


Overload Method2:



This method takes 3 parameters(P) i.e.
P1: stringA (first specified string)
P2: stringB (second specified string)
P3: boolean value(true to ignorecase otherwise false)


Practical Implementation:

using System;

namespace AspnetSolutions

{
    class stringComparison
    {
        public static void
Main()
         {
            string str1 = "Imaginationhunt.BLOGSPOT.COM";
            string str2 = "imaginationhunt.blogspot.com";

            int resp = string.Compare(str1, str2, true);
            Console.WriteLine("Response = {0}", resp);

        }
    }
}



Output



Hence, we got the output saying both strings matched.


Overload Method7:



This method takes 6 parameters(P) i.e.
P1: stringA (first specified string) 
P2: starting index of the substring in stringA.
P3: stringA (second specified string)
P4: starting index of the substring in stringB.
P5: length (i.e. length: the maximum number of characters that you want to compare.)
P6: boolean value (true to ignorecase otherwise false)

Practical Implementation:

using System;

namespace AspnetSolutions

{
    class stringComparison
    {
        public static void
Main() 
        {
            string str1 = "Imaginationhunt.BLOGSPOT.COM";
            string str2 = "imaginationhunt.blogspot.com";

            int startIndexOfstr1 = str1.IndexOf('.');
            int startIndexOfstr2 = str2.IndexOf('.');
            int numberofcharactertocompare = 8;

            int resp = string.Compare(str1, startIndexOfstr1, str2, startIndexOfstr2, numberofcharactertocompare, true);
            Console.WriteLine("Parameters:"+
                "\nstr1 = "+ str1 +
                "\nstartIndexOfstr1 = "+ startIndexOfstr1 +
                "\nstr2 = "+ str2 +
                "\nstartIndexOfstr2 = "+ startIndexOfstr2 +
                "\nnumberofcharactertocompare = "+numberofcharactertocompare +
                "\nbool = true\n");
            Console.WriteLine("Response = {0}", resp);

        }
    }
}


Output




Hence, we got output saying both substrings(i.e. blogspot) matched.


To read STRING SPLIT IN C# - Click here.

Related Questions:  

#newtoprgm try first program

Q-1 Difference between Compare() and CompareTo() method? Ans- Differences:-
a) CompareTo() - perform a case sensitive comparison.
    Compare() - there is no such limitation with this method.
b) CompareTo() - only compare complete string.
    Compare() - compare complete string as well as substring.

Q-2 What is the output of the following code?

using System;

namespace AspnetSolutions

{
    class stringComparison
    {
        public static void
Main()
         {
            string str1 = "Imaginationhunt.BLOGSPOT.COM";
            string str2 = "imaginationhunt.blogspot.com";


            int resp = string.Compare(str1, str2, StringComparison.OrdinalIgnoreCase);
            Console.WriteLine("Response = {0}", resp);

        }
    }
}
 

A) -1
B) 0
C) 1

Ans- Option(B).
[Note]: We have used the Compare Overload method3 here.

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...