Partial tokenization from a certain position in C/C++

Greetings to all of you fellow coders.
I have been doing some reads through all of the documentations but couldn't find anything that suits this...

I have a string like:

char *origin = "THIS, IS, SPARTA!"

What I want to do is get all characters in a string up to a certain delimiter, for that I've searched I can do that with:
sscanf
getline
strok

But what I don't know and haven't found is how to use these functions but to start reading from a certain position in the string.

So I need to get the characters up to a comma but after "THIS," so that would be starting in position 5...

¿How could I do that?

I tried using [x] in brackets but then it would just read a character...
Using a stringstream is one of the simplest ways to do this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <vector>
#include <string>
#include <sstream>

std::vector<std::string> split( std::string str, char delimiter )
{
   std::vector<std::string> tokens ;

   std::istringstream stm(str) ;
   std::string tok ;
   while( std::getline( stm, tok, delimiter ) ) tokens.push_back(tok) ;

   return tokens ;
}

int main()
{
    std::string str = "THIS, IS, SPARTA!" ;
    for( const auto& token : split( str, ',' ) ) std::cout << token << '\n' ;
}
Yes, I know how to do that, what I don't know is how to to that begginning from a certain position in the string....
1
2
3
4
5
6
7
8
std::string str = "xxxx, yyy, zzz: THIS, IS, SPARTA!" ;

auto pos = str.find( ':' ) ; 
if( pos == std::string::npos ) pos = 0 ;
else ++pos ;

for( const auto& token : split( str.substr(pos), ',' ) )
    std::cout << token << '\n' ;

JLBorges is speaking in code. What's he's saying in English is, "Divide and conquer."

You're free to split the string anywhere you like and process it in the peices that you're interested in.
In the end I did this by doing this, more simpler:

char *example;
usrnm = strtok (&string[3],":");
printf("you got: %s\n",example);
Topic archived. No new replies allowed.