1

My string:

std::string With_esc = "asd\b";

I want to convert it to a simple string "as" (apply the backspace character and forget it). Is there any way to do this in C++? It should look like this:

std::string With_esc = "asd\b";
std::string Without_esc = With_esc; //Here I should convert it
std::ofstream FWith_esc ("with");
std::ofstream FWithout_esc ("without");
FWithout_esc << Without_esc;
FWith_esc << With_esc;

Bash:

~ cat -e with
  asd^H
~ cat -e without
  as

Unfortunately I don't know how to convert it, so both files look exactly the same.

RJFalconer
  • 10,890
  • 5
  • 51
  • 66
Tomasz Kasperczyk
  • 1,991
  • 3
  • 22
  • 43
  • best chance is to do a search'n'replace yourself, it shouldn't be too hard – Marco A. Jul 30 '14 at 13:19
  • 1
    I'd go with a regex "(.\\b)" to replace with "" – Adriano Repetti Jul 30 '14 at 13:20
  • 1
    @MarcoA. Well replacing the '\b' character with '' will not consume it. The string variable will still contain "asd" instead of "as". – Tomasz Kasperczyk Jul 30 '14 at 13:22
  • @AdrianoRepetti Would you want `"aa\b\b"` to become `"a\b"`, or do you want it to become `""`? –  Jul 30 '14 at 13:23
  • @user3125731 if the backspace character is the only one you need to deal with you can take care of the previous digit as well. If not then it might be a problem (or a lot of work). There might be a better solution I don't see right now (except passing stuff in a bash shell) – Marco A. Jul 30 '14 at 13:23
  • And what should happen if the `'\b'` is the first character of the string? – James Kanze Jul 30 '14 at 13:24
  • @hvd good question...I assumed just one escape otherwise he may need looping (or a better regex, I'm really not such good with them) – Adriano Repetti Jul 30 '14 at 13:24

3 Answers3

4

Assuming you're lucky enough to use C++11 (otherwise adapt this code to your favorite regex engine):

string With_esc = R"asd\b";
string Without_esc = regex_replace(With_esc, regex(".\\b"), "");

As pointed out in comments this approach has following limitations:

  • It'll replace only one "back" so if you have "12\b\b" you'll get "1\b". To handle this you need to loop until input and output (for regex_replace()) are different (or maybe a better regex, I'm not such good with them).
  • It won't handle \b as beginning of the string like in "\b123". To handle this you need a simple string replacement (using technique suggested by Giobunny) to remove \b after a regex_replace().
  • Note here I'm using verbatim strings (it's ok for an example if your text comes from a file but if you're working with string literals you need to update regex accordingly).

UPDATE
As noted by Eric Finn this expression will also match multiple backspaces then "a\b\b\b" will become "\b\b" and then "\", that's obviously wrong. As he suggested a better regex should include check for "[^\b]\b" too.

Community
  • 1
  • 1
Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208
2

Try something like this, if you don't want to use a regex:

std::string convertBackspaces(std:string str)
{
    std::string::iterator iter = str.begin();
    std::string::iterator end = str.end();
    while (iter != end)
    {
        iter = std::find(iter, end, '\b');
        if (iter == end) break;
        if (iter == str.begin())
            iter = str.erase(iter);
        else
            iter = str.erase(iter-1, iter+1);
        end = str.end();
    }
    return str;
}

std::string Without_esc = convertBackspaces(With_esc);
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
0

You can replace the substring "\b" with an empty string. It's not the fastest/safest method but it will work, you can follow.

Replace substring with another substring C++

Community
  • 1
  • 1
Giobunny
  • 96
  • 2