0

I want to remove the '\' character from my strings.

I've tried several ways but I am still not having any luck

Here is a small piece of my code. It is in fact an HTML obtained from another site.

I'm going to use it at my own site but \ makes problems!

 src=\"http://bartarinha.com/file/logo/ideal.jpg\" style=\"border: 0px none\">

This code gives me error:

news_body_html = news_body_html.Replace("\", " ");

What is the correct way to remove the character?

dplante
  • 2,445
  • 3
  • 21
  • 27
Ali_dotNet
  • 3,219
  • 10
  • 64
  • 115
  • Are you sure there are \ characters or is it just the Visual Studio debugger that shows you the string in C# syntax? – dtb May 23 '12 at 15:06
  • how can I understand it? when I copy paste the text in notepad, there are some instances of this string – Ali_dotNet May 23 '12 at 15:10
  • @Ali_dotNet: Copy paste the text *from where*? – Jon Skeet May 23 '12 at 15:11
  • I go over my string in VS, and copy its text right into notepad, and I have these \ characters, what is the correct way? I want to use this variable for displaying in my ASP.NET DIV – – Ali_dotNet May 23 '12 at 15:19
  • 1
    I think that VS added thoes to the string. They are not actually there. There should be a pulldown at the start of the string when you hover over it that allows you to select a 'visualizer'. Then select the "text visualizer". – user957902 May 23 '12 at 15:27
  • oh yes I think it is the trick! – Ali_dotNet May 23 '12 at 15:31

5 Answers5

12

try news_body_html.Replace("\\", " ");

or news_body_html.Replace(@"\", " ");

eyossi
  • 4,230
  • 22
  • 20
7

Have a go with

news_body_html = news_body_html.Replace("\\", " ");

EDIT:

Actually try this:

news_body_html = news_body_html.Replace('\\', ' ');

Note that I'm using single quotes here around the slashes. I forgot that Replace expects a char as the parameter.

Jason Evans
  • 28,906
  • 14
  • 90
  • 154
4

You have to escape the escape character:

news_body_html = news_body_html.Replace("\\", " ");
Sachin Kainth
  • 45,256
  • 81
  • 201
  • 304
  • @Ali_dotNet: It really should. I strongly suspect your diagnostics are at fault here - for example, are you looking at the string in a debugger? – Jon Skeet May 23 '12 at 15:11
  • yes I go over my string in VS, and copy its text right into notepad, and I have these \ characters, what is the correct way? I want to use this variable for displaying in my ASP.NET DIV – Ali_dotNet May 23 '12 at 15:13
3
news_body_html = news_body_html.Replace("\\", " "); 

That will remove the \ from your code. the \ is a control used most commonly for \n to make a new line, so it was seeing it as a command with nothing to do, therefore doing nothing with it.

Axxelsian
  • 787
  • 3
  • 9
  • 25
2

The follwoing question asks about replacing "-" from a string but the same method should work for your problem.

Just remember that to use \ in a c# string you need to use "\" as it is a single \is an escape character

Community
  • 1
  • 1
Neil_M
  • 462
  • 5
  • 17