str_start: Find start and end index of pattern in string

Description Usage Arguments Value Examples

View source: R/str_start_end.R

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

1
2
3
str_start(x, pattern, ignore.case = TRUE, regex = FALSE)

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

Arguments

x

A character vector.

pattern

Character string to be matched in x. pattern might also be a regular-expression object, as returned by stringr::regex(). Alternatively, use regex = TRUE to treat pattern as a regular expression rather than a fixed string.

ignore.case

Logical, whether matching should be case sensitive or not. ignore.case is ignored when pattern is no regular expression or regex = FALSE.

regex

Logical, if TRUE, pattern is treated as a regular expression rather than a fixed string.

Value

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

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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")

x <- c("test1234testagain")
str_start(x, "\\d+4")
str_start(x, "\\d+4", regex = TRUE)
str_end(x, "\\d+4", regex = TRUE)

Example output

[1]  5  8 11
[1]  5  9 13
[1]  6 10 14
[[1]]
[1]  3 10

[[2]]
[1]  5 18

[[1]]
[1] 11

[[2]]
[1] 13

[[1]]
[1] 15

[[2]]
[1] 17

[[1]]
[1] 11 20

[[2]]
[1] 13

[[1]]
[1] 14 23

[[2]]
[1] 16

[1] -1
[1] 5
[1] 8

sjmisc documentation built on Dec. 11, 2021, 9:34 a.m.