String formatting question
-
I want to format a string with embedded backslashes. The string that I want is as follows: This is John I\. Smith. Here's the code snippet: string temp1 = "John I\\. Smith"; string temp2 = String.Format("This is {0}.", temp); What I get in temp2 is: This is John I\\. Smith. instead of This is John I\. Smith. How do I get rid of the extra backslash? BRCKCC
-
I want to format a string with embedded backslashes. The string that I want is as follows: This is John I\. Smith. Here's the code snippet: string temp1 = "John I\\. Smith"; string temp2 = String.Format("This is {0}.", temp); What I get in temp2 is: This is John I\\. Smith. instead of This is John I\. Smith. How do I get rid of the extra backslash? BRCKCC
BrcKcc wrote: This is John I\\. Smith. Is that in the debugger, or in actual application output?? One easy way to be sure is to use string myString = @"Use\a\slash\only\once"; the @ symbol in front of the string literal tells the compiler not to interpret the slashes as escape sequences. If you do want to include an escape sequence this won't work for you. Cheers, Andy.
-
BrcKcc wrote: This is John I\\. Smith. Is that in the debugger, or in actual application output?? One easy way to be sure is to use string myString = @"Use\a\slash\only\once"; the @ symbol in front of the string literal tells the compiler not to interpret the slashes as escape sequences. If you do want to include an escape sequence this won't work for you. Cheers, Andy.
It was in the debugger. It turns out when I displayed the string via MessageBox.Show(), it only had one backslash. So thanks for that clarification. The @ symbol has been confusing. How do I convert a string defined with the @ symbol to a string without the @ symbol? Do you know where Microsoft documented this capability? Thanks. - Bruce BRCKCC
-
It was in the debugger. It turns out when I displayed the string via MessageBox.Show(), it only had one backslash. So thanks for that clarification. The @ symbol has been confusing. How do I convert a string defined with the @ symbol to a string without the @ symbol? Do you know where Microsoft documented this capability? Thanks. - Bruce BRCKCC
BrcKcc wrote: How do I convert a string defined with the @ symbol to a string without the @ symbol? What do you mean? They are all the same thing: a string object. string a = "String\\Literal"; string b = @"String\Literal"; a==b [EDIT - sorry, I had the slashes the wrong way round before] Cheers, Andy