tags:

views:

228

answers:

6

What is the best rewrite of this method to speed it up?

public static bool EndsWith(string line, string term)
{

    bool rb = false;

    int lengthOfTerm = term.Length;

    string endOfString = StringHelpers.RightString(line, lengthOfTerm);

    if (StringHelpers.AreEqual(term, endOfString))
    {
        return true;
    }
    else
    {
        rb = false;
    }

    if (line == term)
    {
        rb = true;
    }

    return rb;

}
+5  A: 

You may want to drop the method rather than rewrite it...

public static bool EndsWith(string line, string term)
{
  return line.EndsWith(term);
}
Matthew Flaschen
+23  A: 

Maybe I am missing the point completely, but I would spontaneously go for the String.EndsWith method.

Fredrik Mörk
+1, you're unlikely to get it any faster than Microsoft can in the core of the language.
paxdiablo
I am currently refactoring some helper methods that I made back in 2002 at which time there was no .EndsWith in C# 1 or I didn't know about it, thanks. Funny it has the same name.
Edward Tanguay
@Edward; whoever wrote that method sure did the homework in the naming department. Should facilitate refactoring, I guess.
Fredrik Mörk
It's been there since the beginning. When making a StringHelpers class, it probably helps to read through String's members.
Matthew Flaschen
And when making an FAQ on condescension, check Matthew Flaschen's comments. :D
Jeff Yates
+3  A: 

Could you use the .NET builtin in string.Endwith() method?

Jake Pearson
+2  A: 

Can't you just use the standard string.EndsWith() function??

Fermin
+2  A: 

Is there any reason you aren't using the build in String.EndsWith method? I imagine that will be the fastest solution most of the time.

Bob
+2  A: 

line.EndsWidth(term)