views:

600

answers:

1

I have a problem identical to this problem here.

I even want to encode the same infromation as him (it's a date/time for asp.net)...

When ever I try to add a backslash i get two backslashes since I used \.

Everyone in the thread above has claimed that this is a problem with NSLog and that NSString does tread \\ as a \. I have checked this further by using a packet sniffer to examine the packets I'm sending to the webserver and I can confirm that it is transmitting a double backslash instead of a single backslash.

Does anyone know how to add a backslash to a NSString?

+1  A: 

The strings and NSLog are working fine for me:

NSLog(@"\\"); // output is one backslash
NSLog(@"\\\\"); // output is two backslashes
NSLog(@"\\/Date(100034234)\\/"); // output is \/Date(100034234)\/

What am I missing?

gerry3
Hi Gerry,Have you tried it?NSLog(@"\\/Date(1234534534)\\/"); outputs a double backslash for me.More importantly when i transmit that string over http the double backslash appears in the packet
Craig Warren
It works for me (I copied the exact code from your comment). I am running iPhone SDK 3.1.2 and Xcode 3.2.1.
gerry3
On my machine NSLog(@"\\ %@", [NSString stringWithFormat: @"\\n \\\\t"]) correctly outputs "\ \n \\t"
Costique
Gerry, I am using iPhone SDK 3.1.2 and XCode 3.1.4.The only difference in what I do is that I am saving my string to a pointer before displaying: NSString* dateString = [NSString stringWithFormat:@"\\/Date(%ld +0000)\\/", (long)temp*1000];But I definetly get two backslashes
Craig Warren
That code also works fine for me. I set temp to 2331 and the output was: \/Date(2331000 +0000)\/
gerry3
Looks like it's something to do with the JSON parser I'm using... Oh Well thanks for your help. Always good to be challanged by someone until you realize you are wrong
Craig Warren
No problem. I use TouchJSON, but I haven't tested it with escaped backslashes. I am pretty sure the guy that recommended it to me used ASP.NET on his web server.
gerry3