Parsing Strings

This is a discussion on Parsing Strings within the C++ Programming forums, part of the General Programming Boards category; I just started to work with C++ strings and I'm amused by all it's functions (string.h). On second tought, I'm ...

  1. #1
    Registered User SubLogic's Avatar
    Join Date
    Jan 2003
    Posts
    33

    Exclamation Parsing Strings

    I just started to work with C++ strings and I'm amused by all it's functions (string.h). On second tought, I'm a little confused how and where to use them. I'm currently trying to parse a string. I hope you know what parsing means. If you don't, ask and I'll do my best to explain.
    0100001

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Provide an example of the string you're working with, and describe what you want to do with it. If you have any code, post that too, and someone will guide you.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I hope you know what parsing means. If you don't, ask and I'll do my best to explain.
    I would hope that anyone not familiar with what parsing is would refrain from answering your question. What is the string format and how do you want to parse it?

    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User SubLogic's Avatar
    Join Date
    Jan 2003
    Posts
    33
    Parsing. Example, I have a string: "hello nice world" and wish to parse it using the space character (asc:32, " "). That way it will become a string[3] format where:

    string[0]: "hello"
    string[1]: "nice"
    string[2]: "world"

    I hope I explained it right. It's similar to the Split() function in Visual Basic.
    0100001

  5. #5
    terrance11
    Guest
    I can't remember the code off the top of my head, but it's like this:

    string hello = "hello world";

    int i = hello.find("world");

    cout << i << endl;

    i would cout a 6, h being a 0;

    then,

    hello.erase(i, 5);

    would erase the 5 characters world, after i characters erase the next 5 characters.

  6. #6
    terrance11
    Guest
    Originally posted by SubLogic
    Parsing. Example, I have a string: "hello nice world" and wish to parse it using the space character (asc:32, " "). That way it will become a string[3] format where:

    string[0]: "hello"
    string[1]: "nice"
    string[2]: "world"

    I hope I explained it right. It's similar to the Split() function in Visual Basic.
    string[int value]- returns a character, not a string. To return a string, you'd have to push the string into a vector.

    so "hello nice world"

    could be broken up like this, excuse me if I have errors, I'm doing it off the top of my head:

    string hello = "hello nice world";

    hello.find(" ");

    string one;
    one.assign(hello, i, hello.size());

    hello.erase(i, hello.size() - i);

    string two = hello;

    hello.find(" ");

    hello.erase(i, hello.size()- i);

    string three = hello;

    something like that...

  7. #7
    terrrance
    Guest
    Originally posted by terrance11
    string[int value]- returns a character, not a string. To return a string, you'd have to push the string into a vector.

    so "hello nice world"

    could be broken up like this, excuse me if I have errors, I'm doing it off the top of my head:

    string hello = "hello nice world";

    hello.find(" ");

    string one;
    one.assign(hello, i, hello.size());

    hello.erase(i, hello.size() - i);

    string two = hello;

    hello.find(" ");

    hello.erase(i, hello.size()- i);

    string three = hello;

    something like that...

    sorry, it should be: hello.erase(0, i), not (i, hello.size() - i);

  8. #8
    Registered User SubLogic's Avatar
    Join Date
    Jan 2003
    Posts
    33
    Humm. Thanks for all the examples, though I didn't really understand how the first one works. I'll try to scroll through string.h and find some of its functions and how to use them.
    0100001

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Since you mentioned string.h, I'm assuming that you're using a C string. In that case the easiest way to go about this simple parse is with the strtok function:
    Code:
    #include <iostream>
    #include <cstring>
    
    int main()
    {
      char a[] = "hello nice world";
    
      for ( char *sep = strtok ( a, " " );
            sep != 0;
            sep = strtok ( NULL, " " ) )
      {
        std::cout<< sep <<std::endl;
      }
    }
    -Prelude
    My best code is written with the delete key.

  10. #10
    Registered User SubLogic's Avatar
    Join Date
    Jan 2003
    Posts
    33
    Ok. I got the base thing you're trying to explain. I played around with the first example u gave me and found out that the .erase parameters are: .erase(from where, to where) so I modified the code like this:

    Code:
    #include <iostream>
    #include <conio.h>
    #include <string>
    
    using namespace std;
    
    int main()
    {
      string mystring = "hello world";
      int i = mystring.find("world");
      mystring.erase(0, i);
      cout << "\"" << mystring << "\"" << endl;
      getch();
      return 0;
    }
    And guess what... Everything came out just as I expected, my output: "world"
    0100001

  11. #11
    Registered User SubLogic's Avatar
    Join Date
    Jan 2003
    Posts
    33
    Originally posted by Prelude
    Since you mentioned string.h, I'm assuming that you're using a C string. In that case the easiest way to go about this simple parse is with the strtok function:
    Code:
    #include <iostream>
    #include <cstring>
    
    int main()
    {
      char a[] = "hello nice world";
    
      for ( char *sep = strtok ( a, " " );
            sep != 0;
            sep = strtok ( NULL, " " ) )
      {
        std::cout<< sep <<std::endl;
      }
    }
    -Prelude
    I am using string.h, but I'm not using cstrings. C++ strings are much easier to manipulate and finally parse
    0100001

  12. #12
    terrance11
    Guest

    Unhappy

    sorry, anyways, I don't have a compiler with me.

    //Please ignore my posts


    I need a book for this, but:

    Code:
    string hello = "hello nice world";
    
    string one = hello;
    one.find(" ");
    
    one.erase(i, hello.size() - i);
    would get you the hello, then, you can get the rest doing the same.

    Probably a quicker way, but I can't remember

  13. #13
    terrance11
    Guest

    Talking

    just ignore all my post altogether.

    sorry can't edit under guest.

  14. #14
    Registered User SubLogic's Avatar
    Join Date
    Jan 2003
    Posts
    33
    Originally posted by terrance11
    sorry, anyways, I don't have a compiler with me.

    //Please ignore my posts


    I need a book for this, but:

    Code:
    string hello = "hello nice world";
    
    string one = hello;
    one.find(" ");
    
    one.erase(i, hello.size() - i);
    would get you the hello, then, you can get the rest doing the same.

    Probably a quicker way, but I can't remember
    Just one easy little question, will hello.size() return the number of characters in the whole string?
    0100001

  15. #15
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I am using string.h, but I'm not using cstrings. C++ strings are much easier to manipulate and finally parse
    No, you're using <string>, not <string.h>. The two are completely different.
    Code:
    #include <iostream>
    #include <vector>
    #include <string>
    
    int main()
    {
      std::string s = "hello nice world";
      std::vector<std::string> vec;
    
      int start = 0, end = 0;
    
      while ( end != std::string::npos ) {
        end = s.find ( ' ', start );
    
        vec.push_back ( s.substr ( start, end - start ) );
    
        start = end + 1;
      }
    
      for ( int i = 0; i < vec.size(); i++ ) {
        std::cout<< vec[i] <<std::endl;
      }
    }
    >will hello.size() return the number of characters in the whole string?
    Yes.

    -Prelude
    My best code is written with the delete key.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. parsing command line strings
    By John_L in forum C Programming
    Replies: 15
    Last Post: 05-28-2008, 09:26 AM
  2. sscanf and parsing strings
    By jco1323 in forum C Programming
    Replies: 4
    Last Post: 02-20-2008, 06:32 PM
  3. Parsing Strings
    By Hunter2 in forum C++ Programming
    Replies: 29
    Last Post: 12-05-2004, 07:48 PM
  4. Searching and Comparing Strings Using Parsing
    By niroopan in forum C++ Programming
    Replies: 3
    Last Post: 09-28-2002, 11:18 AM
  5. parsing delimited strings
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 11-08-2001, 12:57 PM

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