1

Let's say I have a string that has multiple carriage returns in it, i.e:

394968686
100630382
395950966
335666021

I'm still pretty amateur hour with C++, would anyone be willing to show me how you go about: parsing through each "line" in the string ? So I can do something with it later (add the desired line to a list). I'm guessing using Find("\n") in a loop?

Thanks guys.

MSN
  • 53,214
  • 7
  • 75
  • 105
kogh
  • 995
  • 4
  • 17
  • 30
  • Carriage return (\r) is not newline (\n). Before you write your loop it's pretty important to find out which one (or both) that you have in your string. – john Aug 12 '11 at 16:25

4 Answers4

2
while (!str.IsEmpty())
{
    CString one_line = str.SpanExcluding(_T("\r\n"));
    // do something with one_line
    str = str.Right(str.GetLength() - one_line.GetLength()).TrimLeft(_T("\r\n"));
}

Blank lines will be eliminated with this code, but that's easily corrected if necessary.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
1

You could try it using stringstream. Notice that you can overload the getline method to use any delimeter you want.

string line;
stringstream ss;
ss << yourstring;
while ( getline(ss, line, '\n') )
{
  cout << line << endl;
}

Alternatively you could use the boost library's tokenizer class.

arviman
  • 5,087
  • 41
  • 48
0

If your string is stored in a c-style char* or std::string then you can simply search for \n.

std::string s;
size_t pos = s.find('\n');

You can use string::substr() to get the substring and store it in a list. Pseudo code,

std::string s = " .... ";
for(size_t pos, begin = 0; 
    string::npos != (pos = s.find('\n'));
    begin = ++ pos)
{
  list.push_back(s.substr(begin, pos));
}
iammilind
  • 68,093
  • 33
  • 169
  • 336
0

You can use stringstream class in C++.

#include <iostream>
#include <sstream>
#include <vector>
using namespace std;

int main()
{
   string str = "\
                394968686\
                100630382\
                395950966\
                335666021";
   stringstream ss(str);
   vector<string> v;

   string token;
   // get line by line
   while (ss >> token)
   {
      // insert current line into a std::vector
      v.push_back(token);
      // print out current line
      cout << token << endl;
   }
}

Output of the program above:

394968686
100630382
395950966
335666021

Note that no whitespace will be included in the parsed token, with the use of operator>>. Please refer to comments below.

Eric Z
  • 14,327
  • 7
  • 45
  • 69
  • 1
    or `string line; while (std::getline(ss, line)) {...}` – Sjoerd Aug 12 '11 at 16:28
  • They are not exactly the same. std::getline reads in the whitespace as part of the string as well while overloaded >> won't. It depends on what OP needs though. – Eric Z Aug 12 '11 at 16:33
  • 1
    The question is about MFC's CString, not std::string. Don't see how this answer is relevant. – Mark Ransom Aug 12 '11 at 16:38
  • @Mark, You can always convert a CString to std::string if you want. – Eric Z Aug 12 '11 at 16:47
  • It is ugly transformation from type to type. Why don't you offer converting CString to array of chars (or wchars) then make loop on each character and finally converting back to CString? – George Gaál Aug 12 '11 at 23:10
  • @George, What do you want by "loop on each character"? Are you sure it's related to the OP's problem? – Eric Z Aug 13 '11 at 02:37