Extract and replace substrings from a character vector.
str_sub will recycle all arguments to be the same length as the longest argument. If any arguments are of length 0, the output will be a zero length character vector.
Two integer vectors. start gives the position of the first character (defaults to first), end gives the position of the last (defaults to last character). Alternatively, pass a two-column matrix to start.
Negative values count backwards from the last character.
Single logical value. If TRUE, missing values in any of the arguments provided will result in an unchanged input.
A character vector of substring from start to end (inclusive). Will be length of longest input argument.
Substrings are inclusive - they include the characters at both start and end positions. str_sub(string, 1, -1) will return the complete substring, from the first character to the last.
#>  "a" "e" "i" "a"
#>   "Hadley Wickham" "adley Wickham"  "dley Wickham"   "ley Wickham"
#>   "ey Wickham"     "y Wickham"      " Wickham"       "Wickham"
#>   "ickham"         "ckham"          "kham"           "ham"
#>   "H"              "Ha"             "Had"            "Hadl"
#>   "Hadle"          "Hadley"         "Hadley "        "Hadley W"
#>   "Hadley Wi"      "Hadley Wic"     "Hadley Wick"    "Hadley Wickh"
#>  "Hadley Wickha"  "Hadley Wickham"
x1 <- x2 <- x3 <- x4 <- "AAA"
str_sub(x1, 1, NA) <- "B"
str_sub(x3, 1, NA, omit_na = TRUE) <- "B"