hello,
i was just wondering as to what is the major difference between the Endswith() and Equals() method in .net (c#) ?
I wrote the following code for string comparison n also for numerals by using Endswith method however even if i use Equals method i get the same output ! can ne one plz guide me on this...as to when to use Endswith() and when to use Equals() ??

private void btncompare_Click(object sender, EventArgs e)
        {
            string str = str1.Text.Trim();
            string strcmp = str2.Text.Trim();
            if (str.EndsWith(strcmp))
                
            {
                MessageBox.Show("Strings are Equal");
            }
            else
            {
                MessageBox.Show("Strings are not Equal");

            }

           THANK YOU,
ROHAN

Recommended Answers

All 2 Replies

Equals compares the exact strings: "abc123" == "abc123"

EndsWith compares the string ending, such as: "abc123".EndsWith("123") returns true, where "abc123".EndsWith("abc") returns false.

In your case the two strings are equal. "abc123" does end with "abc123", in fact it equals that string -- thus you get the same result.

commented: Right (Y) +8

Thanks a lot for ur prompt reply !! Finally got it ! thanks once again :)

Cya
ROHAN

Equals compares the exact strings: "abc123" == "abc123"

EndsWith compares the string ending, such as: "abc123".EndsWith("123") returns true, where "abc123".EndsWith("abc") returns false.

In your case the two strings are equal. "abc123" does end with "abc123", in fact it equals that string -- thus you get the same result.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.