Determines if entries of x
start with a string prefix
,
where strings are recycled to common lengths.
startsWith(x, prefix, trim=FALSE, ignore.case=FALSE)
A logical vector, of “common length” of x
and
prefix
, i.e., of the longer of the two lengths unless one of
them is zero when the result is also of zero length. A shorter input
is recycled to the output length.
character vector whose “starts” are considered.
character vector, typicall of length one, i.e., a string.
whether leading and trailing spaces should be removed from
x
before testing for a match.
whether case should be ignored when testing for a match.
Gregory R. Warnes greg@warnes.net
startsWith
for the 'base' package implementation,
grepl
, substring
## Simple example
startsWith("Testing", "Test")
## Vector examples
s <- c("Testing", " Testing", "testing", "Texting")
names(s) <- s
startsWith(s, "Test") # " Testing", "testing", and "Texting" do not match
startsWith(s, "Test", trim=TRUE) # Now " Testing" matches
startsWith(s, "Test", ignore.case=TRUE) # Now "testing" matches
# Comparison
# gdata
startsWith(s, "Test", trim=TRUE)
startsWith(s, "Test", ignore.case=TRUE)
# base
startsWith(trimws(s), "Test")
startsWith(tolower(s), tolower("Test"))
Run the code above in your browser using DataLab