Ali_dotNet
Ali_dotNet

Reputation: 3279

remove the '\' character from a string

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?

Upvotes: 0

Views: 381

Answers (5)

Jason Evans
Jason Evans

Reputation: 29186

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.

Upvotes: 7

Neil_M
Neil_M

Reputation: 482

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

Upvotes: 2

Sachin Kainth
Sachin Kainth

Reputation: 46740

You have to escape the escape character:

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

Upvotes: 4

Axxelsian
Axxelsian

Reputation: 807

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.

Upvotes: 3

eyossi
eyossi

Reputation: 4330

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

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

Upvotes: 12

Related Questions