Comparing 2 strings is often used action in any programming language. You could want to compare two strings when you check if the username and password entered by user are correct. Also it could be very useful if you search some text.This C# ASP.NET tutorial with show you several methods to compare the values of strings.

C# 8.0 and .NET Core 3.0 – Modern Cross-Platform Development: Build applications with C#, .NET Core, Entity Framework Core, ASP.NET Core, and ML.NET using Visual Studio Code, 4th Edition
ASP.NET Core 3 and Angular 9: Full stack web development with .NET Core 3.1 and Angular 9, 3rd Edition
C# in Depth, 4th Edition

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.howtoasp.net”, “www.howtoasp.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”))