How to compare strings in ASP.NET

Comparing 2 strings is often used action in ASP.NET. You could want to compare two strings when you check if the username and pasword entrered by user are correct. Also it could be very useful if you search some text.The ASP.NET provides several methods to compare the values of strings.

Compare strings in ASP.NET

Compare strings in C# an VB.NET

– You could use the Compare method to determine the relative values of two strings. It will return 0 if the 2 strings are equals.

String.Compare(“www.windowshostingasp.net”, “www.windowshostingasp.net”);

– You could use the Compare method to determine the relative values of two strings. It will return the same values as Compare method.

String.CompareOrdinal(“www.windowshostingasp.net”, “www.windowshostingasp.net”);

– You could use the String.Equals method to determine if two strings are the same.

C#

string MyString = “www.windowshostingasp.net”;
Console.WriteLine(MyString.Equals(“www.windowshostingasp.net”));

VB.NET

Dim MyString As String = “www.windowshostingasp.net”
Console.WriteLine(MyString.Equals(“www.windowshostingasp.net”))