String Compareordinal()

The CompareOrdinal method in C# compares two specified String instances by assessing the numerical values of the corresponding characters in each string.

If the two strings are identical, the function will return a value of 0. In case the first string is larger than the second string, a positive number representing the difference will be returned; otherwise, a negative number indicating the difference will be returned.

Example

s1==s2 returns 0
s1>s2 returns positive number in difference
s1<s2 returns negative number in difference

Signature

Example

public static int CompareOrdinal(String first, String second)
public static int CompareOrdinal(String, Int32, String, Int32, Int32)

Parameters

The initial argument denotes a string that will be compared with a second string.

The second argument denotes the string that will be compared with the first string.

Return

It returns an integer value.

C# String CompareOrdinal Method Example

Example

using System;  
    public class StringExample  
    {  
        public static void Main(string[] args)  
        {  
            string s1 = "hello";  
            string s2 = "hello";  
            string s3 = "csharp";
            string s4 = "mello";
      
            Console.WriteLine(string.CompareOrdinal(s1,s2)); 
            Console.WriteLine(string.CompareOrdinal(s1,s3)); 
            Console.WriteLine(string.CompareOrdinal(s1,s4)); 
        }  
    }

Output:

Output

0
5
-5

Input Required

This code uses input(). Please provide values below: