Improved space handling in HasPrefix and HasSuffix