Locate the position of patterns in a string.

Vectorised over string and pattern. If the match is of length 0, (e.g. from a special match like $) end will be one character less than start.

str_locate(string, pattern)
str_locate_all(string, pattern)
Input vector. Either a character vector, or something coercible to one.
Pattern to look for.

The default interpretation is a regular expression, as described in stringi-search-regex. Control options with regex().

Match a fixed string (i.e. by comparing only bytes), using fixed(x). This is fast, but approximate. Generally, for matching human text, you'll want coll(x) which respects character matching rules for the specified locale.

Match character, word, line and sentence boundaries with boundary(). An empty pattern, "", is equivalent to boundary("character").


For str_locate, an integer matrix. First column gives start postion of match, and second column gives end position. For str_locate_all a list of integer matrices.

See Also

str_extract for a convenient way of extracting matches, stri_locate for the underlying implementation.

  • str_locate
  • str_locate_all
library(stringr) fruit <- c("apple", "banana", "pear", "pineapple") str_locate(fruit, "$") str_locate(fruit, "a") str_locate(fruit, "e") str_locate(fruit, c("a", "b", "p", "p")) str_locate_all(fruit, "a") str_locate_all(fruit, "e") str_locate_all(fruit, c("a", "b", "p", "p")) # Find location of every character str_locate_all(fruit, "")
Documentation reproduced from package stringr, version 1.1.0, License: GPL-2

Community examples

Looks like there are no examples yet.