EVOLUTION-MANAGER
Edit File: substr.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Substrings of a Character Vector</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for substr {base}"><tr><td>substr {base}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Substrings of a Character Vector</h2> <h3>Description</h3> <p>Extract or replace substrings in a character vector. </p> <h3>Usage</h3> <pre> substr(x, start, stop) substring(text, first, last = 1000000L) substr(x, start, stop) <- value substring(text, first, last = 1000000L) <- value </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>x, text</code></td> <td> <p>a character vector.</p> </td></tr> <tr valign="top"><td><code>start, first</code></td> <td> <p>integer. The first element to be replaced.</p> </td></tr> <tr valign="top"><td><code>stop, last</code></td> <td> <p>integer. The last element to be replaced.</p> </td></tr> <tr valign="top"><td><code>value</code></td> <td> <p>a character vector, recycled if necessary.</p> </td></tr> </table> <h3>Details</h3> <p><code>substring</code> is compatible with S, with <code>first</code> and <code>last</code> instead of <code>start</code> and <code>stop</code>. For vector arguments, it expands the arguments cyclically to the length of the longest <em>provided</em> none are of zero length. </p> <p>When extracting, if <code>start</code> is larger than the string length then <code>""</code> is returned. </p> <p>For the extraction functions, <code>x</code> or <code>text</code> will be converted to a character vector by <code><a href="character.html">as.character</a></code> if it is not already one. </p> <p>For the replacement functions, if <code>start</code> 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. </p> <p>If any argument is an <code>NA</code> element, the corresponding element of the answer is <code>NA</code>. </p> <p>Elements of the result will be have the encoding declared as that of the current locale (see <code><a href="Encoding.html">Encoding</a></code>) if the corresponding input had a declared Latin-1 or UTF-8 encoding and the current locale is either Latin-1 or UTF-8. </p> <p>If an input element has declared <code>"bytes"</code> encoding (see <code><a href="Encoding.html">Encoding</a></code>, the subsetting is done in units of bytes not characters. </p> <h3>Value</h3> <p>For <code>substr</code>, a character vector of the same length and with the same attributes as <code>x</code> (after possible coercion). </p> <p>For <code>substring</code>, a character vector of length the longest of the arguments. This will have names taken from <code>x</code> (if it has any after coercion, repeated as needed), and other attributes copied from <code>x</code> if it is the longest of the arguments). </p> <p>Elements of <code>x</code> with a declared encoding (see <code><a href="Encoding.html">Encoding</a></code>) will be returned with the same encoding. </p> <h3>Note</h3> <p>The S4 version of <code>substring<-</code> ignores <code>last</code>; this version does not. </p> <p>These functions are often used with <code><a href="nchar.html">nchar</a></code> 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><a href="strtrim.html">strtrim</a></code>), but at least make sure you use the default <code>nchar(type = "c")</code>. </p> <h3>References</h3> <p>Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) <em>The New S Language</em>. Wadsworth & Brooks/Cole. (<code>substring</code>.) </p> <h3>See Also</h3> <p><code><a href="strsplit.html">strsplit</a></code>, <code><a href="paste.html">paste</a></code>, <code><a href="nchar.html">nchar</a></code>. </p> <h3>Examples</h3> <pre> 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 </pre> <hr /><div style="text-align: center;">[Package <em>base</em> version 3.6.0 <a href="00Index.html">Index</a>]</div> </body></html>