I have 20 lines of code that replaces multiple values of NSString
as below.
[resultstring
replaceOccurrencesOfString:@"&"
withString:@"&"
options:NSLiteralSearch
range:NSMakeRange(0, [resultstring length])
];
The problem is that it is takes some noticeable amount of time (20ms) for executing the code.
Is there any better approach to write the NSString
replacement code.
replaceOccurrencesOfString:...
will traverse the string each time you call it.An alternative which should make the replacement faster is to manually traverse the string just once and build a new string along the way.
Here's an example, although there's probably still room for further optimization
Also if your specific issue is to escape XML entities, this question may be useful: Objective C HTML escape/unescape