String replacing

Started by
13 comments, last by Acharis 9 years, 1 month ago

I don't want to reinvent the wheel, probably std::string or something else has some support for this? I prefer to not use heavier addons like boost (preferred STL).

I have a string "Something \n\n something \n", I want to replace all "\n" (2 chars) to \n (ASCII value), so I can print the text with proper newline.

Stellar Monarch (4X, turn based, released): GDN forum topic - Twitter - Facebook - YouTube

Advertisement

A regular expression can do the job for you; it's in the <regex> header.

auto string = std::string("string\\nwith\\nembedded\\nnew-lines");
auto pattern = std::regex("\\\\n");
auto result = std::regex_replace(string, pattern, "\n");

std::cout << string << std::endl;
std::cout << result << std::endl;

The sub-string you want to search for is "\n", but the slash has to be escaped in the regular expression since the slash is a special character, so the actual regular expression pattern becomes "\\n". The two slashes has to be escaped again in the source code since they are special characters to the C++ compiler as well. Hence, the four back-slashes.

std::string has a replace function. You will have to find the position(s) of the text you want to replace first using one of the find funtions.


edit: Ninja'd. ph34r.png

Brother Bob's version required C++11 and I would recommend you use his version if you can. It looks cleaner than using std::string's replace, especially if you need to take care of multiple occurrences of the replaced text.

If your compiler supports it (it's a c++11 addition), you could look into regex, particularly regex_replace. It has exactly the capability you want to find substrings and to replace those substrings with something else.


#include <iostream>
#include <string>
#include <regex>

int main()
{
    std::string s("Something \\n\\n something \\na");
    std::regex e("\\\\n");
    std::string result = std::regex_replace(s, e, "\n");
    std::cout << result;
}

the regex object needs all of those slashes. First to convert the c++ escape sequence to a slash, and second because the regex object then takes those slashes and converts them to its own escape sequence, so you need 4 slashes to get it to come out right.

edit: Whoops, I shouldn't have dawdled.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

They were actually added back in TR1. So even if you have an older compiler -- say Visual Studio 2008 -- you can access them in the std::tr1:: namespace.

But yes, regular expressions are powerful.


I have a string "Something \n\n something \n", I want to replace all "\n" (2 chars) to \n (ASCII value), so I can print the text with proper newline.

Can you describe the particular situation where your quoted string doesn't print properly? I.e., what print function doesn't recognize "\n" as an escape sequence?

I.e.,


std::string temp = "Something \n\n something \n";
size_t tempLen = temp.length(); // tempLen = 24, not 27
std::cout << temp.c_str();
// results in output
Something

 something
// as expected

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.


Can you describe the particular situation where your quoted string doesn't print properly? I.e., what print function doesn't recognize "\n" as an escape sequence?
It's loaded from file, so the actual string in C++ would be "Something \\n\\n something \\n".

Stellar Monarch (4X, turn based, released): GDN forum topic - Twitter - Facebook - YouTube

They were actually added back in TR1. So even if you have an older compiler -- say Visual Studio 2008 -- you can access them in the std::tr1:: namespace.

Though three years ago, MinGW's/GCC's Regex support was non-existent, and I'm not sure when it was implemented, so older versions of GCC and MinGW would likely not have support, std::tr1:: or otherwise.

Reading this thread reminded me of a funny quote:

Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.


I'm not sure when it was implemented, so older versions of GCC and MinGW would likely not have support, std::tr1:: or otherwise.

GCC 4.9 or later. You could do basic RE matching starting GCC 4.3 but we didn't land a functional regex_replace until GCC 4.9 when a GSoC student implemented it as a project.

Stephen M. Webb
Professional Free Software Developer

This topic is closed to new replies.

Advertisement