VBForums >
.NET >
C# > [RESOLVED] Am I an idiot? Quotation marks in a string?
Click to See Complete Forum and Search --> : [RESOLVED] Am I an idiot? Quotation marks in a string?
brandoom
Mar 11th, 2006, 11:39 PM
I'm trying to put quotes around the <Reason> variable in this string but the string is showing up with the backslashes too. How can I get the quotes in it?
string.Format("banby {0} {1} {2} \"{3}\"", PlayerID, BannedBy, BanTime, Reason)
Rattlerr
Mar 11th, 2006, 11:56 PM
Is this for a Console program or what??
jcis
Mar 12th, 2006, 12:07 AM
string.Format("banby {0} {1} {2} \"{3}\"", PlayerID, BannedBy, BanTime, Reason);
It's working fine here, what kind of variable is Reason? tried string and int, both working fine
brandoom
Mar 12th, 2006, 02:11 AM
This is what the string becomes when it is sent to my function. As you can see, the backslashes are still there? Crazy huh? This is C# code in a DLL.
"banby 0 admin \"MY TEST REASON\""
David Anton
Mar 12th, 2006, 04:59 PM
In VB, you escape a quote with another quote:
e.g., ThisString = "abc""def"
will display as: abc"def
penagate
Mar 12th, 2006, 05:04 PM
Indeed, but this is C# ;)
This is what the string becomes when it is sent to my function. As you can see, the backslashes are still there?
What is the exact string that is passed to your function?
brandoom
Mar 12th, 2006, 05:38 PM
Here you go, I think I already showed it but:
PlayerID = "0"
BannedBy = "admin"
BanTime = "perm"
Reason = "MY TEST REASON"
Those variables get passed into a function that does this:
string.Format("banby {0} {1} {2} \"{3}\"", PlayerID, BannedBy, BanTime, Reason);
And that string gets passed into a new function and the watch on it shows the string as:
"banby 0 admin \"MY TEST REASON\""
penagate
Mar 12th, 2006, 05:41 PM
do a MessageBox.Show() after it and see if it isn't just the watch.
Failing that, you could just concatenate them.
jmcilhinney
Mar 12th, 2006, 05:53 PM
That's the Watch window indicating that the quotes are escaped. Try passing your string to MessageBox.Show and you'll see that it's doing exactly as it should.
Edit: too slow :(
brandoom
Mar 12th, 2006, 06:06 PM
Concating them doesn't work either, at least not using the backslash as an escape sequence. What other methods can I use to concat a quotation mark?
jmcilhinney
Mar 12th, 2006, 06:14 PM
You aren't listening. It is working properly. Pass the formatted string to a call to MessageBox.Show and you'll see that.
jmcilhinney
Mar 12th, 2006, 06:15 PM
Notice that in the Watch window the whole value is enclosed in double quotes to indicate that it's a string. That's why the double quotes within the string are escaped, just like in your code.
brandoom
Mar 12th, 2006, 06:38 PM
Thanks for taking the time to help me guys. I've got it all straightened out now.
jmcilhinney
Mar 12th, 2006, 06:55 PM
Don't forget to resolve your thread from the Thread Tools menu.
jcis
Mar 12th, 2006, 08:45 PM
Oh, I didn't know that Inmediate Window <> MessageBox, so the string is not formatted in the inmediate window, good to know.
I was using a MessageBox, that's why I said it worked fine here.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.