2

I need to replace all instances of "" with "null" in my string.

I was going to use

 result.Replace("", ""); 

but I cant seem to find a way to specify the two ".

3
  • 1
    As a note that will only be obvious to those that already know it: string.Replace returns a string and does not make the change to result itself (in the example code). Commented Nov 28, 2011 at 18:45
  • Interesting. I headed straight for string.Empty, but that doesn't work. You can't say result.Replace(string.Empty, "null"). Commented Nov 28, 2011 at 18:45
  • are you trying to replace the string with null as in the null or are you trying to get "null" as the string? Commented Nov 28, 2011 at 18:53

2 Answers 2

14

Escape quotes:

result.Replace("\"\"", "\"null\""); // will return a new string

[edit] Forgot the quotes in the replacement.

Sign up to request clarification or add additional context in comments.

Comments

9

Use the \ escape character:

result.Replace("\"\"", "\"null\"");

2 Comments

I don't imagine they want "null" stored, at least the question doesn't read that way.
Yes I did want null inside the quotes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.