Thread: Splitting a huge string in several smaller strings

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    10

    Splitting a huge string in several smaller strings

    Hello everyone, this is my problem:

    I have a .txt file with a huge string of names, seperated like this:

    "BELLA, "JONATHAN", ...

    I need to operate on each name, so I want to store them in a vector, but without the " and , characters. I've made an algorithm, but it seems there is no easy way to do this:

    - take note of first index of name
    - check for next index until you hit a "
    - cut of the string from index i till i + j

    I thought of storing all seperate characters of a single name in a temporary vector<char>, then compiling those characters in a string with a for loop, is this possible?

    Thanks in advance!

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Here is an easier way:
    1.Use std::getline with the delimiter ',' and looping to the end to get the strings with the quotes, into a std::vector<std::string>.
    2.Use std::string's substr function on each string to prune the quotes.
    (You can easily combine the steps..)
    Last edited by manasij7479; 04-13-2012 at 11:10 AM.

  3. #3
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Code:
    std::vector<std::string> Names;
    boost::split(Names, MyHugeString, boost::is_any_of(","));
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Large STL String into smaller chunks
    By KeithS in forum C++ Programming
    Replies: 6
    Last Post: 05-23-2011, 07:24 PM
  2. How to check if a string contains a smaller string?
    By Blasz in forum C Programming
    Replies: 7
    Last Post: 05-15-2010, 11:33 PM
  3. splitting strings...
    By newbie_socketsp in forum C Programming
    Replies: 4
    Last Post: 08-15-2008, 10:17 AM
  4. splitting strings
    By Unreg1 in forum C++ Programming
    Replies: 3
    Last Post: 12-29-2002, 11:02 PM
  5. how to cut a string into smaller strings?
    By ss3x in forum C++ Programming
    Replies: 7
    Last Post: 04-21-2002, 10:32 PM