views:

100

answers:

1

I am pretty new to C# and I am pretty sure this function can be radically improved:

public static Boolean SuffixExists(String strWhole, String sufx)
    {
        int iLen = sufx.Length;
        if (iLen > 0)
        {
            String s;
            s = strWhole.Substring(strWhole.Length - iLen, iLen);
            if (sufx != s) return false;
            else
                s = null;
            return true;
        }
        return false;
    }

I am going to be calling this function from within a foreach loop. If the passed substring is present as the suffix of the passed whole string, then I will want to alter the whole string passed here by adding a prefix. I did a little testing on this code but I know it is very ugly. I'm using .Net 3.5 and Visual Studio 2008.

+8  A: 

return strWhole.EndsWith( sufx );

Paul Alexander
A case where a little extra familiarity with your framework comes in very handy...
Eric King