The R Project SVN R

Rev

Rev 61153 | Rev 65045 | 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
2
% Part of the R package, http://www.R-project.org
59039 ripley 3
% Copyright 1995-2011 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
  }
53883 maechler 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)
37
    if any input is found which is marked as \code{"bytes"}.}
2395 maechler 38
}
27084 ripley 39
\details{
36073 ripley 40
  Argument \code{split} will be coerced to character, so
27085 ripley 41
  you will see uses with \code{split = NULL} to mean
42
  \code{split = character(0)}, including in the examples below.
27084 ripley 43
 
40284 ripley 44
  Note that splitting into single characters can be done \emph{via}
49666 ripley 45
  \code{split = character(0)} or \code{split = ""}; the two are
55555 ripley 46
  equivalent.  The definition of \sQuote{character} here depends on the
49666 ripley 47
  locale: in a single-byte locale it is a byte, and in a multi-byte
48
  locale it is the unit represented by a \sQuote{wide character} (almost
49
  always a Unicode point).
53883 maechler 50
 
39745 murdoch 51
  A missing value of \code{split} does not split the corresponding
27085 ripley 52
  element(s) of \code{x} at all.
37822 ripley 53
 
54
  The algorithm applied to each input string is
61983 ripley 55
\preformatted{    repeat \{
47616 ripley 56
        if the string is empty
57
            break.
58
        if there is a match
59
            add the string to the left of the match to the output.
60
            remove the match and all to the left of it.
61
        else
62
            add the string to the output.
63
            break.
37822 ripley 64
    \}
55195 ripley 65
}
37822 ripley 66
  Note that this means that if there is a match at the beginning of a
67
  (non-empty) string, the first element of the output is \code{""}, but
68
  if there is a match at the end of the string, the output is the same
53883 maechler 69
  as with the match removed.
27084 ripley 70
}
49666 ripley 71
\value{
72
  A list of the same length as \code{x}, the \code{i}-th element of which
73
  contains the vector of splits of \code{x[i]}.
74
 
75
  If any element of \code{x} or \code{split} is declared to be in UTF-8
52340 murdoch 76
  (see \code{\link{Encoding}}), all non-ASCII character strings in the
50374 ripley 77
  result will be in UTF-8 and have their encoding declared as UTF-8.  As
78
  from \R 2.10.0, for \code{perl = TRUE, useBytes = FALSE} all non-ASCII
50266 ripley 79
  strings in a multibyte locale are translated to UTF-8.
28442 ripley 80
}
50269 ripley 81
 
82
\note{
83
  Prior to \R 2.11.0 there was an argument \code{extended} which could
84
  be used to select \sQuote{basic} regular expressions: this was often
85
  used when \code{fixed = TRUE} would be preferable.  In the actual
86
  implementation (as distinct from the POSIX standard) the only
87
  difference was that \samp{?}, \samp{+}, \samp{\{}, \samp{|}, \samp{(},
88
  and \samp{)} were not interpreted as metacharacters.
89
}
90
 
2 r 91
\seealso{
7125 hornik 92
  \code{\link{paste}} for the reverse,
93
  \code{\link{grep}} and \code{\link{sub}} for string search and
49666 ripley 94
  manipulation; also \code{\link{nchar}}, \code{\link{substr}}.
26760 ripley 95
 
43585 ripley 96
  \sQuote{\link{regular expression}} for the details of the pattern
97
  specification.
2 r 98
}
99
\examples{
2395 maechler 100
noquote(strsplit("A text I want to display with spaces", NULL)[[1]])
101
 
24343 maechler 102
x <- c(as = "asfef", qu = "qwerty", "yuiop[", "b", "stuff.blah.yech")
2395 maechler 103
# split x on the letter e
49658 ripley 104
strsplit(x, "e")
7826 hornik 105
 
106
unlist(strsplit("a.b.c", "."))
107
## [1] "" "" "" "" ""
25118 hornik 108
## Note that 'split' is a regexp!
109
## If you really want to split on '.', use
59759 ripley 110
unlist(strsplit("a.b.c", "[.]"))
7826 hornik 111
## [1] "a" "b" "c"
27085 ripley 112
## or
113
unlist(strsplit("a.b.c", ".", fixed = TRUE))
18813 maechler 114
 
115
## a useful function: rev() for strings
116
strReverse <- function(x)
61153 ripley 117
        sapply(lapply(strsplit(x, NULL), rev), paste, collapse = "")
18813 maechler 118
strReverse(c("abc", "Statistics"))
119
 
27124 ripley 120
## get the first names of the members of R-core
36704 ripley 121
a <- readLines(file.path(R.home("doc"),"AUTHORS"))[-(1:8)]
25030 ripley 122
a <- a[(0:2)-length(a)]
27124 ripley 123
(a <- sub(" .*","", a))
124
# and reverse them
125
strReverse(a)
37810 ripley 126
 
127
## Note that final empty strings are not produced:
128
strsplit(paste(c("", "a", ""), collapse="#"), split="#")[[1]]
129
# [1] ""  "a"
130
## and also an empty string is only produced before a definite match:
131
strsplit("", " ")[[1]]    # character(0)
132
strsplit(" ", " ")[[1]]   # [1] ""
2 r 133
}
286 maechler 134
\keyword{character}