safeSubstring - exception-free substring method