NSMutableString *str = [[NSMutableString alloc] init];
[str appendString: @"ab cd efghil mnopq rstuvz"];
//my test
[str deleteCharactersInRange:NSMakeRange(2, [str length] -1)];
我會提取前3個字符。提取前3個字符到可變字符串
NSMutableString *str = [[NSMutableString alloc] init];
[str appendString: @"ab cd efghil mnopq rstuvz"];
//my test
[str deleteCharactersInRange:NSMakeRange(2, [str length] -1)];
我會提取前3個字符。提取前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)];
由於您對答案不滿意,因此不清楚您在問什麼。 – Benjie