def _insert_substr(text: str,
                   substr: str,
                   index: int,
                   cap_before: bool = False,
                   cap_after: bool = False,
                   replace: bool = False) -> str:
    """Insert a substring into the text."""
    before = text[0:index]
    if replace:
        index += 1
    after = text[index:]
    if cap_before:
        before = before.title()
    if cap_after:
        after = after.title()
    return f'{before}{substr}{after}'