I am thinking about some details regarding
stringByReplacingOccurrencesOfString:withString: in NSString class
According to document.
It returns a new string in which all occurrences of a target string in the receiver are replaced by another given string.
- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement
Parameters
target
The string to replace.
replacement
The string with which to replace target.
my question is that if the replacement string is not found in the target string. If there would be some side effects to call this function.
Thanks
For example
NSString *myString = [NSString stringWithString:@"Hello my string"];
NSString *myReplacementString = [myString stringByReplacingOccurrencesOfString:@"NOTFOUND" withString@"Any side effect?"];
NSLog(@"my replacement string is %@", myReplacementString);
There are no side effects: if the substring to be replaced doesn’t occur in the original string, you end up with an
NSStringidentical to the one you started with.Indeed, (this is an implementation detail and you shouldn’t depend on it, but) you don’t even end up with a copy of the original string, but a pointer to the same exact string.