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

Discussion in 'C++' started by Dwight Army of Champions, Apr 20, 2010.

  1. 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"?
    Dwight Army of Champions, Apr 20, 2010
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Jansson
    Replies:
    5
    Views:
    6,263
    Ivan Vecerina
    Mar 17, 2005
  2. Vinu
    Replies:
    4
    Views:
    342
    Jim Langston
    Jul 7, 2005
  3. Fei Liu
    Replies:
    9
    Views:
    431
  4. Jeffrey Walton
    Replies:
    10
    Views:
    927
    Mathias Gaunard
    Nov 26, 2006
  5. cayenne
    Replies:
    19
    Views:
    647
    John W. Krahn
    May 20, 2004
Loading...

Share This Page