Splitting a huge string in several smaller strings

This is a discussion on Splitting a huge string in several smaller strings within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, this is my problem: I have a .txt file with a huge string of names, seperated like this: ...

  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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,528
    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 12:10 PM.
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  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, 08:24 PM
  2. How to check if a string contains a smaller string?
    By Blasz in forum C Programming
    Replies: 7
    Last Post: 05-16-2010, 12:33 AM
  3. splitting strings...
    By newbie_socketsp in forum C Programming
    Replies: 4
    Last Post: 08-15-2008, 11: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, 11:32 PM

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