Answered by:
Inserting a \ (backslash) into a String?

Question
-
User1688746251 posted
Hi all,
I am writing some code that takes to Strings and concatenates them together to form a string in the following format:
1 string strCode1 = "X345"; 2 string strCode2 = "X678"; 3 4 //Attempt #1 Output: X345\\X678 5 string strFinalCode = strCode1 + "\" strCode2; 6 7 8 //Attempt #2 Output: X345\\X678 9 string strFinalCode = strCode1 + @"\" + strCode2; 10 11 12 //Attempt #3 Output: X345\\X678 13 char c = (char)92; 14 string strFinalCode = strCode1 + c + strCode2; 15 strFinalCode = strFinalCode.Replace(@"\\", @"\"); I have also tried using a StringBuilder with no success.
Is this possible to write a \ in a a string ? [:S]
Regards,
Seán
Friday, June 13, 2008 7:45 AM
Answers
-
User1191518856 posted
To write a \ into a string you should do like this:
string strFinalCode = strCode1 + "\\" + strCode2;
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, June 13, 2008 7:49 AM -
User1191518856 posted
Note, the debugger will show the contents escaped, i.e. if there is one backslash then the debugger will show it as \\
But if you print the value then it should show as only one \
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, June 13, 2008 7:51 AM
All replies
-
User1191518856 posted
To write a \ into a string you should do like this:
string strFinalCode = strCode1 + "\\" + strCode2;
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, June 13, 2008 7:49 AM -
User1191518856 posted
Note, the debugger will show the contents escaped, i.e. if there is one backslash then the debugger will show it as \\
But if you print the value then it should show as only one \
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, June 13, 2008 7:51 AM -
User1688746251 posted
This strFinalCode is being returned (of type string of course) from a method I wrote.
Which in turn is inserted into another string as part of a SQL command.The string being returned is still in the wrong format i.e. two backslashes. e.g. x456\\x456
Suggestions welcome.
SeánFriday, June 13, 2008 8:04 AM -
User1485238302 posted
I tried this and it displays a single '\' instead of '\\'.
string strCode1 = "X345"; string strCode2 = "X678"; string strFinalCode = strCode1 + "\\" + strCode2; Console.Write(strFinalCode); Console.Read();
I have tried returning the value and shows the correct value. Can you post your final code?Friday, June 13, 2008 8:33 AM -
User187056398 posted
As Johram pointed out, a single backslash is displayed as a double backslash in the debugger.
Both these methods work:
string strFinalCode = strCode1 + @"\" + strCode2;
string strFinalCode = strCode1 + "\\" + strCode2;You can validate this by opening up a Debug/Windows/Memory window and type in strFinalCode for the address.
You will see something like this...note there is only one backslash (5c)
0x0158A45C c4 d8 0f 79 0a 00 00 00 09 00 00 00 58 00 33 00 34 00 35 00 5c ÄØ.y........X.3.4.5.\ 0x0158A471 00 58 00 36 00 37 00 38 00 00 00 00 00 00 00 00 00 00 00 00 00 .X.6.7.8............. 0x0158A486 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 .....................
Friday, June 13, 2008 8:42 AM -
User47344791 posted
string strFinalCode = strCode1 + @"\" + strCode2;
string strFinalCode = strCode1 + "\\" + strCode2
both gives the same output :X345\X678
but in debugger it shows "\\"
Friday, June 13, 2008 9:01 AM -
User1688746251 posted
I have validated that the string is being returned correctly.
However,
When I call the method in question it puts the double backslash back in.Here is some code continuing on from the above method I mentioned..
1 string SQL = @"INSERT INTO TABLEX (...., Code ,.....) VALUES (....., {CODE}, .....)"; 2 SQL = SQL.Replace("{CODE}", @P.GetCode()); 3 ClassToExecutoSQLStmt(@SQL);
My question now is, to eliminate this double-backslash should I use the @ everywhere the string 'Code' [x000\x000] is used?
As my SQL command still contains this double backslash even though I have verified that the return value is indeed a single backslash.Friday, June 13, 2008 9:19 AM -
User242249085 posted
should I use the @ everywhere the stringThat is a question of style. But makes no difference to the strings in memory.
When I call the method in question it puts the double backslash back in.How are you checking? If in the debugger (as noted multiple times in this thread), it shows strings like that would be shown in code, so a single backslash is shown doubled, a newline is shown as "\n" etc..
Saturday, June 14, 2008 8:32 AM