Getsuyodev, Programming is mainly used when constructing an application. Programming requires knowledge of application domains, algorithms, and programming language expertise. Programming can be developed in different logic based on developer knowledge.

How to use C# string Compare



C# allows us to

The CSharp String Compare function compares two strings lexicographically. The comparison is based on the Unicode value of each character in the string.

int string.Compare(string str1,string str2)

It returns an Integer indication lexical relationship between the two comprehends

Parameters:

  • string str1: Parameter String
  • string str2: Parameter String

Returns:

Integer: returns less than zero, zero or greater than zero.

  • Less than zero: str1 is less than str2
  • Zero: str1 is equal to str2
  • Greater than zero: str1 is greater than str2
using System;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string str1 = null;
            string str2 = null;

            str1 = "csharp";
            str2 = "CSharp";

            int result = 0;

            result = string.Compare(str1, str2);
            MessageBox.Show(result.ToString());

            result = string.Compare(str1, str2, true);
            MessageBox.Show(result.ToString());
        }
    }
}

When you run this C# program first, you will get -1 in the message box and then 0


Comments are closed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More