\HeaderA{strsplit}{Split the Elements of a Character Vector}{strsplit}
\keyword{character}{strsplit}
\begin{Description}\relax
Split the elements of a character vector \code{x} into substrings
according to the presence of substring \code{split} within them.
\end{Description}
\begin{Usage}
\begin{verbatim}
strsplit(x, split, extended = TRUE, fixed = FALSE, perl = FALSE)
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{x}] character vector, each element of which is to be split.  Other
inputs, including a factor, will give an error.

\item[\code{split}] character vector (or object which can be coerced to such)
containing \LinkA{regular expression}{regular expression}(s) (unless \code{fixed = TRUE})
to use as \dQuote{split}.  If empty matches occur, in particular if
\code{split} has length 0, \code{x} is split into single characters.
If \code{split} has length greater than 1, it is re-cycled along
\code{x}.

\item[\code{extended}] logical.  If \code{TRUE}, extended regular expression matching
is used, and if \code{FALSE} basic regular expressions are used.

\item[\code{fixed}] logical.  If \code{TRUE} match string exactly, otherwise
use regular expressions.  Has priority over \code{perl} and
\code{extended}.

\item[\code{perl}] logical.  Should perl-compatible regexps be used?
Has priority over \code{extended}.

\end{ldescription}
\end{Arguments}
\begin{Details}\relax
Argument \code{split} will be coerced to character, so
you will see uses with \code{split = NULL} to mean
\code{split = character(0)}, including in the examples below.

Note that spltting into single characters can be done via
\code{split=character(0)} or \code{split=""}; the two are equivalent
as from \R{} 1.9.0.

A missing value of \code{split} does not split the the corresponding
element(s) of \code{x} at all.

The algorithm applied to each input string is
\begin{alltt}
    repeat \{
        if the string is empty
            break.
        if there is a match
            add the string to the left of the match to the output.
            remove the match and all to the left of it.
        else
            add the string to the output.
            break.
    \}
\end{alltt}
Note that this means that if there is a match at the beginning of a
(non-empty) string, the first element of the output is \code{""}, but
if there is a match at the end of the string, the output is the same
as with the match removed.
\end{Details}
\begin{Value}
A list of length \code{length(x)} the \code{i}-th element of which
contains the vector of splits of \code{x[i]}.
\end{Value}
\begin{Section}{Warning}
The standard regular expression code has been reported to be very slow
when applied to extremely long character strings
(tens of thousands of characters or more): the code used when
\code{perl = TRUE} seems much faster and more reliable for such usages.

The \code{perl = TRUE} option is only implemented for single-byte and
UTF-8 encodings, and will warn if used in a non-UTF-8 multibyte locale.
\end{Section}
\begin{SeeAlso}\relax
\code{\LinkA{paste}{paste}} for the reverse,
\code{\LinkA{grep}{grep}} and \code{\LinkA{sub}{sub}} for string search and
manipulation; further \code{\LinkA{nchar}{nchar}}, \code{\LinkA{substr}{substr}}.

\LinkA{regular expression}{regular expression} for the details of the pattern specification.
\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
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"
## or
unlist(strsplit("a.b.c", ".", fixed = TRUE))

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

## get the first names of the members of R-core
a <- readLines(file.path(R.home("doc"),"AUTHORS"))[-(1:8)]
a <- a[(0:2)-length(a)]
(a <- sub(" .*","", a))
# and reverse them
strReverse(a)

## Note that final empty strings are not produced:
strsplit(paste(c("", "a", ""), collapse="#"), split="#")[[1]]
# [1] ""  "a"
## and also an empty string is only produced before a definite match:
strsplit("", " ")[[1]]    # character(0)
strsplit(" ", " ")[[1]]   # [1] ""
\end{ExampleCode}
\end{Examples}

