R: Text before or after nth occurrence of pattern.

str_after_nth {filesstrings} | R Documentation

## Text before or after nth occurrence of pattern.

### Description

Extract the part of a string which is before or after the nth occurrence of a specified pattern, vectorized over the string. `n` can be negatively indexed. See 'Arguments'.

### Usage

    str_after_nth(strings, pattern, n)

    str_after_first(strings, pattern)

    str_after_last(strings, pattern)

    str_before_nth(strings, pattern, n)

    str_before_first(strings, pattern)

    str_before_last(strings, pattern)

### Arguments

`strings` | A character vector.
`pattern` | A character vector. Pattern(s) specified like the pattern(s) in the stringr package (e.g. look at `stringr::str_locate()`). If this has length >1 its length must be the same as that of `string`.
`n` | A natural number to identify the nth occurrence (defaults to first (`n = 1`)). This can be negatively indexed, so if you wish to select the last occurrence, you need `n = -1`, for the second-last, you need `n = -2` and so on.

### Details

- `str_after_first(...)` is just `str_after_nth(..., n = 1)`.

- `str_after_last(...)` is just `str_after_nth(..., n = -1)`.

- `str_before_first(...)` is just `str_before_nth(..., n = 1)`.

- `str_before_last(...)` is just `str_before_nth(..., n = -1)`.

### Value

A character vector of the desired strings.

### Examples

    string <- "ab..cd..de..fg..h"
    str_after_nth(string, "\\.\\.", 3)
    str_before_nth(string, "e", 1)
    str_before_nth(string, "\\.", -3)
    str_before_nth(string, ".", -3)
    str_before_nth(rep(string, 2), fixed("."), -3)

[Package filesstrings version 2.5.0 Index]