In this variable, i would like to add some \ before every '.
string html =
"<a href=\"annee-prochaine.html\">Calendrier de l'annee prochaine</a>"
html = html.Replace("'", "\'"); //No change
html = html.Replace("\'", "\'"); //No change
html = html.Replace("\'", "\\'");
//html => <a href=\"annee-prochaine.html\">Calendrier de l\\'annee prochaine</a>
html = html.Replace("\'", @"\'");
//html => <a href=\"annee-prochaine.html\">Calendrier de l\\'annee prochaine</a>
I would like to get that after Replace :
//html => <a href=\"annee-prochaine.html\">Calendrier de l\'annee prochaine</a>
Any ideas ?
Thanks!
Algorithm to Replace string in C Accept the string, with which to replace the sub-string. If the substring is not present in the main string, terminate the program. If the substring is present in the main string then continue.
The fastest way would be to allocate a new string that is strlen (s) - strlen (word) + strlen (rpwrd) + 1 . Then use the strstr function to find the word to be replaced and copy up to that point into a new string, append the new word, then copy the rest of the original sentence into a new string.
No you can still use the object allocated on stack.
I strongly suspect that you're looking at the strings in the debugger, which is why you're seeing doubled backslashes.
This version is absolutely fine:
html = html.Replace("\'", "\\'");
(The one using the verbatim string literal would be fine too.) Rather than looking at it in the debugger, log it or just serve it, and everything should be fine.
The fact that you're seeing it for the double-quote as well is further evidence of this. For example, this string:
string html = "<a href=\"anne...";
... does not contain a backslash, but your diagnostics are showing it, which is what I'd expect in a debugger.
The backslash character is an escape character, so you either need to put 2 of them, or use the @ string modifier which ignores escaping.
html=html.Replace("'", "\\'"); // this should work
html=html.Replace("'", @"\'"); // or this
string html = "<a href=\"annee-prochaine.html\">Calendrier de l'annee prochaine</a>"
html = html.Replace("'",@"\'");
Try this:
html=html.Replace("'", @"\'");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With