0

I have a html/xml document that is originally a CString and I want to get rid of all the newlines, essentially put everything into one line. I've tried converting it to std::String and using:

#include <algorithm>
#include <string>

str.erase(std::remove(str.begin(), str.end(), '\n'), str.end());

But it didn't work.

minusatwelfth
  • 191
  • 6
  • 14
  • if you got a char* (c-string as I interpreted from your topic), why not move along every char and use something like this: `if(string[i] == '\n') string[i] = ' ';` not exactly how a book would describe good code (as there are probably special functions for such purpose) but it would be a working fix. – Sim Jul 31 '12 at 10:25
  • This is the CString I'm talking about: http://msdn.microsoft.com/en-us/library/ms174288.aspx – minusatwelfth Jul 31 '12 at 10:28
  • on windows the newline delimiter is not "\n" but "\r\n". – Tobias Schlegel Jul 31 '12 at 10:28
  • Unless you want to replace the newlines with another character (space for example) there isn't any method that does that. What you can do is loop over every character in the string, appending them to another string except if the current character is the newline, when done you have a new string containing no newlines. – Some programmer dude Jul 31 '12 at 10:29
  • 1
    It seems I might have been wrong... What about the [`Remove`](http://msdn.microsoft.com/en-us/library/ds4931ye.aspx) function? – Some programmer dude Jul 31 '12 at 10:31
  • What you have works: https://ideone.com/Q3KfV – hmjd Jul 31 '12 at 10:32
  • try without converting FROM CString http://msdn.microsoft.com/en-us/library/aa300579(v=vs.60).aspx – loler Jul 31 '12 at 10:35
  • @TobiasSchlegel, I converted the CString to std::string then used that erase function above, str.erase(std::remove(str.begin(), str.end(), '\r\n'), str.end()); But it still didn't work. – minusatwelfth Jul 31 '12 at 10:35
  • @loler, I tried that and it didn't work lol. – minusatwelfth Jul 31 '12 at 10:38
  • @user1564795, you will need to passes over `str`: one with `'\r'` and one with `'\n'`. – hmjd Jul 31 '12 at 10:38
  • @hmjd, I don't understand what you mean, could you please explain a little further? – minusatwelfth Jul 31 '12 at 10:41
  • @user1564795: `str.erase(std::remove(str.begin(), str.end(), '\n'), str.end());` and `str.erase(std::remove(str.begin(), str.end(), '\r'), str.end());` because `'\r\n'` is a multi-byte character constant, with an implementation defined value. – hmjd Jul 31 '12 at 10:43

3 Answers3

4

In order to stop your block of text looking odd, you'd want to replace the line breaks with a space. Make sure to replace both newline('\n') and carriage return('\r') characters.

CString str = "Line 1 Windows Style\r\n Line 2 Unix Style\n Line 3";
str.Replace('\r', " ");
str.Replace('\n', " ");
str.Replace("  ", " ");
2

You need only to use the method remove

CString str = _T("Test newline \nremove"), str2;
str.Remove('\n');
0

How about?

str.Replace("\n", "");

Documented here

ThirdOne
  • 1,227
  • 9
  • 13