Thread: Parsing strings

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    62

    Question Parsing strings

    Hey all. I would like to find a way to parse a string, without using vectors.
    Basically let us say I have...
    Code:
    std::string str = "value1|value2|value3"
    and I want to parse or tokenize it so that i have:
    Code:
    string values[100];
    //resulting strings
    values[0] = "value1";
    values[1] = "value2";
    values[2] = "value3";
    I would really like to do it without using vectors or boost. But if it is necessary I guess I could.
    Up until now, ive been using istringstream to parse it through whitespace, but I want to be able to use custom seperators (like |).

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    For your example using std::getline with '|' as the delimiter in a loop should suffice.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Up until now, ive been using istringstream to parse it through whitespace, but I want to be able to use custom seperators
    Use istringstream , but when getting the value from the stream use getline with the necessary delimiter .

    EDIT: Saw Laserlight's post after writhig this
    Last edited by manasij7479; 07-10-2011 at 08:45 PM. Reason: ~

  4. #4
    Registered User
    Join Date
    Jul 2011
    Posts
    62
    Yeah, I've made a little test code with it and it works fine =D thanks. Here is the code:
    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    
    #define ARRAY_SIZE 100
    
    using namespace std;
    
    int main()
    {
        string tokens[ARRAY_SIZE], source("Here|is|some|text");
        istringstream iss(source);
        int i=0;
        while(getline(iss, tokens[i], '|'))
        {
            cout << tokens[i] << endl;
            i++;
        }
        return 0;
    }
    I wasnt entirely sure that the statement inside while was actually run, thought it only evaluated as an anonymous bool variable. ty all

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Remember to check that i < ARRAY_SIZE before calling getline.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Shingetsu Kurai View Post
    Hey all. I would like to find a way to parse a string, without using vectors.
    ...
    I would really like to do it without using vectors or boost. But if it is necessary I guess I could.
    Now why would you do that? That is counter-productive.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Jul 2011
    Posts
    62
    Well, I dont want to use boost because it is an additional library to include, which in my opinion is unnecessary if it can be done easily without it. (Dont want to add unneded weight to the program)
    And I prefer string arrays to vectors because I can manage them more easily.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Shingetsu Kurai
    And I prefer string arrays to vectors because I can manage them more easily.
    If you would learn how to use vectors, you would manage them just as easily.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Shingetsu Kurai View Post
    Well, I dont want to use boost because it is an additional library to include, which in my opinion is unnecessary if it can be done easily without it. (Dont want to add unneded weight to the program)
    And I prefer string arrays to vectors because I can manage them more easily.
    Boost can do it even more conveniently. You should not avoid boost because "it is just another library." It's not. It is a C++ programmer's best friend after the standard library.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by Elysia View Post
    Boost can do it even more conveniently. You should not avoid boost because "it is just another library." It's not. It is a C++ programmer's best friend after the standard library.
    Except it IS a library. Boost can also do their own marketing.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I didn't mean that it isn't a library. Sure, it is.
    I meant it isn't "just another library." It is the library.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Elysia
    I meant it isn't "just another library." It is the library.
    As much as I am a fan of Boost, that is not true. Objectively, it is just another library (or collection of libraries) since it is not part of the standard library or technical report extensions thereof, even if various Boost libraries have made their way through the standardisation process, and the whole project was intended to help move the standard library forward.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I realize this is subjective. What I mean to say is that when I say the library, I simply means it's a highly recommended library in my opinion.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Parsing of strings
    By nsp in forum C Programming
    Replies: 4
    Last Post: 02-16-2011, 08:51 AM
  2. Parsing strings
    By Tupcia in forum C++ Programming
    Replies: 19
    Last Post: 04-24-2008, 09:38 AM
  3. sscanf and parsing strings
    By jco1323 in forum C Programming
    Replies: 4
    Last Post: 02-20-2008, 06:32 PM
  4. Parsing Strings
    By Hunter2 in forum C++ Programming
    Replies: 29
    Last Post: 12-05-2004, 07:48 PM
  5. Parsing Strings
    By SubLogic in forum C++ Programming
    Replies: 15
    Last Post: 01-07-2003, 11:11 AM

Tags for this Thread