Reputation: 28268
I am getting a JSON response from a web service but it is not wrapped by the [] tags required by the JSON parser I am using so I need to Append and Prepend those characters to my NSString before I pass that to the JSON parser.
Here is what I haver so far:
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
responseString = [responseString stringByAppendingFormat:@"]"];
The appending works perfectly now I just need to prepend the [ to this, can't seem to find this method.
Upvotes: 10
Views: 7857
Reputation: 90681
Just for completeness:
responseString = [@"[" stringByAppendingString:responseString];
It sometimes surprises folks that you can message a string literal, until they think about it. ;)
Upvotes: 4
Reputation: 14595
Using a NSMutableString you can do it like this:
NSMutableString *str = [[NSMutableString alloc] initWithString:@"Overflow"];
[str insertString:@"Stack" atIndex:0];
After that the NSMutableString str will hold:
"StackOverflow"
Upvotes: 9
Reputation: 95385
Try this:
responseString = [NSString stringWithFormat:@"[%@]", responseString]
There are other ways of acheiving the same thing, I'm sure others will be able to provide more efficient methods, but if responseString
isn't very large then the above should suffice.
Upvotes: 16