Backward Search in Objective-c

 NSString *string = @"Here is an example string HELLO ";
 NSRange rangeToSearch = NSMakeRange(0, [string length] - 1); // get a range without the space character
 NSRange rangeOfSecondToLastSpace = [string rangeOfString:@" " 
options:NSBackwardsSearch range:rangeToSearch];
 NSString *spaceReplacement = @" text that i want ";
 NSString *result = [string
 stringByReplacingCharactersInRange:rangeOfSecondToLastSpace
 withString:spaceReplacement];
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s