Introduction:

In this article we will explain how to use string Compare method or what is string Compare method in c# with example.

Description:

In c# programming language Compare() method is a string method, the Compare method is used to compares two string and returns an integer value. The return value of Compare method can be less than zero, greater than zero or equals to zero.  

 

Following table lists the values which will be return by the Compare method after comparing the two specified strings.

Value

Meaning

Less than zero

When first string is less than second.

Zero

When both strings are equal.

Greater than zero

When first string is greater than zero.

 

C# Compare Method Example

Following is the example of using Compare() method to compare a given two strings and return an integer value in c# programming language.

 

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace SQLTutorialsPoint
{
    class Program
    {
        static void Main(string[] args)
        {
            string str1 = "QQQ";
            string str2 = "DDD";
            int result = String.Compare(str1, str2);
            Console.WriteLine("First result:" + result.ToString());
            string str3 = "UUU";
            result = String.Compare(str1, str3);
            Console.WriteLine("Second result:" + result.ToString());
            string str4 = "UUU";
            result = String.Compare(str3, str4);
            Console.WriteLine("Third result:" + result.ToString());
            Console.WriteLine("\nPress Enter Key to Exit..");
            Console.ReadLine();
        }
    }
}

 

As you see above example, we used a Compare() method to compare a two given strings and return an integer value.

 

When we execute above c# program, we will get the result like as shown below.


 How to use C# String Compare Method 

 

In this article we have learned, how we can use Compare() method to compare two given strings and return an integer value which indicates a lexical relationship between two strings in c# programming language.

 

I hope it will help to you after reading.

0 comments


Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

you may also like