Home » .Net

Compare strings using Equals() method in c#

Comparing two strings using String.Equals() in C#: Here, we will learn with example, how we can compare two strings using String.Equals() Method in C#.Net?

Given two strings and we have to compare them using String.Equals() method in C#.Net.

String.Equals() Method

It is a method of string class, which is used to compare strings and return either true if they are equal or false, if they are not equal.

Syntax:

bool str.Equals(string str);

Return values:

  • True - if strings matche.
  • False - if strings do not matche.

C# program to compare two strings using C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            string str = "Hello";

            if (str.Equals("Hello"))
            {
                Console.WriteLine("Strings is matched");
            }
            else
            {
                Console.WriteLine("String is not matched");
            }


            if (str.Equals("Hiii"))
            {
                Console.WriteLine("String is matched");
            }
            else
            {
                Console.WriteLine("String is not matched");
            }
        }
    }
}

Output

String is matched
String is not matched




Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.