Break up text at newline

This is a discussion on Break up text at newline within the C++ Programming forums, part of the General Programming Boards category; I'm trying to take text from a multiline text input and break it up to into string Address[7] but to ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    27

    [SOLVED] Break up text at newline

    I'm trying to take text from a multiline text input and break it up to into
    string Address[7] but to do this I need to know how to break the text up at \n

    so if I have "Hello world\nI am mikey\nThis is line 3"
    I need to break it up to fill the different indexes

    So far I have this but need to join beginning and end.

    Code:
    void _labelguiFrame::OnButton2Click(wxCommandEvent& event)
    {
        //Get address from box
        wxString instring = AddressTextIn->GetValue();
        //Process address to insert spacings at beginning of lines
        //Break up string to lines
        string address[7];
        //!!!More code here...!!!
    
        //Reform with added spaces
        //str length for labels is 26, so 3+22+3
        string linebegin = "   ";
        string printstring;
        int x = 0;
        int y = 7;
        while (x<y) {
            //Address output here
            if ( x < y - 1 ) {
                printstring = printstring + linebegin + address[x] + "\n";
            }
            else {
                printstring = printstring + linebegin + address[x];
            }
            cout<<linebegin<<address[x]<<endl;
            x++;
        };
        //Address to the printer
        //Setup printer
        ofstream print;
        print.open("lpt1:", ios::out);
        //cout<<printstring;
        print<<"\x1b\x43\x09";
        print<<printstring;
        print<<"\x1b\x0c\x1b\x07\x1b\x07";
        print.close();
        wxMessageBox(_("The label has printed.\nPlease peel it off and press\nreturn when label has been ejected."));
        //End print job by resetting printer, and pulling back paper.
        print.open("lpt1:", ios::out);
        print<<"\x1b\x40";
        print.close();
    }
    Last edited by mikeyp; 12-13-2009 at 01:28 PM. Reason: Debugged code + solved

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Well. You could use stringstreams...
    Code:
    #include <sstream>
    #include <string>
    #include <iostream>
    
    int main( int argc, char *argv[] ) { 
      // Set up the stream
      std::stringstream ss( "one two\nthree\nfour" );
      std::string l;
    
      // Two means of extracting the stuff
      std::vector<std::string> vecStrings;
      while ( std::getline(ss,l) )
        vecStrings.push_back( l );
    
      // Print the results
      std::copy( vecStrings.begin(), vecStrings.end(),
        std::ostream_iterator<std::string>( std::cout, "|" ) );
    
      return 0;
    }
    /*My output:
    one two|three|four|
    */
    Provided you can get the miltiline string as a std::string or char* it should work

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Without reading any of your apparently long code and just focusing on this:
    so if I have "Hello world\nI am mikey\nThis is line 3"
    I need to break it up to fill the different indexes
    So do you essentially have one long string with the few "\n"s and have to break up the string by "\n"s? Cant you just search for the first occurrence of "\n" and get a substring of it until that position? Then do this again, from 1 + where the previous "\n" was found. Continue until no more "\n"s are found. Then each iteration you have access to some substring that ends with "\n", and therefore can do whatever you want with it.

    Not sure if thats what your asking, I didnt fully understand.

  4. #4
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    I personally use Boost.

    Stack Overflow is your friend (lol, now with Google, you have two):

    C++: How to split a string? - Stack Overflow
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  5. #5
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    The advantage of getline'ing from the stringstream over >>'ing is that you may add a third parameter to split according with a different deliminator. In fact >>'ing doesn't solve the problem in this case -- he wants to break on a new line and I believe the >> operator will stop at whitespace. All whitespace. Not just a new line.

  6. #6
    Registered User
    Join Date
    Nov 2009
    Posts
    27
    Thank you, after working out how to convert wxString to string, processing the text with stringstream and getline worked!

    Cheers!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  2. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  4. Keypress reading
    By geek@02 in forum Windows Programming
    Replies: 1
    Last Post: 06-16-2004, 12:16 PM
  5. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 06:48 PM

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