Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8815475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:23:18+00:00 2026-06-14T04:23:18+00:00

I want to write a function that slices a ‘string’ into a vector, sequentially,

  • 0

I want to write a function that slices a ‘string’ into a vector, sequentially, at a given index. I have a fairly adequate R solution for it; however, I figure that writing the code in C/C++ would likely be faster. For example, I’d like to be able to write a function ‘strslice’ that operates as follows:

x <- "abcdef"
strslice( x, 2 ) ## should return c("ab", "cd", "ef")

However, I’m not sure how to handle treating elements of the ‘CharacterVector’ passed around in the Rcpp code as strings. This is what I imagine might work (given my lack of C++/Rcpp knowledge I’m sure there’s a better approach):

f <- rcpp( signature(x="character", n="integer"), '
  std::string myString = Rcpp::as<std::string>(x);
  int cutpoint = Rcpp::as<int>(n);
  vector<std::string> outString;
  int len = myString.length();
  for( int i=0; i<len/n; i=i+n ) {
    outString.push_back( myString.substr(i,i+n-1 ) );
    myString = myString.substr(i+n, len-i*n);
  }
  return Rcpp::wrap<Rcpp::CharacterVector>( outString );
  ')

For the record, the corresponding R code I have is:

strslice <- function(x, n) {
  x <- as.data.frame( stringsAsFactors=FALSE, 
                      matrix( unlist( strsplit( x, "" ) ), ncol=n, byrow=T )
  )

  do.call( function(...) { paste(..., sep="") }, x )

}

…but I figure jumping around between data structures so much will slow things down with very large strings.

(Alternatively: is there a way to coerce ‘strsplit’ into behaving as I want?)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T04:23:19+00:00Added an answer on June 14, 2026 at 4:23 am

    I would use substring. Something like this:

    strslice <- function( x, n ){   
        starts <- seq( 1L, nchar(x), by = n )
        substring( x, starts, starts + n-1L )
    }
    strslice( "abcdef", 2 )
    # [1] "ab" "cd" "ef"
    

    About your Rcpp code, maybe you can allocate the std::vector<std::string> with the right size, so that you avoid resizing it which might mean memory allocations, … or perhaps directly use a Rcpp::CharacterVector. Something like this:

    strslice_rcpp <- rcpp( signature(x="character", n="integer"), '
        std::string myString = as<std::string>(x);
        int cutpoint = as<int>(n);
        int len = myString.length();
        int nout = len / cutpoint ;
        CharacterVector out( nout ) ;
        for( int i=0; i<nout; i++ ) {
          out[i] = myString.substr( cutpoint*i, 2 ) ;
        }
        return out ;
    ')
    strslice_rcpp( "abdcefg", 2 )
    # [1] "ab" "cd" "ef"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write a function that format int and decimal differently into string
I want to write a function that, given a vector v computes the product
I have problem with fancybox. I want to write a function that will run
I want to Write a function that returns the Products brought in a given
I want to write a function that will take a string which represents a
I want to write a function that accepts either a path as a string
I want to write a function that given a number will set all but
I want to write a function that splits lists into sublists according to what
I want to write a function that creates a Vector (template), stores a couple
I want to write a function that resizes a 2D array to the given

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.