Thread: Chopping up strings

  1. #1
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986

    Chopping up strings

    Hey everyone this is just a quick question.

    I have a string that looks like this
    Code:
    string Name = "Hello everyone\n\nMy name is stovellp";
    I want to break it up so I have "Hello everyone" in one string, and "My name is stovellp" in the other.

    If anyone could point me on the trail for what functions I should look for I would really appreciate it.
    Last edited by nickname_changed; 07-16-2003 at 02:40 AM.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    boost::string_tokenizer from www.boost.org
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Thanks for the reply CornedBee, I've looked at boost in the past but I'm not too trusting with downloading and configuring external libraries (Because I hate to read documentation mostly).

    Before your reply I ended up writing a function of my own to do it just using a counter.

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    This should work,
    Code:
    #include <iostream>
    #include <string>
    #include <stdlib.h>
    
    int main()
    {
          string Name = "Hello everyone\n\nMy name is stovellp";
          string newName, newName2;
          int loc = Name.find("\n\n",0);
    
          if (loc != string::npos)
          {
           newName = Name.substr(0, loc);
           newName2 = Name.substr( (loc + 2), Name.length() );
          }
          else
              cout << "Couldnt find \\n\\n" << endl;
    
          cout << newName << endl;
          cout << newName2 << endl;
    
          system("PAUSE");
          return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Problems with strings as key in STL maps
    By all_names_taken in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:34 AM
  4. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM