Fix the infinite loop problem when string is empty.