Add backslash to String in Objective-c

11,359

Solution 1

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?

Solution 2

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)\/"}

Share:
11,359
Craig Warren
Author by

Craig Warren

I am a Software Engineer based in Scotland, working in London. I primarily work with Android/Java but have experience with the Microsoft .Net technology stack, including an MCTS in ASP.Net. I also occasionally build simply websites in PHP. Scripting in Python, Ruby and Perl. Plenty of experience with SQL database (Oracle, MS and MySQL) used for relational data and data warehousing. I graduated from Heriot Watt University in Edinburgh with a MEng Distinction in Software Engineering.

Updated on June 04, 2022

Comments

  • Craig Warren
    Craig Warren almost 2 years

    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?

  • Craig Warren
    Craig Warren over 14 years
    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
  • gerry3
    gerry3 over 14 years
    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.
  • Costique
    Costique over 14 years
    On my machine NSLog(@"\\ %@", [NSString stringWithFormat: @"\\n \\\\t"]) correctly outputs "\ \n \\t"
  • Craig Warren
    Craig Warren over 14 years
    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
  • gerry3
    gerry3 over 14 years
    That code also works fine for me. I set temp to 2331 and the output was: \/Date(2331000 +0000)\/
  • Craig Warren
    Craig Warren over 14 years
    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
  • gerry3
    gerry3 over 14 years
    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.
  • Muhammad Yusuf
    Muhammad Yusuf about 8 years
    This case happened to me too when converting from NSDictionary that contains NSString objects with backslashes in it to an NSString.