Per above comment, I think this is easier after you've parsed the URI. You don't need to do the substringing stuff that it's already doing for you.