CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: string question

  1. #1
    Join Date
    Apr 2001
    Posts
    1,029

    string question

    Hello all,

    say I have a string that may or may not have whitespace at the front and looks something like this:

    "test 1055 message blah blah"

    I want to put the first word into a string and the second part into an int, and the rest of the message into another string.

    so I would have:

    string name = "test"
    int number = 1055
    string message "message blah blah blah"

    how can I do this??? Sample code would be appreciated!

    Thanks!

  2. #2
    Join Date
    Sep 2004
    Posts
    561

    Re: string question

    Code:
    string name = "test";
    int number = 1055; 
    string message = "message blah blah blah";
    string textMessage;
    stringstream ss; 
    ss << name << " " << number << " " << message;
    textMessage = ss.str();
    cout << textMessage;
    EDIT: Need to include "#include <sstream>"

  3. #3
    Join Date
    Apr 2001
    Posts
    1,029

    Re: string question

    sorry I think you mis read my question. I am not trying to build a string here, but rather extract the data from the string.

  4. #4
    Join Date
    Sep 2004
    Posts
    561

    Re: string question

    Ahh...sorry my mistake.

    Code:
    string textMessage = "test 1055 message blah blah blah";
    string name;
    int number; 
    string message, word;
     
    stringstream ss(textMessage); 
    ss >> name >> number; 
    while( ss >> word )
    {
       message += word + " ";
    }
    cout << name << " " << number << " " << message;

  5. #5
    Join Date
    Apr 2001
    Posts
    1,029

    Re: string question

    Is there a nicer way to do that last part? Because my whitespace is not guarenteed to be a single space everytime.

    Thanks!

  6. #6
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,718

    Re: string question

    use getline() after reading in the number, and then remove
    leading blanks from the remaining message ...

    Code:
    #include <string>
    #include <sstream>
    #include <iostream>
    
    using namespace std;
    
    namespace
    {
        void TrimLeft(std::string& str, const char* chars2remove)
        {
            str.erase(0,str.find_first_not_of(chars2remove));
        }
    }
    
    
    int main()
    {
        string textMessage = "  test 1055 message blah blah blah";
    
        string name;
        int number; 
        string message;
     
        stringstream ss(textMessage); 
        ss >> name >> number; 
        getline(ss,message);
    
        TrimLeft(message," \t");  // optionally (depends on what you want)
    
        cout << name    << "\n";
        cout << number  << "\n";
        cout << message << "\n";
    
        return 0;
    }

  7. #7
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,718

    Re: string question

    And without using a trim-type of function

    Code:
        string name;
        int number; 
        string message , message2;
     
        stringstream ss(textMessage); 
        ss >> name >> number >> message; 
        getline(ss,message2);
    
        message += message2;

  8. #8
    Join Date
    Apr 2001
    Posts
    1,029

    Re: string question

    thanks so much!!!!!!!!!!

  9. #9
    Join Date
    Apr 2001
    Posts
    1,029

    Re: string question

    hmm.. actually something went wrong for me.. here is my simple code:

    string data = "0 test message here blah blah";

    stringstream ss(data);
    ss >> iMessadeID;

    if(iMessadeID == 0)
    {
    string sFilename;
    string sMessage;

    ss >> sFilename;
    getline(ss, sMessage);
    }

    sMessage was blank for some reason, but the Message ID and the Filename worked.. any idea why the message didnt work?

  10. #10
    Join Date
    Apr 2001
    Posts
    1,029

    Re: string question

    oh I see why... we only do a getline()

    I need to read in the entire buffer which may contain end of line characters!

    Any idea on the best way to do this?

    Thanks!

  11. #11
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,718

    Re: string question

    1) sMessage will not be blank in your example

    2) Is this what you want : in Smessage, you want everything
    past the filename, inclusing end-of-line-characters ? If so,
    you can specify what "end-of-line" means. getline() has
    a third argument that specifies when to stop reading. You
    can specify a character that will notr be in the string (such as NULL
    or 255). If all characters are possible, you will need to loop on
    getline() , appending the result to sMessage (and adding "\n");

    Code:
    ss >> sFilename; 
    getline(ss, sMessage,'\255');
    If this is not what you want, could you post the actual
    specifications that you are trying to code ?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)