How to extract an std::string from another std::string?

  • Thread starter Dwight Army of Champions
  • Start date
D

Dwight Army of Champions

Suppose I have an std::string called line that has the value
"sdsder>abc<gd<< sds". I want to get the substring between the greater-
than less-than signs and assign it to another std::string called
substr. How do I do this? My guess is that you should use two string
iterators, one called it_begin that points to the first occurrence of
a greater-than sign and another string iterator called it_end that
starts at the same spot as it_begin and goes to the first occurrence
of a less-than sign. Then you somehow extract the data between those
two iterators. Here's what I have so far:

#include <iostream>
#include <string>

int main()
{
std::string line = "sdsder>abc<gd<< sds";

std::string::iterator it_begin;
std::string::iterator it_end;

return 0;
}

Here's where I'm stumped. What value should I be assigning these two
iterators? Obviously their indexes need to be six and ten, but we need
to assume that line could be any string at all, not the garbage one
given. How do I tell std::string to "find the first occurrence of
character such-and-such and point it there"?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top