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
Method4: Using string.CompareOrdinal() method
CompareOrdinal() method is present in System.String class. CompareOrdinal() method compares two user specified substring by giving out result in numerical values. This method performs a case-sensitive comparison. It returns the output in integer type.
Range Description
outputValue<=0, Means string1 is less than string2.
outputValue=0, Means string1 is equal to string2.
outputValue>=0, Means string1 is greater than string2.
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)
Let's understand this by an example.
Practical Implementation:
using System;
namespace AspnetSolutions
{
class stringComparison
{
public static void Main()
{
string var1 = "imaginationhunt.blogspot";
string var2 = "imaginationhunt.blogspot.com";
}
}
CompareOrdinal() method is present in System.String class. CompareOrdinal() method compares two user specified substring by giving out result in numerical values. This method performs a case-sensitive comparison. It returns the output in integer type.
Range Description
outputValue<=0, Means string1 is less than string2.
outputValue=0, Means string1 is equal to string2.
outputValue>=0, Means string1 is greater than string2.
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)
Let's understand this by an example.
Practical Implementation:
namespace AspnetSolutions
{
class stringComparison
{
public static void Main()
{
string var1 = "imaginationhunt.blogspot";
string var2 = "imaginationhunt.blogspot.com";
Console.WriteLine(string.CompareOrdinal(var1, var2));
}}
}
Output
Here, the output is less than zero. Means, var1 is less than var2. Because var2 string contains extra ".com" in reference to string var1. That's why output shows -4.
Click imaginationhunt.blogspot to see latest Blogs
Overload Method2: Second overload takes five parameters(P) i.e.
P1: string1(first specified string)
P2: starting index of the substring in string1.
P3: string2(second specified string)
P4: starting index of the substring in string2.
P5: length (i.e. length: the maximum number of characters that you want to compare.)
Practical Implementation:
using System;P2: starting index of the substring in string1.
P3: string2(second specified string)
P4: starting index of the substring in string2.
P5: length (i.e. length: the maximum number of characters that you want to compare.)
Practical Implementation:
namespace AspnetSolutions
{
class stringComparison
{
public static void Main()
{
string var1 = "imaginationhunt.blogspot.in";
string var2 = "imaginationhunt.blogspot.in";
//if result is 0. means both strings are same
Console.WriteLine(string.CompareOrdinal(var1, var1.IndexOf('.'), var2, var2.IndexOf('.'), 8));
Console.WriteLine(string.CompareOrdinal(var1, var1.IndexOf('.'), var2, var2.IndexOf('.'), 8));
}
}
}
}
}
Output
Here, the output is zero. Means both the strings are equal. But, if you see the code we not using the complete string. We are only using the substring i.e. "blogspot" from both the strings. If you notice we have taken parameter like-
P1 = var1 (i.e. string1),
P2 = position of '.' in var1 (i.e. 15),
P3 = var2 (i.e. string2),
P4 = position of '.' in var2 (i.e. 15),
P5 = length we have used 8 (i.e. comparing next 8 character for character after '.' from both string var1 and var2).
And when we find that string matched properly, we get output as 0.
Related Questions:
Q-1 Can we ignorecase if we are using CompareOrdinal()?
Ans- No.
Q-2 What is the criteria behind the output integer number for CompareOrdinal()?
string var1 = "a";
string var2 = "A";
Console.WriteLine(string.CompareOrdinal(var1, var2));
output is 32.
Ans- Concept is ASCII. As, a=97 and A=65. So, a-A=32.
Practical Implementaton:
namespace AspnetSolutions
{
class stringComparison
{
public static void Main()
{
Console.WriteLine("Enter a String 1:");
string name1 = Console.ReadLine();
Console.WriteLine("Enter a String 2:");
string name2 = Console.ReadLine();
int number = string.CompareOrdinal(name1, name2);
Console.WriteLine("Number is :{0}", number);
string Out = ((number < 0) ? "Less than" : ((number > 0) ? "Greater than" : "Equal"));
Console.WriteLine(Out);
}
}
}
If you enter string1 as 'a' and string 2 as 'A'. Then you find the number as 32. which is nothing but the difference between ASCII values.
string name1 = Console.ReadLine();
Console.WriteLine("Enter a String 2:");
string name2 = Console.ReadLine();
int number = string.CompareOrdinal(name1, name2);
Console.WriteLine("Number is :{0}", number);
string Out = ((number < 0) ? "Less than" : ((number > 0) ? "Greater than" : "Equal"));
Console.WriteLine(Out);
}
}
}
If you enter string1 as 'a' and string 2 as 'A'. Then you find the number as 32. which is nothing but the difference between ASCII values.
No comments:
Post a Comment