Learn R Programming

filesstrings (version 1.1.0)

str_after_nth: Text before or after \(n\)th occurrence of pattern.

Description

Extract the part of a string which is before or after the \(n\)th occurrence of a specified pattern, vectorised over the string. One can also choose \(n\) to be the last occurrence of the pattern. See argument n.

Usage

str_after_nth(strings, pattern, n = 1)

str_before_nth(strings, pattern, n = 1)

Arguments

strings

A character vector.

pattern

A regular expression.

n

A natural number to identify the \(n\)th 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.

Value

A character vector of the desired strings.

Examples

Run this code
# NOT RUN {
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)
# }

Run the code above in your browser using DataLab