-2

Say I have a string:

std::string x = "\\asdasd\\sewef\\sdvrhe\\"

How do I remove the double backslahes so that the string only contains single backslahes?

I need x string's value to be:

\asdasd\sewef\sdvrhe\

I am open to replacing chars or removing any method that achieves the desired result will be accepted.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
Enviro
  • 3
  • 1
  • 8
    The way you have written it, `x` *does* only have single backslashes. The double backslash in the string is to escape the backslash – sshashank124 Jan 10 '20 at 07:09
  • Does this answer your question? [using \ in a string as literal instead of an escape](https://stackoverflow.com/questions/12103445/using-in-a-string-as-literal-instead-of-an-escape) – sshashank124 Jan 10 '20 at 07:11
  • Try printing `x` and see what it really contains. – Some programmer dude Jan 10 '20 at 07:14
  • Please clarify your question. `x` shown in your code *does* have the value you are showing. So it is not clear what you want to achieve. – walnut Jan 10 '20 at 07:26
  • Please be more specific. Perhaps you mean that in your code editor you only want to see single slashes? If so look into [raw string literals](https://en.cppreference.com/w/cpp/language/string_literal). `std::string x = R"(\asdasd\sewef\sdvrhe\)"` – acraig5075 Jan 10 '20 at 07:26
  • 1
    @NutCracker Your edit is assuming a lot. Are you sure that was OP's intention? – acraig5075 Jan 10 '20 at 07:45
  • Could not tell for certain... but I believe this is what he meant, just did not express himself in a right way and got confused what is being outputted and what is the true content of C++ string – NutCracker Jan 10 '20 at 07:49
  • @NutCracker It's not really for us to guess what the poster really means or have. If the question needs clarifications then it's up to the original poster to edit the question to improve it, not for us to guess or assume. – Some programmer dude Jan 10 '20 at 07:54
  • 1
    @Enviro Please take some time to read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Then *edit your question* (there's a link right below the tags) to improve it. And if this is a misunderstanding about how backslashes works in string literals, and the contents of `x` really is what you want, then please delete the question. – Some programmer dude Jan 10 '20 at 07:56
  • I agree. Then, it's up to him to edit the question – NutCracker Jan 10 '20 at 07:56

1 Answers1

0

"boost string algo" is your friend:

boost::replace_all(x, "\\\\", "\\");
user23573
  • 2,479
  • 1
  • 17
  • 36