[ RDocumentation ](/)

Moon  [ ](https://github.com/datacamp/rdocumentation-2.0) [ Learn R
](https://www.datacamp.com/learn/r)

Search all packages and functions

[ base  (version  3.6.2  ) ](/packages/base/versions/3.6.2)

#  strsplit: Split the Elements of a Character Vector

##  Description

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

##  Usage

    
    
    strsplit(x, split, fixed = FALSE, perl = FALSE, useBytes = FALSE)

##  Arguments

x

character vector, each element of which is to be split. Other inputs,
including a factor, will give an error.

split

character vector (or object which can be coerced to such) containing [ regular
expression ](/link/regular%20expression?package=base&version=3.6.2) (s)
(unless ` fixed = TRUE ` ) to use for splitting. 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 ` .

fixed

logical. If ` TRUE ` match ` split ` exactly, otherwise use regular
expressions. Has priority over ` perl ` .

perl

logical. Should Perl-compatible regexps be used?

useBytes

logical. If ` TRUE ` the matching is done byte-by-byte rather than character-
by-character, and inputs with marked encodings are not converted. This is
forced (with a warning) if any input is found which is marked as ` "bytes" `
(see ` [ Encoding ](/link/Encoding?package=base&version=3.6.2) ` ).

##  Value

A list of the same length as ` x ` , the ` i ` -th element of which contains
the vector of splits of ` x[i] ` .

If any element of ` x ` or ` split ` is declared to be in UTF-8 (see ` [
Encoding ](/link/Encoding?package=base&version=3.6.2) ` ), all non-ASCII
character strings in the result will be in UTF-8 and have their encoding
declared as UTF-8. For ` perl = TRUE, useBytes = FALSE ` all non-ASCII strings
in a multibyte locale are translated to UTF-8.

##  Details

Argument ` split ` will be coerced to character, so you will see uses with `
split = NULL ` to mean ` split = character(0) ` , including in the examples
below.

Note that splitting into single characters can be done _via_ ` split =
character(0) ` or ` split = "" ` ; the two are equivalent. The definition of
‘character’ here depends on the locale: in a single-byte locale it is a byte,
and in a multi-byte locale it is the unit represented by a ‘wide character’
(almost always a Unicode code point).

A missing value of ` split ` does not split the corresponding element(s) of `
x ` at all.

The algorithm applied to each input string is

    
    
        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.
        }
    

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 ` "" ` , but if there is a
match at the end of the string, the output is the same as with the match
removed.

Invalid inputs in the current locale are warned about up to 5 times.

##  See Also

` [ paste ](/link/paste?package=base&version=3.6.2) ` for the reverse, ` [
grep ](/link/grep?package=base&version=3.6.2) ` and ` [ sub
](/link/sub?package=base&version=3.6.2) ` for string search and manipulation;
also ` [ nchar ](/link/nchar?package=base&version=3.6.2) ` , ` [ substr
](/link/substr?package=base&version=3.6.2) ` .

‘ [ regular expression
](/link/regular%20expression?package=base&version=3.6.2) ’ for the details of
the pattern specification.

Option ` PCRE_use_JIT ` controls the details when ` perl = TRUE ` .

##  Examples

[ Run this code
](https://app.datacamp.com/workspace/preview?_tag=rdocs&rdocsPath=packages%2Fbase%2Fversions%2F3.6.2%2Ftopics%2Fstrsplit&utm_source=r-docs&utm