EVOLUTION-MANAGER
Edit File: stri_sub.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: Extract a Substring From or Replace a Substring In a...</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 stri_sub {stringi}"><tr><td>stri_sub {stringi}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Extract a Substring From or Replace a Substring In a Character Vector</h2> <h3>Description</h3> <p><code>stri_sub</code> extracts particular substrings at code point-based index ranges provided. Its replacement version allows to substitute (in-place) parts of a string with given replacement strings. <code>stri_sub_replace</code> is its <span class="pkg">magrittr</span>'s pipe-operator-friendly variant that returns a copy of the input vector. </p> <p>For extracting/replacing multiple substrings from/within each string, see <code><a href="stri_sub_all.html">stri_sub_all</a></code>. </p> <h3>Usage</h3> <pre> stri_sub(str, from = 1L, to = -1L, length) stri_sub(str, from=1L, to=-1L, length, omit_na=FALSE) <- value stri_sub_replace(..., replacement, value = replacement) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>str</code></td> <td> <p>a character vector</p> </td></tr> <tr valign="top"><td><code>from</code></td> <td> <p>an integer vector giving the start indexes or a two-column matrix of type <code>cbind(from, to)</code></p> </td></tr> <tr valign="top"><td><code>to</code></td> <td> <p>an integer vector giving the end indexes; mutually exclusive with <code>length</code> and <code>from</code> being a matrix</p> </td></tr> <tr valign="top"><td><code>length</code></td> <td> <p>an integer vector giving the substring lengths; mutually exclusive with <code>to</code> and <code>from</code> being a matrix</p> </td></tr> <tr valign="top"><td><code>omit_na</code></td> <td> <p>a single logical value; indicates whether missing values in any of the indexes or in <code>value</code> leave the corresponding input string unchanged [replacement function only]</p> </td></tr> <tr valign="top"><td><code>value</code></td> <td> <p>a character vector defining the replacement strings [replacement function only]</p> </td></tr> <tr valign="top"><td><code>...</code></td> <td> <p>arguments to be passed to <code>stri_sub<-</code></p> </td></tr> <tr valign="top"><td><code>replacement</code></td> <td> <p>alias of <code>value</code> [wherever applicable]</p> </td></tr> </table> <h3>Details</h3> <p>Vectorized over <code>str</code>, [<code>value</code>], <code>from</code> and (<code>to</code> or <code>length</code>). Parameters <code>to</code> and <code>length</code> are mutually exclusive. </p> <p>Indexes are 1-based, i.e., the start of a string is at index 1. For negative indexes in <code>from</code> or <code>to</code>, counting starts at the end of the string. For instance, index -1 denotes the last code point in the string. Non-positive <code>length</code> gives an empty string. </p> <p>Argument <code>from</code> gives the start of a substring to extract. Argument <code>to</code> defines the last index of a substring, inclusive. Alternatively, its <code>length</code> may be provided. </p> <p>If <code>from</code> is a two-column matrix, then these two columns are used as <code>from</code> and <code>to</code>, respectively, and anything passed explicitly as <code>from</code> or <code>to</code> is ignored. Such types of index matrices are generated by <code><a href="stri_locate.html">stri_locate_first</a></code> and <code><a href="stri_locate.html">stri_locate_last</a></code>. If extraction based on <code><a href="stri_locate.html">stri_locate_all</a></code> is needed, see <code><a href="stri_sub_all.html">stri_sub_all</a></code>. </p> <p>In <code>stri_sub</code>, out-of-bound indexes are silently corrected. If <code>from</code> > <code>to</code>, then an empty string is returned. </p> <p>In <code>stri_sub<-</code>, some configurations of indexes may work as substring "injection" at the front, back, or in middle. </p> <p>If both <code>to</code> and <code>length</code> are provided, <code>length</code> has priority over <code>to</code>. </p> <p>Note that for some Unicode strings, the extracted substrings might not be well-formed, especially if input strings are not NFC-normalized (see <code><a href="stri_trans_nf.html">stri_trans_nfc</a></code>), include byte order marks, Bidirectional text marks, and so on. Handle with care. </p> <h3>Value</h3> <p><code>stri_sub</code> and <code>stri_sub_replace</code> return a character vector. <code>stri_sub<-</code> changes the <code>str</code> object in-place. </p> <h3>See Also</h3> <p>Other indexing: <code><a href="stri_locate_boundaries.html">stri_locate_all_boundaries</a>()</code>, <code><a href="stri_locate.html">stri_locate_all</a>()</code>, <code><a href="stri_sub_all.html">stri_sub_all</a>()</code> </p> <h3>Examples</h3> <pre> s <- "Lorem ipsum dolor sit amet, consectetur adipisicing elit." stri_sub(s, from=1:3*6, to=21) stri_sub(s, from=c(1,7,13), length=5) stri_sub(s, from=1, length=1:3) stri_sub(s, -17, -7) stri_sub(s, -5, length=4) (stri_sub(s, 1, 5) <- "stringi") (stri_sub(s, -6, length=5) <- ".") (stri_sub(s, 1, 1:3) <- 1:2) x <- c("12 3456 789", "abc", "", NA, "667") stri_sub(x, stri_locate_first_regex(x, "[0-9]+")) # see stri_extract_first stri_sub(x, stri_locate_last_regex(x, "[0-9]+")) # see stri_extract_last stri_sub_replace(x, stri_locate_first_regex(x, "[0-9]+"), omit_na=TRUE, replacement="***") # see stri_replace_first stri_sub_replace(x, stri_locate_last_regex(x, "[0-9]+"), omit_na=TRUE, replacement="***") # see stri_replace_last stri_sub(x, stri_locate_first_regex(x, "[0-9]+"), omit_na=TRUE) <- "***" print(x) ## Not run: x %>% stri_sub_replace(1, 5, replacement="new_substring") </pre> <hr /><div style="text-align: center;">[Package <em>stringi</em> version 1.4.6 <a href="00Index.html">Index</a>]</div> </body></html>