Parsing strings

This is a discussion on Parsing strings within the C++ Programming forums, part of the General Programming Boards category; Hey all. I would like to find a way to parse a string, without using vectors. Basically let us say ...

  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
    21,636
    For your example using std::getline with '|' as the delimiter in a loop should suffice.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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: ~
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  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
    21,636
    Remember to check that i < ARRAY_SIZE before calling getline.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,545
    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
    21,636
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,545
    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,545
    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
    21,636
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,545
    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, 07: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, 05:32 PM
  4. Parsing Strings
    By Hunter2 in forum C++ Programming
    Replies: 29
    Last Post: 12-05-2004, 06:48 PM
  5. Parsing Strings
    By SubLogic in forum C++ Programming
    Replies: 15
    Last Post: 01-07-2003, 10:11 AM

Tags for this Thread


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