\HeaderA{substr}{Substrings of a Character Vector}{substr}
\aliasA{substr<\Rdash}{substr}{substr<.Rdash.}
\aliasA{substring}{substr}{substring}
\aliasA{substring<\Rdash}{substr}{substring<.Rdash.}
\keyword{character}{substr}
\begin{Description}\relax
Extract or replace substrings in a character vector.
\end{Description}
\begin{Usage}
\begin{verbatim}
substr(x, start, stop)
substring(text, first, last = 1000000)
substr(x, start, stop) <- value
substring(text, first, last = 1000000) <- value
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{x, text}] a character vector.
\item[\code{start, first}] integer. The first element to be replaced.
\item[\code{stop, last}] integer. The last element to be replaced.
\item[\code{value}] a character vector, recycled if necessary.
\end{ldescription}
\end{Arguments}
\begin{Details}\relax
\code{substring} is compatible with S, with \code{first} and
\code{last} instead of \code{start} and \code{stop}.
For vector arguments, it expands the arguments cyclically to the
length of the longest \emph{provided} none are of zero length.

When extracting, if \code{start} is larger than the string length then
\code{""} is returned.

For the extraction functions, \code{x} or \code{text} will be
converted to a character vector by \code{\LinkA{as.character}{as.character}} if it is not
already one.

For the replacement functions, if \code{start} is larger than the
string length then no replacement is done.  If the portion to be
replaced is longer than the replacement string, then only the
portion the length of the string is replaced.

If any argument is an \code{NA} element, the corresponding element of
the answer is \code{NA}.

Elements of the result will be have the encoding declared as that of
the current locale (see \code{\LinkA{Encoding}{Encoding}} if the corresponding
input had a declared encoding and the current locale is either Latin-1
or UTF-8.

Inputs with embedded nul bytes will be truncated at the first nul.
\end{Details}
\begin{Value}
For \code{substr}, a character vector of the same length and with the
same attributes as \code{x} (after possible coercion).

For \code{substring}, a character vector of length the longest of the
arguments.  This will have names taken from \code{x} (if it has any
after coercion, repeated as needed), and other attributes copied from
\code{x} if it is the longest of the arguments).

Elements of \code{x} with a declared encoding (see
\code{\LinkA{Encoding}{Encoding}}) will be returned with the same encoding.
\end{Value}
\begin{Note}\relax
The S4 version of \code{substring<-} ignores \code{last}; this version
does not.

These functions are often used with \code{\LinkA{nchar}{nchar}} to truncate a
display.  That does not really work (you want to limit the width, not
the number of characters, so it would be better to use
\code{\LinkA{strtrim}{strtrim}}), but at least make sure you use
\code{nchar(type="c")} (the default since \R{} 2.6.0).
\end{Note}
\begin{References}\relax
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)
\emph{The New S Language}.
Wadsworth \& Brooks/Cole. (\code{substring}.)
\end{References}
\begin{SeeAlso}\relax
\code{\LinkA{strsplit}{strsplit}}, \code{\LinkA{paste}{paste}}, \code{\LinkA{nchar}{nchar}}.
\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
substr("abcdef",2,4)
substring("abcdef",1:6,1:6)
## strsplit is more efficient ...

substr(rep("abcdef",4),1:4,4:5)
x <- c("asfef", "qwerty", "yuiop[", "b", "stuff.blah.yech")
substr(x, 2, 5)
substring(x, 2, 4:6)

substring(x, 2) <- c("..", "+++")
x
\end{ExampleCode}
\end{Examples}

