Skip to contents

This helper function extracts one or more characters from a string using their byte positions. It converts the input string to raw bytes, selects the specified positions, and converts them back to a character.

Usage

extract_string_chars(str, pos)

Arguments

str

A character string from which characters will be extracted.

pos

An integer vector of positions (1-based) indicating which characters to extract.

Value

A character string containing the extracted characters.

Details

This is often faster and easier than splitting the string, subsetting, and pasting the string back together.

Note

This function operates at the raw byte level and may not behave as expected with multibyte or non-ASCII characters. For the purposes of this package, all validated sequence strings will only contain ASCII strings and this will work as expected.

Examples

extract_string_chars("hello", c(1, 2))  # Returns "he"
#> [1] "he"