intelligent substring method