-1

I want to ask how can I take the position of last index of char=" | " in R

My string will be similar like below:

I have apple|Orange|banana|perry| in my bag.

Thanks,

PKumar
  • 10,971
  • 6
  • 37
  • 52
Afnan m
  • 11
  • 3
  • I am having a hard time following your question. Did you want "apple|Orange|banana|perry|" to turn into "apple|Orange|banana|perry"? – Sterling Beason May 09 '17 at 16:55
  • Hi , i have many string and each time i want to retern the last index of char("|") , becuase each time will be deffrent position of char ("|") – Afnan m May 09 '17 at 17:29

1 Answers1

3

One option is str_locate

library(stringr)
tail(str_locate_all(str1, "[|]")[[1]], 1)

With stringi, there is a convenient function

library(stringi)
stri_locate_last_fixed(str1, '|')

data

str1 <- "I have apple|Orange|banana|perry| in my bag"
akrun
  • 874,273
  • 37
  • 540
  • 662