-6

How can I remove this character \ from CString?

For Example: I have string with this content "this is\a string"

How can I remove \ from my string?

Thank you very much.

Swifty
  • 839
  • 2
  • 15
  • 40
Hieu Le
  • 79
  • 2
  • 8
  • 1
    https://meta.stackoverflow.com/questions/252677/when-is-it-justifiable-to-downvote-a-question – ayrusme Jan 08 '18 at 12:17
  • Does it make [\a](https://stackoverflow.com/questions/4060601/make-sounds-beep-with-c) sound? – user1810087 Jan 08 '18 at 12:18
  • 3
    Your question is unclear. Does the string print to `std::cout` as `this is \a string` (i.e. the backslash and the 'a' are visible on screen) or as `this is string` (with the backslash and and the 'a' not visible? There are different solutions for each case. – Peter Jan 08 '18 at 12:31
  • 1
    You remove the `\\` character from a string like you would remove any other character from a string. This is a trivial operation, exposed through the [CSting::Remove](https://learn.microsoft.com/en-us/cpp/atl-mfc-shared/reference/cstringt-class#remove) class member. – IInspectable Jan 08 '18 at 13:51
  • 1
    @IInspectable He may not realise that `\` is a prefix for formatting codes like `\t`. So iif he wants to delete a `\` he probably has to use `\\`. But I know you know that! :) – Andrew Truckle Jan 08 '18 at 14:29
  • 1
    Please [edit] your question to show [the code you have so far](http://whathaveyoutried.com). You should include at least an outline (but preferably a [mcve]) of the code that you are having problems with, then we can try to help with the specific problem. You should also read [ask]. – Toby Speight Jan 08 '18 at 16:57
  • This looks like an [XY Problem](http://xyproblem.info/) – Jabberwocky Jan 08 '18 at 17:43
  • Thank you all for your support. Sorry for my question is not clear. Actually, I have 2 problem here. The 1st is: Trying to remove this '\' from a string "\" ", I had try many ways like remove(), delete() but it does not work. The 2nd is: How can I declare CString like this: CString = _T(" include "abc.h" "); It always built false with not correct form. But I dont know how to solve this problem. Thank you very much. – Hieu Le Jan 09 '18 at 04:30
  • I found my problem. The string ' \" ' when print to window only ' " ' will be printed. So I do not need to remove '\' any more. My problem is solved. Thank you so much, I understood this issue. – Hieu Le Jan 09 '18 at 06:56

2 Answers2

5

It's simple for a CString:

cstr.Remove('\\');
Blacktempel
  • 3,935
  • 3
  • 29
  • 53
-4
  #include <algorithm>
  ...
  // Use replace with substitute a space in place of the \ char.
  std::replace(my_string.begin(), my_string.end(), '\\', ' ');
  // or remove it all together.
  std::remove(my_string.begin(), my_string.end(), '\\')
Sorin
  • 11,863
  • 22
  • 26