We have explained the first three methods of string deformation. Now we are talking about the last method. The method is similar to the method of overwriting, but it is slightly different from the overwrite method. It is not directly covered by another definition, but can be overwritten by modifying the position and length of the original string.
The following methods are involved:
Replacecharactersinrange:It means to modify the original string through the position and length returned by nsmakerange.
The following is a simple example:
#import <Foundation/Foundation.h>int main(int argc, const char * argv[]) { @autoreleasepool { NSMutableString *str = [NSMutableString new]; [str setString:@"0123456789"]; NSLog(@"\n%@", str); [str replaceCharactersInRange:NSMakeRange(3, 5) withString:@"abcdefg"]; //通过一个NSMakeRange返回的位置和长度, 把abcdefg这个字符串从第三个, 长度为5的字符串改成abcdefg. //通俗点说就是把34567这五个改成abcdefg, 而后面的89就会跟在abcdefg后面. NSLog(@"\n%@", str); } return 0;}
Output results before modification:
2014-10-11 17:04:48.799 AlterNSMutableString[1647:303] str = 0123456789Program ended with exit code: 0
Output result after modification:
2014-10-11 17:04:48.801 AlterNSMutableString[1647:303] str = 012abcdefg89Program ended with exit code: 0
Nsstring form -- variable string -- alter Method