Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

split std::string into vector of std::string

Status
Not open for further replies.

mangocinn

Programmer
Jul 26, 2001
66
US
How is this done in C++?

I have std::string that has newline characters in it.
I want to split the string into a vector of strings. I want to split everytime I see the newline character.

How can this be done?

Thanks.
 
hmm....its been a while since I've worked with C++, is there not a split command? If not, you could always check the string character by character and grab the substrings yourself. Are you reading it in from a file? If so, you can just read to the newlines and put each section into a vector slot.

Just an idea or two, good luck,
Kevin

- "The truth hurts, maybe not as much as jumping on a bicycle with no seat, but it hurts.
 
Kevin,

There is no split function to my knowledge. Visual Basic had one and that is what I did when I coded with VB.

I am new to the C++ world. No, I am not reading it from a file. It is coming from an edit box on a dialog. However, this could be very large.
 
One solution:
Put the original string into a istringstream (in <sstream>). Use getline() to get one line at a time and place it into the vector.

Another solution:
In a loop, use the find() function to find the newline character and use substr() to get a substring containing the current line out of the original string to put into the vector.

Here's an example of the first solution. You could probably use an algorithm to do it in one line of code, but you get the idea.
Code:
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>

int main()
{
    std::string data("Using istringstream\
                     \nmakes splitting a string easy\
                     \nwhen in C++");
    std::istringstream origStream(data);

    std::vector<std::string> lineList;
    std::string curLine;
    while (std::getline(origStream, curLine))
    {
        // Only add non empty lines.
        if (!curLine.empty())
            lineList.push_back(curLine);
    }

    // Output vector.
    std::copy(lineList.begin(), lineList.end(),
        std::ostream_iterator<std::string>(std::cout, "\n"));
}
 
UOLJ is right, that is a pretty good way to handle it.

sorry I didn't notice any responses until after he/she had already posted.

Anyway, good luck,
Kevin

- "The truth hurts, maybe not as much as jumping on a bicycle with no seat, but it hurts.
 
Ok, the code the uolj posted above works... however I am running int another problem related to this.

It appears that the lines of a CRichEditCtrl are not separated by the newline character (\n). I assumed they were....but found out that is not the case. So, what is the character that separates the lines of the text in the control?

I am using the code posted above by uolj... but with the following change:
myRichEditCtrl.SetSel(0, -1);
data = myRichEditCtrl.GetSelText();

I want break this string by the lines and place it in a vector. My problem now is that the "\n" does not appear to be the characer for the new line in the CRichEditCtrl.

I would be greatful if someone can help or offer a suggestion.

Thank you so much.
 
I think the CRichEditCtrl uses either "\r" or "\r\n" as it's line breaks. If you are using my code, try changing the getline call to this:
Code:
    while (std::getline(origStream, curLine[red], '\r'[/red]))
No guarantees, but it might work without any other tweaking.
 
Thank you very very much uolj. It works beautifully. I had been trying '\r\n' and that was not working.

THANK YOU!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top