compare strings c# code example

Example 1: string comparison in c#

SYNTAX:
string.Compare(str1, str2);

RULE:
s1==s2 returns 0  
s1>s2 returns 1  
s1<s2 returns -1 

EXAMPLE:
using System;    
public class StringExample{    
	public static void Main(string[] args){    
        string s1 = "hello";    
        string s2 = "hello";    
        string s3 = "csharp";
        
        Console.WriteLine(string.Compare(s1,s2));   //0
        Console.WriteLine(string.Compare(s2,s3));  	//1
    }    
}

Example 2: compare text c#

string str1 = "London";
string str2 = "London";
 
str1 == str2; // true
str1.Equals(str2); // true

Example 3: c# string.compare

public static int Compare (string? strA, string? strB);
                                                                                 
//Less than zero strA precedes strB in the sort order
//zero strA occurs in the same position as strB in the sort order
//Greater than zero strA follows strB in the sort order