views:

170

answers:

10

Simple! How to replace " in .NET with something...?

+2  A: 
myString.Replace("\"", "something");
Matthew Flaschen
That won't work, should be @"\"" and the R should be uppercase
Robert Greiner
@Robert, you're right about the R, which I fixed, but not the @.
Matthew Flaschen
@Robert Greiner - The @ is not necessary
Jeremy
@Robert - with the @ you escape with double quotes instead: `Replace(@"""",//...`
Rudu
Right, my mistake. I was thinking of Regex.
Robert Greiner
+1  A: 

in c# \"

or in vb.net ""

Joachim VR
Should be a comment
Henk Holterman
Nah...it's part of an answer. But not extremely helpful, as it doesn't include any context.
cHao
+3  A: 

Something like this:

var st = "String with \" in it.";
st.Replace('\"', 'c'); // replacement char
Nate Bross
+2  A: 

Using escape character \. So " becomes \" within a string

Floyd Pink
Works in C#, but not in VB. Pity the OP didn't bother to specify what *language* he was using, as the language is way more important than the platform.
cHao
+2  A: 

I don't understand your question but if escape sequences are ones you are looking for.. in this case to escape " then for C# look here and for vb.net and comparison with C# look in the Strings section of this post.

Misnomer
+1 for more general answer, while avoiding flame war about @
Steve Townsend
+2  A: 

You want to replace a double quote with something?

If you have a string variable:

string sMyText = ".....";

You could replace a double quote with something like this:

sMyText = sMyText.Replace("\"","x");

The slash character \ is an escape character, allowing you to use " inside a string.

Jeremy
+7  A: 
string result = myString.Replace("\"", "foo");
Steve Michelotti
Not sure why this was down-voted. It works perfectly as you will see if you run the code.
Steve Michelotti
Isn't he escaping the double quote? And so, we don't need the `@` or the double escaping.
Floyd Pink
@Steve: Well, I didn't downvote, but it *might* have been downvoted because you don't indicate in your answer that the code as written won't do anything (you'd have to assign the result to something, like `myString = myString.Replace("\"", "foo");`).
Dan Tao
No, `\"` is correct. It is that way in C-like languages to make your brain catch fire when you write string literals with quotes in them. `"My lady, I \"" + verb + "\" your dress!"`
Skurmedel
@Dan Tao - the down-vote was removed.
Steve Michelotti
+3  A: 
string newValue = "quote \"here\"".Replace("\"", "'");

Or

string newValue = @"quote ""here""".Replace(@"""", "'");
John Fisher
Probably should be `@"""""`. That sure is ugly.
Kobi
@Kobi: 5 quotes wouldn't work. - 1 quote starts the string, 2 quotes escapes and puts a quote into the string, and 1 quote closes the string.
John Fisher
@John Fisher - Oh, of course. I put 5 quotes by accident. I meant to say you forgot the `@` sign.
Kobi
@Kobi: Oh, right!
John Fisher
+5  A: 

Oh, you mean you want to replace all occurrences of the string " with something else?

Try this:

public static class EvilStringHelper {
    private static readonly Action<string, int, char> _setChar;
    private static readonly Action<string, int> _setLength;

    static EvilStringHelper() {
        MethodInfo setCharMethod = typeof(string).GetMethod(
            "SetChar",
            BindingFlags.Instance | BindingFlags.NonPublic
        );

        _setChar = (Action<string, int, char>)Delegate.CreateDelegate(typeof(Action<string, int, char>), setCharMethod);

        MethodInfo setLengthMethod = typeof(string).GetMethod(
            "SetLength",
            BindingFlags.Instance | BindingFlags.NonPublic
        );

        _setLength = (Action<string, int>)Delegate.CreateDelegate(typeof(Action<string, int>), setLengthMethod);
    }

    public static void ChangeTo(this string text, string value) {
        _setLength(text, value.Length);
        for (int i = 0; i < value.Length; ++i)
            text.SetChar(i, value[i]);
    }

    public static void SetChar(this string text, int index, char value) {
        _setChar(text, index, value);
    }
}

Usage:

"\"".ChangeTo("Bob");
string test = string.Concat("\"", "Hello!", "\"");
Console.WriteLine(test);

Output:

BobHello!Bob

Note: This was totally a joke.

Dan Tao
@Dan Tao - ooooh. That's even snarkier than my reply. It didn't even occur to me to go so far as to use delegates and reflection.
Joel Etherton
+1 for the evil code :D
Matt Ellen
A: 
char[] str = myString.ToCharArray();
StringBuilder newString = new StringBuilder("");

for(int i = 0;i<str.Length;i++)
{
    if('"'.Equals(str[i])) newString.Append(''); // put your new char here
    else newString.Append(str[i]);
}

myString = newString.ToString();
str = null;

// Don't use this method. This is really stupid. I just felt like being a little snarky.
Joel Etherton
Seriously? A downvote for snark on a question like this? Tsk.
Joel Etherton
At least you're using `StringBuilder` and not just doing `+=` :)
Nate Bross