Simple! How to replace " in .NET with something...?
That won't work, should be @"\"" and the R should be uppercase
Robert Greiner
2010-09-16 17:56:14
@Robert, you're right about the R, which I fixed, but not the @.
Matthew Flaschen
2010-09-16 17:57:49
@Robert Greiner - The @ is not necessary
Jeremy
2010-09-16 17:58:43
@Robert - with the @ you escape with double quotes instead: `Replace(@"""",//...`
Rudu
2010-09-16 17:59:24
Right, my mistake. I was thinking of Regex.
Robert Greiner
2010-09-16 18:00:43
Nah...it's part of an answer. But not extremely helpful, as it doesn't include any context.
cHao
2010-09-16 18:04:10
+3
A:
Something like this:
var st = "String with \" in it.";
st.Replace('\"', 'c'); // replacement char
Nate Bross
2010-09-16 17:55:50
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
2010-09-16 18:06:37
+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
2010-09-16 17:56:33
+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
2010-09-16 17:56:41
Not sure why this was down-voted. It works perfectly as you will see if you run the code.
Steve Michelotti
2010-09-16 17:59:49
Isn't he escaping the double quote? And so, we don't need the `@` or the double escaping.
Floyd Pink
2010-09-16 18:00:50
@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
2010-09-16 18:03:59
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
2010-09-16 18:04:15
+3
A:
string newValue = "quote \"here\"".Replace("\"", "'");
Or
string newValue = @"quote ""here""".Replace(@"""", "'");
John Fisher
2010-09-16 17:57:10
@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
2010-09-17 15:54:55
@John Fisher - Oh, of course. I put 5 quotes by accident. I meant to say you forgot the `@` sign.
Kobi
2010-09-17 18:22:14
+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
2010-09-16 18:00:08
@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
2010-09-16 18:02:12
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
2010-09-16 18:01:21