Question

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 treat \\ 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?

Was it helpful?

Solution

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?

OTHER TIPS

Try this:

yourStr =  [yourStr stringByReplacingOccurrencesOfString:@"\\\\" withString:@"\\"];
NSLog(@"%@", yourStr);

I had the same problem, turned out that my JSON Parser replaced all occurrances of "\\" with "\\\\", so when I NSLogged my original code like this:

NSString *jsonString = [myJSONStuff JSONRepresentation];
NSLog(@"%@", jsonString);

This is what I got:

{TimeStamp : "\\/Date(12345678)\\/"}

However, the string itself contained FOUR backslashes (but only 2 of them are printed by NSLog).

This is what helped me:

NSString *jsonString = [myJSONStuff JSONRepresentation];
jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\\\\" withString:@"\\"];
NSLog(@"%@", jsonString);

The result:

{TimeStamp : "\/Date(12345678)\/"}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top