Better string.substring implementation