Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to extract an std::string from another std::string?

Thread Tools

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

Dwight Army of Champions
Posts: n/a
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"?
Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How do i extract vidios when winrar wont extract them??? help plzzzzzzzz Computer Support 2 12-23-2007 07:03 AM
Another newbie question: extract doubles from strings philbo30 C Programming 2 02-17-2007 11:47 PM
How to extract x amount of bytes from a byte object and store into another byte obj. DaBeef Java 1 07-21-2006 05:20 PM
Extract text from a file and write to another. Sesa Woruban Perl Misc 4 02-02-2004 06:36 AM