Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Addition of extra " while using .Replace method in C#

Tags:

c#

My problem is when i try to add a url to a link, an extra " is always appended at the end of the string.

Current faulty code :

Label add_url = "<a href=\"/test/\">link</a>"; // produces link pointing to /test/

Label rep_url = add_url.Replace("\"/", "http://mysite.com/"); // produces a faulty link which points to http://mysite.com/test/" <- NOTE! EXTRA " HERE

Am i missing something? please help.

like image 725
Fckya Overtime Avatar asked Dec 04 '25 19:12

Fckya Overtime


2 Answers

You probably missed the open quote, since you are replacing it:

Label rep_url = add_url.Replace("\"/", "\"http://mysite.com/"); 
like image 185
Tallmaris Avatar answered Dec 07 '25 07:12

Tallmaris


The trailing quote is fine, it should be there. It's the one after the equal sign that is missing.

The imbalance of quotes happens because you are removing the initial quote, not because you are inserting a trailing one:

Label rep_url = add_url.Replace("\"/", "\"http://mysite.com/"); 
like image 38
Sergey Kalinichenko Avatar answered Dec 07 '25 08:12

Sergey Kalinichenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!