Learn R Programming

gdata (version 3.0.1)

startsWith: Does String Start or End With Another String?

Description

Determines if entries of x start with a string prefix, where strings are recycled to common lengths.

Usage

startsWith(x, prefix, trim=FALSE, ignore.case=FALSE)

Value

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.

Arguments

x

character vector whose “starts” are considered.

prefix

character vector, typicall of length one, i.e., a string.

trim

whether leading and trailing spaces should be removed from x before testing for a match.

ignore.case

whether case should be ignored when testing for a match.

Author

Gregory R. Warnes greg@warnes.net

See Also

startsWith for the 'base' package implementation, grepl, substring

Examples

Run this code
## 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