stringr (version 0.6.1)

str_sub: Extract substrings from a character vector.

Description

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.

Usage

str_sub(string, start = 1L, end = -1L)

Arguments

string
input character vector.
start
integer vector giving position of first charater in substring, defaults to first character. If negative, counts backwards from last character.
end
integer vector giving position of last character in substring, defaults to last character. If negative, counts backwards from last character.

Value

  • character vector of substring from start to end (inclusive). Will be length of longest input argument.

Details

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.

See Also

substring which this function wraps, and link{str_sub_replace} for the replacement version

Examples

Run this code
hw <- "Hadley Wickham"

str_sub(hw, 1, 6)
str_sub(hw, end = 6)
str_sub(hw, 8, 14)
str_sub(hw, 8)
str_sub(hw, c(1, 8), c(6, 14))

str_sub(hw, -1)
str_sub(hw, -7)
str_sub(hw, end = -7)

str_sub(hw, seq_len(str_length(hw)))
str_sub(hw, end = seq_len(str_length(hw)))

Run the code above in your browser using DataCamp Workspace