Description Usage Arguments Details Value See Also Examples
The first function extracts substrings under code point-based index ranges provided. The second one allows you to substitute parts of a string with given strings.
1 2 3 |
str |
character vector |
from |
integer vector or two-column matrix |
to |
integer vector; mutually exclusive with
|
length |
integer vector; mutually exclusive with
|
value |
character vector to be substituted [replacement function] |
Vectorized over str, [value], from and
(to or length). to and length
are mutually exclusive.
to has priority over length. If from
is a two-column matrix, then the first column is used as
from and the second one as to. In such case
arguments to and length are ignored.
Of course, the indices are code point-based, and not byte-based. Note that for some Unicode strings, the extracted substrings may not be well-formed, especially if the input is not NFC-normalized, includes byte order marks, Bidirectional text marks, and so on. Handle with care.
Indices are 1-based, i.e. an index equal to 1 denotes the
first character in a string, which gives a typical R
look-and-feel. Argument to defines the last index of
the substring, inclusive.
For negative indices in from or to, counting
starts at the end of the string. E.g. index -1 denotes the
last code point in the string. Negative length means
counting backwards.
In stri_sub, out-of-bound indices are silently
corrected. If from > to, then an empty string
is returned.
In stri_sub<-, “strange” configurations of indices
work as string concatenation at the front, back, or middle.
stri_sub returns a character vector.
stri_sub<- changes the value object.
The extract function stri_sub returns the indicated
substrings. The replacement function stri_sub<- is
invoked for its side effect: after a call, str is
modified.
Other indexing: stri_locate_all_charclass,
stri_locate_all_charclass,
stri_locate_first_charclass,
stri_locate_first_charclass,
stri_locate_last_charclass,
stri_locate_last_charclass;
stri_locate_all_fixed,
stri_locate_all_fixed,,
stri_locate_first_fixed,
stri_locate_first_fixed,,
stri_locate_last_fixed,
stri_locate_last_fixed;
stri_locate_all_regex,
stri_locate_all_regex,
stri_locate_first_regex,
stri_locate_first_regex,
stri_locate_last_regex,
stri_locate_last_regex;
stri_locate_all;
stri_locate_first;
stri_locate_last; stri_locate
1 2 3 4 5 6 7 8 9 | 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)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.