safe substring method