replacing " within a string

  • Thread starter Thread starter Dan
  • Start date Start date
D

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
 
Dan said:
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
 
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
 
I've tried your code, and:

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

Works fine.
Probably the error's somewhere else.

Niki
 
hi,

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

use this thing.
 
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.
 
Sreejith S S Nair said:
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.
 
Back
Top