Fix Str::startsWith() end endsWith() with empty needles 

Follow-up to #2627.

Also includes a code optimization.