I have a string:
str1 <- \"This is a string, that I\'ve written
to ask about a question, or at least tried to.\"
How would
This really is an adaptation of Richie Cotton's answer. I hate having to repeat the same function over and over. This approach allows you to feed a vector of terms to match within the string:
str1 <- "This is a string, that I've written to ask about a question,
or at least tried to."
matches <- c(",", "ion")
sapply(matches, function(x) length(gregexpr(x, str1, fixed = TRUE)[[1]]))
# , ion
# 2 1