0

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
2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by laghaterohan
1

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.

Votes + Comments
Right (Y)
0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.