1#' Check the prefix of a string.
2#'
3#' Return \code{TRUE} if the string \code{str} starts with the specified
4#' \code{prefix}, otherwise return \code{FALSE}.
5#'
6#' @details
7#' With optional \code{start}, test string beginning at that position.
8#' With optional \code{end}, stop comparing string at that position.
9#'
10#' @param str A character vector.
11#' @param prefix A character vector.
12#' @param start A numeric vector.
13#' @param end A numeric vector.
14#'
15#' @return A logical vector.
16#'
17#' @references \url{https://docs.python.org/3/library/stdtypes.html#str.startswith}
18#'
19#' @seealso \code{\link{pystr_endswith}}
20#'
21#' @examples
22#' pystr_startswith("www.example.com", "www.")
23#' pystr_startswith("example.com", "www.")
24#' pystr_startswith("www.example.com", "example", 5)
25#'
26#' @export
27pystr_startswith <- function(str, prefix, start=1, end=nchar(str)) {
28  return(mapply(pystr_startswith_, str, prefix, start, end, USE.NAMES=FALSE))
29}
30
31pystr_startswith_ <- function(str, prefix, start, end) {
32  string_to_check = substr(str, start, end)
33  start_check = 1
34  end_check = nchar(prefix)
35  letters_to_check = substr(string_to_check, start_check, end_check)
36  return(letters_to_check == prefix)
37}
38