The R Project SVN R

Rev

Rev 68948 | Rev 82587 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
42333 ripley 1
% File src/library/base/man/strsplit.Rd
68948 ripley 2
% Part of the R package, https://www.R-project.org
72247 ripley 3
% Copyright 1995-2017 R Core Team
42333 ripley 4
% Distributed under GPL 2 or later
5
 
2 r 6
\name{strsplit}
56186 murdoch 7
\alias{strsplit}
12778 pd 8
\title{Split the Elements of a Character Vector}
2 r 9
\description{
12778 pd 10
  Split the elements of a character vector \code{x} into substrings
49658 ripley 11
  according to the matches to substring \code{split} within them.
2 r 12
}
27085 ripley 13
\usage{
50266 ripley 14
strsplit(x, split, fixed = FALSE, perl = FALSE, useBytes = FALSE)
27085 ripley 15
}
2395 maechler 16
\arguments{
27085 ripley 17
  \item{x}{
36073 ripley 18
    character vector, each element of which is to be split.  Other
19
    inputs, including a factor, will give an error.
27085 ripley 20
  }
21
  \item{split}{
36073 ripley 22
    character vector (or object which can be coerced to such)
23
    containing \link{regular expression}(s) (unless \code{fixed = TRUE})
42961 ripley 24
    to use for splitting.  If empty matches occur, in particular if
36073 ripley 25
    \code{split} has length 0, \code{x} is split into single characters.
26
    If \code{split} has length greater than 1, it is re-cycled along
27
    \code{x}.
27085 ripley 28
  }
29
  \item{fixed}{
43233 ripley 30
    logical.  If \code{TRUE} match \code{split} exactly, otherwise
53883 maechler 31
    use regular expressions.  Has priority over \code{perl}.
27085 ripley 32
  }
66332 hornik 33
  \item{perl}{logical.  Should Perl-compatible regexps be used?}
49658 ripley 34
  \item{useBytes}{logical.  If \code{TRUE} the matching is done
35
    byte-by-byte rather than character-by-character, and inputs with
54753 ripley 36
    marked encodings are not converted.  This is forced (with a warning)
66872 ripley 37
    if any input is found which is marked as \code{"bytes"}
38
    (see \code{\link{Encoding}}).}
2395 maechler 39
}
27084 ripley 40
\details{
36073 ripley 41
  Argument \code{split} will be coerced to character, so
27085 ripley 42
  you will see uses with \code{split = NULL} to mean
43
  \code{split = character(0)}, including in the examples below.
27084 ripley 44
 
40284 ripley 45
  Note that splitting into single characters can be done \emph{via}
49666 ripley 46
  \code{split = character(0)} or \code{split = ""}; the two are
55555 ripley 47
  equivalent.  The definition of \sQuote{character} here depends on the
49666 ripley 48
  locale: in a single-byte locale it is a byte, and in a multi-byte
49
  locale it is the unit represented by a \sQuote{wide character} (almost
65045 ripley 50
  always a Unicode code point).
53883 maechler 51
 
39745 murdoch 52
  A missing value of \code{split} does not split the corresponding
27085 ripley 53
  element(s) of \code{x} at all.
37822 ripley 54
 
55
  The algorithm applied to each input string is
61983 ripley 56
\preformatted{    repeat \{
47616 ripley 57
        if the string is empty
58
            break.
59
        if there is a match
60
            add the string to the left of the match to the output.
61
            remove the match and all to the left of it.
62
        else
63
            add the string to the output.
64
            break.
37822 ripley 65
    \}
55195 ripley 66
}
37822 ripley 67
  Note that this means that if there is a match at the beginning of a
68
  (non-empty) string, the first element of the output is \code{""}, but
69
  if there is a match at the end of the string, the output is the same
53883 maechler 70
  as with the match removed.
65334 ripley 71
 
72
  Invalid inputs in the current locale are warned about up to 5 times.
27084 ripley 73
}
49666 ripley 74
\value{
75
  A list of the same length as \code{x}, the \code{i}-th element of which
76
  contains the vector of splits of \code{x[i]}.
77
 
78
  If any element of \code{x} or \code{split} is declared to be in UTF-8
52340 murdoch 79
  (see \code{\link{Encoding}}), all non-ASCII character strings in the
65147 ripley 80
  result will be in UTF-8 and have their encoding declared as UTF-8.
81
  For \code{perl = TRUE, useBytes = FALSE} all non-ASCII strings in a
82
  multibyte locale are translated to UTF-8.
28442 ripley 83
}
50269 ripley 84
 
2 r 85
\seealso{
7125 hornik 86
  \code{\link{paste}} for the reverse,
87
  \code{\link{grep}} and \code{\link{sub}} for string search and
49666 ripley 88
  manipulation; also \code{\link{nchar}}, \code{\link{substr}}.
26760 ripley 89
 
43585 ripley 90
  \sQuote{\link{regular expression}} for the details of the pattern
91
  specification.
72247 ripley 92
 
93
  Option \code{PCRE_use_JIT} controls the details when \code{perl = TRUE}.
2 r 94
}
95
\examples{
2395 maechler 96
noquote(strsplit("A text I want to display with spaces", NULL)[[1]])
97
 
24343 maechler 98
x <- c(as = "asfef", qu = "qwerty", "yuiop[", "b", "stuff.blah.yech")
2395 maechler 99
# split x on the letter e
49658 ripley 100
strsplit(x, "e")
7826 hornik 101
 
102
unlist(strsplit("a.b.c", "."))
103
## [1] "" "" "" "" ""
25118 hornik 104
## Note that 'split' is a regexp!
105
## If you really want to split on '.', use
59759 ripley 106
unlist(strsplit("a.b.c", "[.]"))
7826 hornik 107
## [1] "a" "b" "c"
27085 ripley 108
## or
109
unlist(strsplit("a.b.c", ".", fixed = TRUE))
18813 maechler 110
 
111
## a useful function: rev() for strings
112
strReverse <- function(x)
61153 ripley 113
        sapply(lapply(strsplit(x, NULL), rev), paste, collapse = "")
18813 maechler 114
strReverse(c("abc", "Statistics"))
115
 
27124 ripley 116
## get the first names of the members of R-core
36704 ripley 117
a <- readLines(file.path(R.home("doc"),"AUTHORS"))[-(1:8)]
25030 ripley 118
a <- a[(0:2)-length(a)]
27124 ripley 119
(a <- sub(" .*","", a))
120
# and reverse them
121
strReverse(a)
37810 ripley 122
 
123
## Note that final empty strings are not produced:
124
strsplit(paste(c("", "a", ""), collapse="#"), split="#")[[1]]
125
# [1] ""  "a"
126
## and also an empty string is only produced before a definite match:
127
strsplit("", " ")[[1]]    # character(0)
128
strsplit(" ", " ")[[1]]   # [1] ""
2 r 129
}
286 maechler 130
\keyword{character}