Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

prepend NSString?

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.

like image 593
Slee Avatar asked Jun 29 '10 12:06

Slee


2 Answers

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.

like image 56
dreamlax Avatar answered Oct 21 '22 13:10

dreamlax


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"
like image 24
Thomas C. G. de Vilhena Avatar answered Oct 21 '22 14:10

Thomas C. G. de Vilhena