To compare strings in C#, use the compare() method. It compares two strings and returns the following integer values −
If str1 is less than str2, it returns -1. If str1 is equal to str2, it returns 0. If str1 is greater than str2, it returns 1.
Set the two strings in the String.compare() method and compare them −
string.Compare(string1, string2);
Example
You can try to run the following code to compare two strings in C#.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Demo {
class MyApplication {
static void Main(string[] args) {
string string1 = null;
string string2 = null;
string1 = "amit";
string2 = "Amit";
int myOutput = 0;
myOutput = string.Compare(string1, string2);
Console.WriteLine(myOutput.ToString());
Console.ReadLine();
}
}
}Output
-1