Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing a single occurrence of string Obj-c

I am trying to replace only one of the occurrences of a NSString with another string. I know that you can use stringByReplacing*Occurrences*OfString; however, that replaces all occurrences. What I am looking for is more like stringByReplacing*Occurrence*OfString. If anyone could help me that would be great. Thanks, Jnani

like image 353
Jnani Avatar asked Sep 03 '11 22:09

Jnani


1 Answers

Something like this?

NSRange location = [someString rangeOfString:stringToReplace];
NSString* result = [someString stringByReplacingCharactersInRange:location withString:stringToReplaceWith];
like image 94
SVD Avatar answered Nov 15 '22 07:11

SVD