Add backslash to String in Objective-c

前端 未结 2 1185
庸人自扰
庸人自扰 2020-12-07 04:12

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

相关标签:
2条回答
  • 2020-12-07 04:49

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

    0 讨论(0)
  • 2020-12-07 04:54

    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?

    0 讨论(0)
提交回复
热议问题