Safe way to obtain string offsets in 'for' statement