NSMutableString *str = [[NSMutableString alloc] init];
[str appendString: @"ab cd efghil mnopq rstuvz"];
//my test
[str deleteCharactersInRange:NSMakeRange(2, [str length] -1)];
我会提取前3个字符。
答案 0 :(得分:3)
如果你的问题是你希望你的可变字符串只是前三个字符,那么这应该有用(帽子提示@EmptyStack):
NSMutableString *str = [[NSMutableString alloc] init];
[str appendString: @"ab cd efghil mnopq rstuvz"];
[str setString:[str substringToIndex:2]];
或者,您提出的解决方案并不遥远:
NSMutableString *str = [[NSMutableString alloc] init];
[str appendString: @"ab cd efghil mnopq rstuvz"];
[str deleteCharactersInRange:NSMakeRange(3, [str length] -3)];
如果你有疑问,你想要一个字符串中的前3个字符,剩余部分留在可变字符串中,那么这就是你想要的:
NSMutableString *str = [[NSMutableString alloc] init];
[str appendString: @"ab cd efghil mnopq rstuvz"];
NSMutableString *first3Chars = [[[str substringToIndex:3] mutableCopy] autorelease];
[str deleteCharactersInRange:NSMakeRange(0, 3)];