How to split a string from right-to-left, like Python's rsplit()?

Viewed 2841

Suppose a vector:

xx.1 <- c("zz_ZZ_uu_d", "II_OO_d")

I want to get a new vector splitted from right most and only split once. The expected results would be:

c("zz_ZZ_uu", "d", "II_OO", "d").

It would be like python's rsplit() function. My current idea is to reverse the string, and split the with str_split() in stringr.

Any better solutions?

update
Here is my solution returning n splits, depending on stringr and stringi. It would be nice that someone provides a version with base functions.

rsplit <- function (x, s, n) {
  cc1 <- unlist(stringr::str_split(stringi::stri_reverse(x), s, n))
  cc2 <- rev(purrr::map_chr(cc1, stringi::stri_reverse))
  return(cc2)
}
5 Answers
Related