Aliases: str_sub str_sub<- str_sub_all
Keywords:
### ** Examples hw <- "Hadley Wickham" str_sub(hw, 1, 6)
[1] "Hadley"
str_sub(hw, end = 6)
[1] "Hadley"
str_sub(hw, 8, 14)
[1] "Wickham"
str_sub(hw, 8)
[1] "Wickham"
# Negative indices index from end of string str_sub(hw, -1)
[1] "m"
str_sub(hw, -7)
[1] "Wickham"
str_sub(hw, end = -7)
[1] "Hadley W"
# str_sub() is vectorised by both string and position str_sub(hw, c(1, 8), c(6, 14))
[1] "Hadley" "Wickham"
# if you want to extract multiple positions from multiple strings, # use str_sub_all() x <- c("abcde", "ghifgh") str_sub(x, c(1, 2), c(2, 4))
[1] "ab" "hif"
str_sub_all(x, start = c(1, 2), end = c(2, 4))
[[1]] [1] "ab" "bcd" [[2]] [1] "gh" "hif"
# Alternatively, you can pass in a two column matrix, as in the # output from str_locate_all pos <- str_locate_all(hw, "[aeio]")[[1]] pos
start end [1,] 2 2 [2,] 5 5 [3,] 9 9 [4,] 13 13
str_sub(hw, pos)
[1] "a" "e" "i" "a"
# You can also use `str_sub()` to modify strings: x <- "BBCDEF" str_sub(x, 1, 1) <- "A"; x
[1] "ABCDEF"
str_sub(x, -1, -1) <- "K"; x
[1] "ABCDEK"
str_sub(x, -2, -2) <- "GHIJ"; x
[1] "ABCDGHIJK"
str_sub(x, 2, -2) <- ""; x
[1] "AK"