R: Find start and end index of pattern in string

str_start {sjmisc} | R Documentation

## Find start and end index of pattern in string

### Description

`str_start()` finds the beginning position of `pattern` in each element of `x`, while `str_end()` finds the stopping position of `pattern` in each element of `x`.

### Usage

    str_start(x, pattern, ignore.case = TRUE)

    str_end(x, pattern, ignore.case = TRUE)

### Arguments

`x` | A character vector.
`pattern` | Character string to be matched in `x`. `pattern` might also be a regular-expression object, as returned by `regex`, or any of stringr's supported `modifiers`.
`ignore.case` | Logical, whether matching should be case sensitive or not.

### Value

A numeric vector with index of start/end position(s) of `pattern` found in `x`, or an empty vector, if `pattern` was not found in `x`.

### Examples

    path <- "this/is/my/fileofinterest.csv"
    str_start(path, "/")

    path <- "this//is//my//fileofinterest.csv"
    str_start(path, "//")
    str_end(path, "//")

    x <- c("my_friend_likes me", "your_friend likes_you")
    str_start(x, "_")

    # pattern "likes" starts at position 11 in first, and
    # position 13 in second string
    str_start(x, "likes")

    # pattern "likes" ends at position 15 in first, and
    # position 17 in second string
    str_end(x, "likes")

    x <- c("I like to move it, move it", "You like to move it")
    str_start(x, "move")
    str_end(x, "move")

[Package sjmisc version 2.7.5 Index]