Learn R Programming

Kmisc (version 0.1.0)

strslice: Slice a String at Consecutive Indices

Description

Slice a string at consecutive indices, such that substrings of equal length are generated from a string.

Usage

strslice(x, n)

Arguments

x
a character vector of length one; ie, a string.
n
the index at which to cut

Value

  • A character vector, 'sliced' at consecutive indices n.

Examples

Run this code
x <- "ABCDEF"
stopifnot( strslice( x, 2 ) == c("AB", "CD", "EF") )

## applying strslice to a vector of 'strings'
y <- c("ABCD", "EFGH", "IJK") ## note unequal length
sapply( y, function(x) { strslice(x, 2) } ) ## each element sliced; warning generated for "IJK"

Run the code above in your browser using DataLab