We have introduced the addition method and query method before. Now we will introduce the subtraction method. As the name suggests, the subtraction method definitely means to remove a part from the original string and then form a new string.
The method involved here:
Deletecharactersinrange:PassNsmakerangeReturned location and lengthDeletecharacterinrangeThe number of objects to be deleted from the beginning.
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 deleteCharactersInRange:NSMakeRange(3, 1)]; //NSMakeRange返回一个位置和一个长度, 告诉deleteCharactersInRange //要从哪一个字符开始删, 删除的长度是多少. //删除后打印一次结果对比 NSLog(@"\n%@", str); } return 0;}
Output result:
2014-10-11 16:57:09.014 DeleteNSMutableString[1605:303] 01234567892014-10-11 16:57:09.015 DeleteNSMutableString[1605:303] 012456789Program ended with exit code: 0
Nsstring form -- variable string -- Subtraction Method Delete