Splitting strings into words.

This is a discussion on Splitting strings into words. within the C++ Programming forums, part of the General Programming Boards category; yes i could probablly sit down and write some ugly loop to split a string into individual words, however im ...

  1. #1
    0x7f
    Guest

    Splitting strings into words.

    yes i could probablly sit down and write some ugly loop to split a string into individual words, however im interested in knowing if there is a more elegent way to do what i want.

    string s1 = "hello my name is bob";
    vector<string> v1;

    the end result im looking for is something like
    v1[0] = "hello"; v1[1] = "my"; v1[...] = ...;

    can i take this and dump it into a vector of strings, or pipeing it somehow like s1 >> s2; or anything like that. ya i know it wont work but im just kinda pulling ideas out of my ass. im a perl monk and i miss my split and foreach functions =P

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,701
    Use istringstream.

    gg

  3. #3
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    see if this helps you:

    splitting strings

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,701
    Code:
        string str = "hello my name is Codeplug";
        string word;
        istringstream is(str);
        vector<string> sv;
    
        while (is >> word)
            sv.push_back(word);
    This will parse on spaces, if you want to do anthing more fancy, you will have to do it yourself or you could use C-string lib function strtok(). And there are all the examples from Rod's post.

    gg

  5. #5
    0x7f
    Guest

    istringstream

    ok, im liking this istringstream thing, what shall i include. its $$$$$ing because "is" uses undefined class basic_istrginstream bla bla bla.

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,701
    My bad....its "#include <sstream>"

    gg

  7. #7
    0x7f
    Guest

    Talking yep, thx

    ya that deffinetly god it, thanks alot. no more ugly split functions for every program i write w00t! i was trying to include stuff like istringstream and stringstream. never thought of sstream, and of course the msft help phile is most unhelpful. we recently decided that the msft help file was based on recursion because you need to know the answer to your question to find it. lol.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Storing Words from a text file
    By matt_570 in forum C++ Programming
    Replies: 18
    Last Post: 12-10-2008, 12:35 PM
  2. Problem with Strings, Please help!
    By varus in forum C++ Programming
    Replies: 8
    Last Post: 11-27-2006, 11:47 PM
  3. Beginners Contest #2 For those who wanted more!!
    By ILoveVectors in forum Contests Board
    Replies: 16
    Last Post: 08-12-2005, 01:03 AM
  4. New Theme
    By XSquared in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 04-01-2004, 08:00 PM
  5. finding strings in strings
    By watshamacalit in forum C Programming
    Replies: 14
    Last Post: 01-11-2003, 01:08 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21