Avoid an IndexError when dealing with empty strings

Fixes issue #778.