Skip to contents

strs_count counts the number of times a specified substring occurs in each element of a character vector. Optionally, the search can be limited to a substring of each element, specified by start and end positions. This function is similar to Python's str.count() method.

Usage

strs_count(string, substring, start = 1L, end = -1L)

Arguments

string

A character vector where each element is a string in which to count occurrences of substring.

substring

The substring to count within each element of string.

start

An optional integer specifying the starting position in each element of string for the search. Defaults to 1, indicating the start of the string.

end

An optional integer specifying the ending position in each element of string for the search. The default value of -1 indicates the end of the string.

Value

An integer vector of the same length as string, with each element indicating the count of substring in the corresponding element of string.

Examples

strs_count("hello world", "o")
#> [1] 2
strs_count("banana", "na")
#> [1] 2
strs_count("hello world", "o", start = 6)
#> [1] 1
strs_count("hello world", "o", end = 5)
#> [1] 1