Copy Part of String Into Other Strings

Discussion in 'C++' started by sakitah, Oct 4, 2005.

  1. sakitah

    sakitah Guest

    Hello Everyone,


    Here's the problem (I'm using Visual c++ 6.0):


    I have a string:
    string1 = "This&is&life";


    and i want to use the '&' as a seperator to help me parse this string
    into 3 strings so that:


    string2 = 'This'
    string3 = 'is'
    string4 = 'life'


    thanx
    Peace
    Sakitah
     
    sakitah, Oct 4, 2005
    #1
    1. Advertising

  2. sakitah

    Default User Guest

    sakitah wrote:

    > Hello Everyone,
    >
    >
    > Here's the problem (I'm using Visual c++ 6.0):
    >
    >
    > I have a string:
    > string1 = "This&is&life";


    What kind of "string"? A std::string or a C-style array of characters?
    The answer tends to be different.



    Brian
    --
    Please quote enough of the previous message for context. To do so from
    Google, click "show options" and use the Reply shown in the expanded
    header.
     
    Default User, Oct 4, 2005
    #2
    1. Advertising

  3. sakitah

    Mike Wahler Guest

    "sakitah" <> wrote in message
    news:...
    > Hello Everyone,
    >
    >
    > Here's the problem (I'm using Visual c++ 6.0):
    >
    >
    > I have a string:
    > string1 = "This&is&life";
    >
    >
    > and i want to use the '&' as a seperator to help me parse this string
    > into 3 strings so that:
    >
    >
    > string2 = 'This'
    > string3 = 'is'
    > string4 = 'life'
    >

    #include <iostream>
    #include <sstream>
    #include <string>

    int main()
    {
    std::string s1 = "This&is&life";
    std::string s2, s3, s4;
    std::istringstream iss(s1);

    std::getline(iss, s2, '&');
    std::getline(iss, s3, '&');
    std::getline(iss, s4, '&');

    std::cout << s1 << '\n'
    << s2 << '\n'
    << s3 << '\n'
    << s4 << '\n';

    return 0;
    }

    -Mike
     
    Mike Wahler, Oct 5, 2005
    #3
  4. sakitah

    Ludy Guest

    Hi Mike,

    In case the string is a large one, and we are planning to parse it and
    store it in a vector,
    how can we get the count of '&' using istringstream so that we can run
    a loop to put it in a vector....?
    i tired it with string.find but it required the calculation of
    positons but its pretty lenghth one.

    -Alex
     
    Ludy, Oct 5, 2005
    #4
  5. sakitah

    Dan Cernat Guest

    Ludy wrote:
    > Hi Mike,
    >
    > In case the string is a large one, and we are planning to parse it and
    > store it in a vector,
    > how can we get the count of '&' using istringstream so that we can run
    > a loop to put it in a vector....?
    > i tired it with string.find but it required the calculation of
    > positons but its pretty lenghth one.
    >
    > -Alex


    one doesn't need to count the '&'. Use a std::vector<string> and
    push_back your substrings. It will grow by itself.

    Dan
     
    Dan Cernat, Oct 5, 2005
    #5
  6. Ludy wrote:
    >
    > Hi Mike,
    >
    > In case the string is a large one, and we are planning to parse it and
    > store it in a vector,
    > how can we get the count of '&' using istringstream so that we can run
    > a loop to put it in a vector....?


    An istregstream, as the name suggests, works like a stream. That is,
    getline() will tell you when there is nothing more to read:

    std::string s2;
    std::vector< std::string > words;

    while( std::getline( iss, s2, '&' ) )
    words.push_back( s2 );

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Oct 5, 2005
    #6
  7. "Ludy" <> wrote in message
    news:...

    > how can we get the count of '&' using istringstream so that we can run
    > a loop to put it in a vector....?


    You don't need to know the number of elements to fill the vector. Just
    push_back:

    #include <vector>

    /* ... */

    std::vector<std::string> sections;

    while (iss)
    {
    std::string section;
    std::getline(iss, section, '&');

    if (iss)
    {
    sections.push_back(section);
    }
    }

    > i tired it with string.find but it required the calculation of
    > positons but its pretty lenghth one.


    If you really need to know, consider the count_if algorithm.

    Ali
     
    =?iso-8859-1?Q?Ali_=C7ehreli?=, Oct 5, 2005
    #7
  8. sakitah

    sakitah Guest

    Thank You Everyone, I have figured it out. Brian, I was using an
    std::string.
    Basically, I used find_first_of function to return the first instance
    of the '&'
    Then I used the append function to place what's at the beginning of the
    original string1 until the '&' into string2.
    Then I used the erase function to delete from the original string from
    0 until the first '&' and so on until I filled the rest of the strings.

    Now that I did that, I want to deal with the new strings as integers,
    how can I do that?
    So, say the originals were:

    string1 = "001-202-1234567"

    and parsing it into:

    string2 = "001";
    string3 = "202";
    string4 = "1234567";

    I want to manipulate these as numbers. Sadly, I cant append from the
    original string into an integer, so how can I do it?

    Thank You
    Sakitah
     
    sakitah, Oct 5, 2005
    #8
  9. sakitah wrote:
    >
    > Thank You Everyone, I have figured it out. Brian, I was using an
    > std::string.
    > Basically, I used find_first_of function to return the first instance
    > of the '&'
    > Then I used the append function to place what's at the beginning of the
    > original string1 until the '&' into string2.
    > Then I used the erase function to delete from the original string from
    > 0 until the first '&' and so on until I filled the rest of the strings.
    >
    > Now that I did that, I want to deal with the new strings as integers,
    > how can I do that?
    > So, say the originals were:
    >
    > string1 = "001-202-1234567"
    >
    > and parsing it into:
    >
    > string2 = "001";
    > string3 = "202";
    > string4 = "1234567";
    >
    > I want to manipulate these as numbers. Sadly, I cant append from the
    > original string into an integer, so how can I do it?


    You need to *convert* each string into a number. And again istringstream
    does exactly what you want:

    int i;
    istringstream iss( string1 );
    iss > i;

    // now i holds the numerical equivalent of the character sequence

    As I said in some other posting: istringstream behaves like any other
    stream with the only difference that the 'input' comes from a string.

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Oct 5, 2005
    #9
  10. sakitah

    sakitah Guest

    what library do i need to include? (because Im getting an error that
    it's an undeclared identifier
     
    sakitah, Oct 5, 2005
    #10
  11. sakitah wrote:
    >
    > what library do i need to include? (because Im getting an error that
    > it's an undeclared identifier


    I don't want to get rude, but:

    * do you know how to operate the help system that came with your compiler.

    * do you have some books to teach you?

    * Do you know how to use google for searching information? You have the worlds
    knowledge at your finger tips. Use the force, Lucke.

    * Just look up an answer Mike gave on a previous question of yours:

    #include <iostream>
    #include <sstream>
    #include <string>

    int main()
    {
    std::string s1 = "This&is&life";
    std::string s2, s3, s4;
    std::istringstream iss(s1);

    std::getline(iss, s2, '&');
    std::getline(iss, s3, '&');
    std::getline(iss, s4, '&');

    std::cout << s1 << '\n'
    << s2 << '\n'
    << s3 << '\n'
    << s4 << '\n';

    return 0;
    }

    Mike used istringstream and was kind enough to poste a complete program.

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Oct 5, 2005
    #11
  12. sakitah

    sakitah Guest

    If you don't want to get rude, then please don't.
    This is afterall one of the resources of the net, and I know the
    questions might not seem very complicated for you, but I am under a
    very short time limit with my use for the internet. So I try to get as
    much as I can. And I do use searches of google and other search
    engines.
     
    sakitah, Oct 5, 2005
    #12
  13. Marcelo Pinto, Oct 5, 2005
    #13
  14. sakitah

    Default User Guest

    sakitah wrote:

    > what library do i need to include? (because Im getting an error that
    > it's an undeclared identifier


    This is a model for an non-informative post. What error? What
    identifier does is say is undefined?

    Show the code (a complete, minimal program) along with the exact error
    messages. Also read my sig below.


    Brian
    --
    Please quote enough of the previous message for context. To do so from
    Google, click "show options" and use the Reply shown in the expanded
    header.
     
    Default User, Oct 5, 2005
    #14
    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. Alex
    Replies:
    2
    Views:
    1,239
  2. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    768
    Malcolm
    Jun 24, 2006
  3. Jim Showalter

    how to copy strings into a linked list?

    Jim Showalter, Aug 23, 2006, in forum: C Programming
    Replies:
    6
    Views:
    369
    Jim Showalter
    Aug 24, 2006
  4. Replies:
    26
    Views:
    2,123
    Roland Pibinger
    Sep 1, 2006
  5. Scott Bass
    Replies:
    4
    Views:
    118
    Tad McClellan
    May 12, 2005
Loading...

Share This Page