Large STL String into smaller chunks

This is a discussion on Large STL String into smaller chunks within the C++ Programming forums, part of the General Programming Boards category; Okay, I have tried. I know it is still a little rough, but before I go any farther, anyone know ...

  1. #1
    Registered User KeithS's Avatar
    Join Date
    Jul 2009
    Location
    Colombia
    Posts
    21

    Large STL String into smaller chunks

    Okay, I have tried. I know it is still a little rough, but before I go any farther, anyone know a different or better way?

    Code:
    #include <string>
    #include <vector>
    #include <iostream>
    
    using namespace std;
    
    size_t pos;
    
    int main()
    {
    
        string long_string("Hi! I am a string that exceeds twenty characters in length. I need to be cut up into smaller strings");
        vector<string> small_string;
        string string_temp;
        string::iterator it;
        int i;
    
        while(long_string.length() > 20)
        {
            it = long_string.begin() + 20;
            if(*it != ' ')
            {
                for(i = 19; i > 0; i--)
                {
                    it = long_string.begin() + i;
                    if(*it == ' ')
                    {
                        string_temp.assign(long_string.begin(), it);
                        long_string.erase(long_string.begin(), it);
                        small_string.push_back(string_temp);
                        break;
                    }
                }
            }
            else
            {
                string_temp.assign(long_string.begin(), it);
                long_string.erase(long_string.begin(), it);
                small_string.push_back(string_temp);
            }
    
            if(long_string.length() < 20)
            {
                string_temp.assign(long_string.begin(), long_string.end());
                long_string.erase(long_string.begin(), long_string.end());
                small_string.push_back(string_temp);
                break;
            }
        }
    
        size_t str_z = small_string.size();
    
        for(i = 0; i < (int)str_z; i++)
        {
            cout << small_string.at((size_t)i) << endl;
        }
        return 0;
    }
    I am sure you get the idea. Thanks in advance for any suggestions.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,415
    I could read your code and figure out what exactly does it do, but why not just tell us how you intend to split the string into smaller chunks? For example, chunks of a particular maximum size? A specific number of chunks? Chunks split according to certain delimiters found in the string?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I leave it to you to decide whether
    Code:
    long_string.find_last_of(' ', 20);
    meets your needs.

  4. #4
    Registered User KeithS's Avatar
    Join Date
    Jul 2009
    Location
    Colombia
    Posts
    21
    Right you are. Thanks.

    What I want to do is find the nearest space to the end of each 20 character long segment of the string and cut the string there, make a new smaller string with that (with a <string>vector.push_back) and then take the next section of 20 characters as from there, and run the same test for the space, until the remaining part of the original string is less than 20 characters long. The last part gets pushed into the last vector.

    If that is not very clear, the best I can think of is to say think "word wrap", 20 characters long maximum, in this case.

    EDIT:
    Thanks, tabstop, was not sure how to use that. Clear(er) now.
    Last edited by KeithS; 05-23-2011 at 12:22 PM.

  5. #5
    Registered User xentaka's Avatar
    Join Date
    May 2011
    Posts
    60
    Here's what I came up with to cut the line off at 20 chars and store it inside of a string array.

    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    int main() {
        string myText("Hi! I am a string that exceeds twenty characters in length. I need to be cut up into smaller strings");
    
        int countString = 0, startT = 0, stringLen = myText.length();
        vector<string> tmpStor(10);
    
        while ( (startT + 20) < stringLen ) {
            tmpStor[countString] = myText.substr(startT,20);
            cout << tmpStor[countString] << endl;
            countString++;
            startT += 20;
        }
        tmpStor[countString] = myText.substr(startT);
    
        //Complete Line
        int i = 0;
        while (tmpStor[i].length() > 0) {
            cout << tmpStor[i];
            i++;
        }
    
        return 0;
    }
    Last edited by xentaka; 05-23-2011 at 02:30 PM. Reason: Made code more complete
    "The people don't want war, but they can always be brought to the bidding of the leaders. This is easy. All you have to do is tell them they are being attacked, and denounce the pacifists for lack of patriotism and for exposing the country to danger. It works the same in every country." - Hermann Goering.

  6. #6
    Registered User xentaka's Avatar
    Join Date
    May 2011
    Posts
    60

    Erp

    Just realized you probably don't want to cut words off ... how bout this.

    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    int main() {
        string myText("Hi! I am a string that exceeds twenty characters in length. I need to be cut up into smaller strings");
    
        int countString = 0, startT = 0, stringLen = myText.length();
        vector<string> tmpStor(10);
    
        while ( (startT + 20) < stringLen ) {
            int tmpStr;
            tmpStor[countString] = myText.substr(startT, 20);
            tmpStr = tmpStor[countString].find_last_of(' ', (startT + 20));
            if ( tmpStr < 19 ) {
                tmpStor[countString] = myText.substr(startT, tmpStr);
                startT += tmpStr;
            } else {
                startT +=  20;
            }
            cout << tmpStor[countString] << endl;
            countString++;
        }
        tmpStor[countString] = myText.substr(startT);
        //Complete Line
        int i = 0;
        while (tmpStor[i].length() > 0) {
            cout << tmpStor[i];
            i++;
        }
    
        return 0;
    }
    "The people don't want war, but they can always be brought to the bidding of the leaders. This is easy. All you have to do is tell them they are being attacked, and denounce the pacifists for lack of patriotism and for exposing the country to danger. It works the same in every country." - Hermann Goering.

  7. #7
    Registered User KeithS's Avatar
    Join Date
    Jul 2009
    Location
    Colombia
    Posts
    21
    I have already managed to figure out something along those lines, xentaka, but thank you very much for the suggestion just the same!

    Consider this one solved, thanks again, and all the best.

    For posterity and reference... (I have a poor memory)

    Code:
    #include <string>
    #include <vector>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    
        string long_string("Hi! I am a string that exceeds twenty characters in length. I need to be cut up into smaller strings");
        vector<string> small_string;
        string string_temp;
        //string::iterator it;
        size_t i;
        size_t marker_1 = 0, marker_2 = 0;
    
        while((marker_1 + 20) < long_string.length())
        {
            marker_2 = long_string.find_last_of(' ', (marker_1 + 20));
            string_temp.assign(long_string.begin() + marker_1, long_string.begin() + marker_2);
            if(*string_temp.begin() == ' ')
                string_temp.erase(string_temp.begin());
    
            small_string.push_back(string_temp);
            marker_1 = marker_2;
        }
        if((long_string.length() - marker_1) > 0)
        {
            string_temp.assign(long_string.begin() + marker_1, long_string.end());
            if(*string_temp.begin() == ' ')
                string_temp.erase(string_temp.begin());
    
            small_string.push_back(string_temp);
        }
    
        size_t sl_z = small_string.size();
    
        for(i = 0; i < sl_z; i++)
        {
            cout << small_string.at(i) << endl;
        }
        return 0;
    }
    Last edited by KeithS; 05-24-2011 at 09:25 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Splitting string in chunks of 2 characters
    By ScoutDavid in forum C++ Programming
    Replies: 8
    Last Post: 03-27-2011, 05:34 AM
  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. Problem collecting large string of data
    By gkoenig in forum C Programming
    Replies: 3
    Last Post: 03-01-2008, 04:34 PM
  4. Large String Comparisions...
    By alvifarooq in forum C++ Programming
    Replies: 10
    Last Post: 05-30-2005, 03:54 AM
  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

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