Prevent throwing StringIndexOutOfBoundsException