stringi (version 0.3-1)

stri_startswith: Determine if the Start or End of a String Matches a Pattern

Description

These functions check if a string starts or ends with a pattern occurrence.

Usage

stri_startswith(str, ..., fixed, coll, charclass)

stri_endswith(str, ..., fixed, coll, charclass)

stri_startswith_fixed(str, pattern, from = 1L)

stri_endswith_fixed(str, pattern, to = -1L)

stri_startswith_charclass(str, pattern, from = 1L)

stri_endswith_charclass(str, pattern, to = -1L)

stri_startswith_coll(str, pattern, from = 1L, opts_collator = NULL)

stri_endswith_coll(str, pattern, to = -1L, opts_collator = NULL)

Arguments

str
character vector
...
additional arguments passed to the underlying functions; stri_startswith and stri_endswith only
pattern,fixed,coll,charclass
character vector defining search patterns; for more details refer to stringi-search
from
integer vector
to
integer vector
opts_collator
a named list with ICU Collator's settings as generated with stri_opts_collator; NULL for default settings; stri_*_coll only

Value

  • All the functions return a logical vector.

Details

Vectorized over str, pattern, and from or to.

If pattern is empty, then the result is NA and a warning is generated.

Argument start controls the start position in str at which the pattern is matched. On the other hand, to gives the end position.

Indices given by from or to are 1-based, i.e. an index equal to 1 denotes the first character in a string, which gives a typical R look-and-feel.

For negative indices in from or to, counting starts at the end of the string. E.g. index -1 denotes the last code point in the string.

If you would like to test for a pattern match at any position in str, use stri_detect.

stri_startswith and stri_endswith are convenience functions. They call either stri_*_fixed, stri_*_coll, or stri_*_charclass, depending on the argument used. Unless you are a very lazy person, please refer to the underlying functions directly for better performance.

Note that testing for a pattern match at the start or end of a string has not been implemented separately for regex patterns. For that you may use the "^" and "$" metacharacters, see stringi-search-regex.

See Also

Other search_detect: stri_detect, stri_detect_charclass, stri_detect_coll, stri_detect_fixed, stri_detect_regex; stringi-search

Examples

Run this code
stri_startswith_charclass(" trim me! ", "\\p{WSpace}")
stri_startswith_fixed(c("a1", "a2", "b3", "a4", "c5"), "a")
stri_detect_regex(c("a1", "a2", "b3", "a4", "c5"), "^a")
stri_startswith_fixed("ababa", "ba")
stri_startswith_fixed("ababa", "ba", from=2)
stri_startswith_coll(c("a1", "A2", "b3", "A4", "C5"), "a",
   opts_collator=stri_opts_collator(strength=1))
pat <- stri_paste("\u0635\u0644\u0649 \u0627\u0644\u0644\u0647 ",
                  "\u0639\u0644\u064a\u0647 \u0648\u0633\u0644\u0645XYZ")
stri_endswith_coll("\ufdfa\ufdfa\ufdfaXYZ", pat,
   opts=stri_opts_collator(strength = 1))

Run the code above in your browser using DataLab