| 42333 |
ripley |
1 |
% File src/library/base/man/strsplit.Rd
|
| 68948 |
ripley |
2 |
% Part of the R package, https://www.R-project.org
|
| 87828 |
ripley |
3 |
% Copyright 1995-2024=5 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 |
|
| 84518 |
kalibera |
72 |
Note also that if there is an empty match at the beginning of a non-empty
|
|
|
73 |
string, the first character is returned and the algorithm continues with
|
| 84532 |
smeyer |
74 |
the rest of the string. This needs to be kept in mind when designing the
|
| 84518 |
kalibera |
75 |
regular expressions. For example, when looking for a word boundary
|
|
|
76 |
followed by a letter (\code{"[[:<:]]"} with \code{perl = TRUE}), one can
|
|
|
77 |
disallow a match at the beginning of a string (via \code{"(?!^)[[:<:]]"}).
|
|
|
78 |
|
| 65334 |
ripley |
79 |
Invalid inputs in the current locale are warned about up to 5 times.
|
| 27084 |
ripley |
80 |
}
|
| 49666 |
ripley |
81 |
\value{
|
|
|
82 |
A list of the same length as \code{x}, the \code{i}-th element of which
|
|
|
83 |
contains the vector of splits of \code{x[i]}.
|
|
|
84 |
|
|
|
85 |
If any element of \code{x} or \code{split} is declared to be in UTF-8
|
| 52340 |
murdoch |
86 |
(see \code{\link{Encoding}}), all non-ASCII character strings in the
|
| 65147 |
ripley |
87 |
result will be in UTF-8 and have their encoding declared as UTF-8.
|
| 79717 |
ripley |
88 |
(This also holds if any element is declared to be Latin-1 except in a
|
|
|
89 |
Latin-1 locale.)
|
| 65147 |
ripley |
90 |
For \code{perl = TRUE, useBytes = FALSE} all non-ASCII strings in a
|
|
|
91 |
multibyte locale are translated to UTF-8.
|
| 82587 |
kalibera |
92 |
|
| 82827 |
kalibera |
93 |
If any element of \code{x} or \code{split} is marked as \code{"bytes"}
|
|
|
94 |
(see \code{\link{Encoding}}), all non-ASCII character strings created by
|
|
|
95 |
the splitting in the result will be marked as \code{"bytes"}, but encoding
|
|
|
96 |
of the resulting character strings not split is unspecified (may be
|
|
|
97 |
\code{"bytes"} or the original). If no element of \code{x} or
|
|
|
98 |
\code{split} is marked as \code{"bytes"}, but \code{useBytes = TRUE}, even
|
|
|
99 |
the encoding of the resulting character strings created by splitting is
|
|
|
100 |
unspecified (may be \code{"bytes"} or \code{"unknown"}, possibly invalid
|
|
|
101 |
in the current encoding). Mixed use of \code{"bytes"} and other marked
|
|
|
102 |
encodings is discouraged, but if still desired one may use
|
|
|
103 |
\code{\link{iconv}} to re-encode the result e.g. to UTF-8 with suitably
|
|
|
104 |
substituted invalid bytes.
|
| 28442 |
ripley |
105 |
}
|
| 50269 |
ripley |
106 |
|
| 87828 |
ripley |
107 |
\section{Warning}{
|
| 87850 |
hornik |
108 |
An all too common mis-usage is to pass unnamed arguments which are then
|
| 87828 |
ripley |
109 |
matched to one or more of \code{fixed},
|
|
|
110 |
\code{perl} and \code{useBytes}. So it is good
|
|
|
111 |
practice to name all the arguments.
|
|
|
112 |
}
|
|
|
113 |
|
| 2 |
r |
114 |
\seealso{
|
| 7125 |
hornik |
115 |
\code{\link{paste}} for the reverse,
|
|
|
116 |
\code{\link{grep}} and \code{\link{sub}} for string search and
|
| 87291 |
maechler |
117 |
manipulation; also \code{\link{nchar}}, \code{\link{substr}},
|
| 87299 |
smeyer |
118 |
\code{\link{startsWith}}, and \code{\link{endsWith}}.
|
| 26760 |
ripley |
119 |
|
| 43585 |
ripley |
120 |
\sQuote{\link{regular expression}} for the details of the pattern
|
|
|
121 |
specification.
|
| 87291 |
maechler |
122 |
|
| 72247 |
ripley |
123 |
Option \code{PCRE_use_JIT} controls the details when \code{perl = TRUE}.
|
| 2 |
r |
124 |
}
|
|
|
125 |
\examples{
|
| 2395 |
maechler |
126 |
noquote(strsplit("A text I want to display with spaces", NULL)[[1]])
|
|
|
127 |
|
| 24343 |
maechler |
128 |
x <- c(as = "asfef", qu = "qwerty", "yuiop[", "b", "stuff.blah.yech")
|
| 2395 |
maechler |
129 |
# split x on the letter e
|
| 49658 |
ripley |
130 |
strsplit(x, "e")
|
| 7826 |
hornik |
131 |
|
|
|
132 |
unlist(strsplit("a.b.c", "."))
|
|
|
133 |
## [1] "" "" "" "" ""
|
| 25118 |
hornik |
134 |
## Note that 'split' is a regexp!
|
|
|
135 |
## If you really want to split on '.', use
|
| 59759 |
ripley |
136 |
unlist(strsplit("a.b.c", "[.]"))
|
| 7826 |
hornik |
137 |
## [1] "a" "b" "c"
|
| 27085 |
ripley |
138 |
## or
|
|
|
139 |
unlist(strsplit("a.b.c", ".", fixed = TRUE))
|
| 18813 |
maechler |
140 |
|
|
|
141 |
## a useful function: rev() for strings
|
|
|
142 |
strReverse <- function(x)
|
| 61153 |
ripley |
143 |
sapply(lapply(strsplit(x, NULL), rev), paste, collapse = "")
|
| 18813 |
maechler |
144 |
strReverse(c("abc", "Statistics"))
|
|
|
145 |
|
| 27124 |
ripley |
146 |
## get the first names of the members of R-core
|
| 36704 |
ripley |
147 |
a <- readLines(file.path(R.home("doc"),"AUTHORS"))[-(1:8)]
|
| 25030 |
ripley |
148 |
a <- a[(0:2)-length(a)]
|
| 27124 |
ripley |
149 |
(a <- sub(" .*","", a))
|
|
|
150 |
# and reverse them
|
|
|
151 |
strReverse(a)
|
| 37810 |
ripley |
152 |
|
|
|
153 |
## Note that final empty strings are not produced:
|
|
|
154 |
strsplit(paste(c("", "a", ""), collapse="#"), split="#")[[1]]
|
|
|
155 |
# [1] "" "a"
|
|
|
156 |
## and also an empty string is only produced before a definite match:
|
|
|
157 |
strsplit("", " ")[[1]] # character(0)
|
|
|
158 |
strsplit(" ", " ")[[1]] # [1] ""
|
| 2 |
r |
159 |
}
|
| 286 |
maechler |
160 |
\keyword{character}
|