Skip to contents

strs_slice extracts substrings from each element of a character vector, specified by start and stop positions. It is similar to Python's slicing syntax for strings, but it uses 1 indexing and stops are inclusive.

Usage

strs_slice(string, start = 1L, stop = -1L, ..., step = 1L)

Arguments

string

A character vector where each element is a string to slice.

start

An integerish scalar for the starting position for slicing (inclusive).

stop

An integerish scalar for the ending position for slicing (inclusive).

...

Used to force keyword argument usage of step.

step

An integer greater than 0 or equal to -1 for the step size. If -1 is provided, each string will be reversed after slicing operations.

Value

A character vector of the same length as string, with each element being the sliced substring.

Examples

strs_slice("hello world", 1, 5)
#> [1] "hello"
strs_slice("hello world", 7)
#> [1] "world"
strs_slice("hello world", start = 7, stop = 11)
#> [1] "world"