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
Method5: Using string1.CompareTo(string2) method
Compare.To() method is an instance of a specified string or object. This method compares the instance of the specified string/object with the string/object that appears within the overload of the method as parameter. This method performs a case-sensitive comparison. It returns the output in Integer type.
Range(R) Description
outputValue<=0, Means string1 less than string2.
outputValue=0, Means string1 is equal as string2.
outputValue>=0, Means string1 greater than string2.
Let's see every Range(R) value cases-
R1: When string1 less than string2.Let's see this in action quickly
string str1 = "A"; //ASCII Value for A=65
string str2 = "B"; //ASCII Value for B=66
int resp= str1.CompareTo(str2);
Console.WriteLine("Response = {0}", resp);
Output: Response = -1.
The output is very straight forward as we can predict 65<66, which give as -1.
R2: When string1 is equal as string2.
Let's see this in action quickly
string str1 = "A"; //ASCII Value for A=65
string str2 = "A"; //ASCII Value for A=65
int resp= str1.CompareTo(str2);
Console.WriteLine("Response = {0}", resp);
Output: Response = 0.
The output is very straight forward as we can predict 65=65, which give as 0.
R3: When string1 greater than string2.Let's see this in action quickly
string str1 = "B"; //ASCII Value for A=66
string str2 = "A"; //ASCII Value for B=65
int resp= str1.CompareTo(str2);
Console.WriteLine("Response = {0}", resp);
Output: Response = 1.
The output is very straight forward as we can predict 66>65, which give as 1.
string str1 = "B"; //ASCII Value for A=66
string str2 = "A"; //ASCII Value for B=65
int resp= str1.CompareTo(str2);
Console.WriteLine("Response = {0}", resp);
Output: Response = 1.
The output is very straight forward as we can predict 66>65, which give as 1.
This method has two overloaded constructors.
Overload Method1:
One takes two parameters(P) i.e.
P1: string1(first specified string)
P2: string2(second specified string, compare instance with string1)
Let's do it practically when we pass the instance type string.
Click imaginationhunt.blogspot to see latest Blogs
Practical Implementation:
using System;
namespace AspnetSolutions
{
class stringComparison
{
public static void Main()
{
string str1 = "B"; //ASCII Value for A=66
string str2 = "A"; //ASCII Value for A=65
int resp= str1.CompareTo(str2);
Console.WriteLine("Response = {0}", resp);
}
}
}
Output
Hence, we get the response as expected from the first overload of CompareTo() method.
Overload Method2:
One takes two parameters(P) i.e.
P1: object1(first specified object)
P2: object2(second specified object, compare instance with object1)
Let's do it practically when we pass the instance type object (i.e. datetime, int, char, float, string anything).
Practical Implementation:
using System;
namespace AspnetSolutions
{
class stringComparison
{
public static void Main()
{
//date passed in object
DateTime date1=new DateTime(2015,7,5);
DateTime date2=new DateTime(2000,7,5);
int responseDate= date1.CompareTo(date2) ;
Console.WriteLine("responseDate = {0}",responseDate);
//Integer passed in object
int number1 = 2523;
int number2 = 5000;
int responseInteger=number1.CompareTo(number2);
Console.WriteLine("responseInteger = {0}", responseInteger);
//character passed in object
char ch1='a';
char ch2 = 'z';
int responseChar=ch1.CompareTo(ch2);
Console.WriteLine("responseChar = {0}", responseChar);
//string passed
string str1 = "Hello";
string str2 = "Programmers's";
int resp= str1.CompareTo(str2);
Console.WriteLine("Response = {0}", resp);
}
}
}
Output
All are working fine as you can see the output.
ResponseDate (+1) shows date1 is greater than date2.
ResponseInteger (-1) shows number1 is less than number2.
ResponseCharacter (-25) shows ch1 is less than ch2 by 25 characters. As ASCII of z-a=122-97=25.
ResponseString (-1) shows str1 is smaller than str2.
To read STRING COMPARISON IN C# PART3 - Click here.
Related Questions:
Q-2 What will be the output of the code?
using System;
namespace AspnetSolutions
{
class stringComparison
{
public static void Main()
{
string str1 = "a";
string str2 = "A";
int resp= str1.CompareTo(str2);
Console.WriteLine("Response = {0}", resp);
namespace AspnetSolutions
{
class stringComparison
{
public static void Main()
{
string str1 = "a";
string str2 = "A";
int resp= str1.CompareTo(str2);
Console.WriteLine("Response = {0}", resp);
}
}
}
Click imaginationhunt.blogspot to see latest Blogs
A) Response = 32
B) Response = -32
C) Response = -1
D) Response = 1
Ans- Option(C).}
}
Click imaginationhunt.blogspot to see latest Blogs
A) Response = 32
B) Response = -32
C) Response = -1
D) Response = 1
Keep learning and sharing...
No comments:
Post a Comment