Add backslash to String in Objective-c

前端 未结 2 1191
庸人自扰
庸人自扰 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)\/"}

提交回复
热议问题