Instance Method


Replaces all occurrences of a given string in a given range with another given string, returning the number of replacements.


- (NSUInteger)replaceOccurrencesOfString:(NSString *)target withString:(NSString *)replacement options:(NSStringCompareOptions)options range:(NSRange)searchRange;



The string to replace.


The string with which to replace target.


A mask specifying search options. See String Programming Guide for details.

If opts is NSBackwardsSearch, the search is done from the end of the range. If opts is NSAnchoredSearch, only anchored (but potentially multiple) instances are replaced. NSLiteralSearch and NSCaseInsensitiveSearch also apply.


The range of characters to replace. aRange must not exceed the bounds of the receiver. Specify searchRange as NSMakeRange(0, [receiver length]) to process the entire string.

Return Value

The number of replacements made.


This method treats the length of the string as a valid range value that returns an empty string.

See Also

Modifying a String

- appendFormat:

Adds a constructed string to the receiver.

- appendString:

Adds to the end of the receiver the characters of a given string.

- applyTransform:reverse:range:updatedRange:

Transliterates the receiver by applying a specified ICU string transform.

- deleteCharactersInRange:

Removes from the receiver the characters in a given range.

- insertString:atIndex:

Inserts into the receiver the characters of a given string at a given location.

- replaceCharactersInRange:withString:

Replaces the characters from aRange with those in aString.

- setString:

Replaces the characters of the receiver with those in a given string.