
Last chance! 50% off unlimited learning
Sale ends in
Determines if a string starts or ends with a match to a specified fixed pattern.
startsWith(
x,
pattern = prefix,
...,
ignore_case = ignore.case,
fixed = TRUE,
ignore.case = FALSE,
prefix
)endsWith(
x,
pattern = suffix,
...,
ignore_case = ignore.case,
fixed = TRUE,
ignore.case = FALSE,
suffix
)
Each function returns a logical vector, indicating whether a pattern match has been detected or not. They preserve the attributes of the longest inputs (unless they are dropped due to coercion).
character vector whose elements are to be examined
character vector with patterns to search for
further arguments to stri_startswith
and stri_endswith
, e.g., locale
single logical value; indicates whether matching should be case-insensitive
single logical value;
TRUE
for fixed pattern matching
(see about_search_fixed);
NA
for the Unicode collation algorithm
(about_search_coll);
FALSE
is not supported -- use grepl
instead
alias to the ignore_case
argument [DEPRECATED]
aliases to the pattern
argument [DEPRECATED]
Replacements for base startsWith
and endsWith
implemented with stri_startswith
and stri_endswith
.
there are inconsistencies between the argument order and naming
in grepl
, strsplit
,
and startsWith
(amongst others); e.g.,
where the needle can precede the haystack, the use of the forward
pipe operator, |>
, is less convenient
[fixed here]
grepl
also features the ignore.case
argument
[added here]
partial recycling without the usual warning [fixed here]
no attributes preserved whatsoever [fixed here]
not suitable for natural language processing
[fixed here -- use fixed=NA
]
These functions are fully vectorised with respect to both arguments.
For matching with regular expressions, see grepl
with patterns like "^prefix"
and "suffix$"
.
The official online manual of stringx at https://stringx.gagolewski.com/
startsWith("ababa", c("a", "ab", "aba", "baba", NA))
outer(
c("aba", "abb", "abc", "baba", "bac"),
c("A", "B", "C"),
endsWith,
ignore_case=TRUE
)
x <- c("Mario", "mario", "M\u00E1rio", "M\u00C1RIO", "Mar\u00EDa", "Rosario")
x[startsWith(x, "mario", ignore_case=TRUE)]
x[startsWith(x, "mario", fixed=NA, strength=1L)]
Run the code above in your browser using DataLab