stri_sub: Extract a Substring From or Replace a Substring In a...

Description Usage Arguments Details Value See Also Examples

Description

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.

Usage

1
2
3
stri_sub(str, from = 1L, to = -1L, length = .Machine$integer.max)

stri_sub(str, from = 1L, to = -1L, length) <- value

Arguments

str

character vector

from

integer vector or two-column matrix

to

integer vector; mutually exclusive with length and from being a matrix

length

integer vector; mutually exclusive with to and from being a matrix

value

character vector to be substituted [replacement function]

Details

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.

Value

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.

See Also

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

Examples

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)

Example output

[1] " ipsum dolor sit" " dolor sit"       " sit"            
[1] "Lorem" "ipsum" "dolor"
[1] "L"   "Lo"  "Lor"
[1] "adipisicing"
[1] "elit"
[1] "stringi"
[1] "."
[1] 1 2
Warning message:
In `stri_sub<-`(`*tmp*`, 1, 1:3, value = 1:2) :
  longer object length is not a multiple of shorter object length

stringi documentation built on May 2, 2019, 4:54 p.m.