strsplit {base} | R Documentation

## Split the Elements of a Character Vector

### Description

Split the elements of a character vector `x` into substrings according to the presence of substring `split` within them.

### Usage

    strsplit(x, split, extended = TRUE)

### Arguments

`x` | character vector, to be split.
`split` | character vector containing a regular expression to use as “split”. If empty matches occur, in particular if `split` has length 0, `x` is split into single characters. If `split` has length greater than 1, it is re-cycled along `x`.
`extended` | if `TRUE`, extended regular expression matching is used, and if `FALSE` basic regular expressions are used.

### Value

A list of length `length(x)` the `i`-th element of which contains the vector of splits of `x[i]`.

### See Also

`paste` for the reverse, `grep` and `sub` for string search and manipulation; further `nchar`, `substr`.

### Examples

    noquote(strsplit("A text I want to display with spaces", NULL)[[1]])

    x <- c(as = "asfef", qu = "qwerty", "yuiop[", "b", "stuff.blah.yech")
    # split x on the letter e
    strsplit(x,"e")

    unlist(strsplit("a.b.c", "."))
    ## [1] "" "" "" "" ""
    ## Note that 'split' is a regexp!
    ## If you really want to split on '.', use
    unlist(strsplit("a.b.c", "\\."))
    ## [1] "a" "b" "c"

    ## a useful function: rev() for strings
    strReverse <- function(x)
            sapply(lapply(strsplit(x,NULL), rev), paste, collapse="")
    strReverse(c("abc", "Statistics"))

    a <- readLines(file.path(R.home(),"AUTHORS"))[-(1:8)]
    a <- a[(0:2)-length(a)]
    sub("\t.*","", a)
    strReverse(sub(" .*","", a))

[Package Contents]