Sunday, June 2, 2013

NSMutableString deleteCharactersInRange example in Objective C (iOS).

NSMutableString deleteCharactersInRange

Removes from the receiver the characters in a given range.

- (void)deleteCharactersInRange:(NSRange)aRange

The range of characters to delete. aRange must not exceed the bounds of the receiver.
Important: Raises an NSRangeException if any part of aRange lies beyond the end of the string.

Discussion of [NSMutableString deleteCharactersInRange]
This method treats the length of the string as a valid range value that returns an empty string.

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

Example of [NSMutableString deleteCharactersInRange].
NSMutableString *string = [NSMutableString stringWithString:@"-21.256"];
NSLog(@"%@", string);
[string deleteCharactersInRange:NSMakeRange(0, 1)];
NSLog(@"%@", string);
results in:


NSMutableString deleteCharactersInRange example.
ou could use deleteCharactersInRange:

NSMutableString *str = [NSMutableString stringWithString:@"FooBarx"];
[str deleteCharactersInRange:NSMakeRange([str length]-1, 1)];

End of NSMutableString deleteCharactersInRange example article.