By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,836 Members | 2,097 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,836 IT Pros & Developers. It's quick & easy.

replacing " within a string

P: n/a
Dan
I wish to replace all the occurrances of " with " within a string.

I have tried using myString.Replace("\"", """), but this does not work

Any suggestions will be greatly appreciated

Thanks

Nov 16 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Dan wrote:
I wish to replace all the occurrances of " with " within a string.

I have tried using myString.Replace("\"", """), but this does not work


Do not make the mistake to do this:

string foo = "lalalla\"";
foo.Replace("\"", """);

this will not change 'foo'. do:

string foo = "lalalla\"";
foo = foo.Replace("\"", """);

FB

--
Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
My .NET Blog: http://weblogs.asp.net/fbouma
Microsoft C# MVP
Nov 16 '05 #2

P: n/a
Does not work? Could you be a bit more specific? Runtime error or compile
error?

I assume it's a runtime error as the code seems fine.

are you forgetting to assign the result? myString = myString.Replace(...);

Lars Moastuen

On Fri, 6 Aug 2004 12:35:34 +0100, Dan <dan.parker@_nospam_pro-bel.com>
wrote:
I wish to replace all the occurrances of " with &quot; within a string.

I have tried using myString.Replace("\"", "&quot;"), but this does not
work

Any suggestions will be greatly appreciated

Thanks


--
Using Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 16 '05 #3

P: n/a
I've tried your code, and:

String myString = "Hello \"World\"";
Console.WriteLine(myString);
Console.WriteLine(myString.Replace("\"", "&quot;"));

Works fine.
Probably the error's somewhere else.

Niki
"Dan" <dan.parker@_nospam_pro-bel.com> wrote in
news:Oc*************@TK2MSFTNGP12.phx.gbl...
I wish to replace all the occurrances of " with &quot; within a string.

I have tried using myString.Replace("\"", "&quot;"), but this does not work
Any suggestions will be greatly appreciated

Thanks

Nov 16 '05 #4

P: n/a
hi,

string mystring ="asdasdasd\"\"asdasd\"asdsd\"";
mystring.Replace("\"","&quot");

use this thing.
Nov 16 '05 #5

P: n/a
hi,
i am really sorry. my previous post is wrong.

this is the correct answer.

In your code you are trying to replace few characters. That's fine.

myString.Replace("\"", """),

But you need to know one importent thing in String class is that

string is immutable. That means if you try to update one variable of that
type string it won't update the real value.

string SSS=myString.Replace("\"", """),

Now you will get the result on SSS.
If you try to print the value of myString it won't change the old value.

Try this example and try to view the value of myString after printing SSS.

Nov 16 '05 #6

P: n/a
Sreejith S S Nair <Sr************@discussions.microsoft.com> wrote:
string mystring ="asdasdasd\"\"asdasd\"asdsd\"";
mystring.Replace("\"","&quot");

use this thing.


No, don't. The second line is effectively a no-op, because you're not
using the returned value. Calling Replace doesn't change the actual
string itself, it returns a new string with the replacement having been
performed.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.