Search code examples
rstrsplit

Using strsplit with multiple separators


A character string of interest can either be 'there are five apples' or 'there are five APPLES'

  strsplit(string, c('apples', 'APPLES'))

So I want to split by either apples or APPLES because I don't know if the string is going to have lower case or uppercase letters. But I tried the code above and it didn't work.


Solution

  • You could use the following which splits on case-insensitive "apples".

    x <- 'there are five APPLES in this case'
    unlist(strsplit(x, '(?i:apples)', perl=T))
    # [1] "there are five " " in this case"