views:

949

answers:

1

How would I use deleteCharactersInRange to remove the first character of a NSMutableString?

+3  A: 

It's not that hard...

NSMutableString *a = [NSMutableString stringWithString:@"aString"];
NSRange range;
range.location = 0;
range.length = 1;
[a deleteCharactersInRange:range];

You can shorten the range creation like this:

NSRange range = {0,1}; // edit: of course 0,1 instead of 1,0, thanks Omar
drvdijk
Thanks for the help. It worked but I have to point out that to remove the first character, it would be: NSRange range = {0,1};notNSRange range = {1,0};
Omar
It's just as simple (and arguably better practice?) to use NSMakeRange(), an inline function defined by Cocoa. For example: NSRange range = NSMakeRange(0,1); Either way works, although this idiom is a common practice and is more likely to be recognized broadly.
Quinn Taylor