[SOLVED] C++ Remove new line from multiline string

Issue

Whats the most efficient way of removing a ‘newline’ from a std::string?

Solution

#include <algorithm>
#include <string>

std::string str;

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

The behavior of std::remove may not quite be what you’d expect. See an explanation of it here.

Answered By – luke

Answer Checked By – Marilyn (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *