Avoid StringIndexOutOfBoundsException