Skip to content

Instantly share code, notes, and snippets.

@SONIC3D
Created August 4, 2012 17:31
Show Gist options
  • Save SONIC3D/3258865 to your computer and use it in GitHub Desktop.
Save SONIC3D/3258865 to your computer and use it in GitHub Desktop.
NSMutableString Code Snippet
// life cycle
NSMutableString *s1 = [NSMutableString stringWithCapacity:10];
NSMutableString *s2 = [[NSMutableString alloc] initWithCapacity:10];
[s2 release];
// fill string
[s1 appendFormat:@"%@ Objective-C!", @"Hello"];
NSLog(@"%@", s1);
// append string
[s1 appendString:@" Glad to be here."];
NSLog(@"%@", s1);
// delete characters
[s1 deleteCharactersInRange:NSMakeRange(s1.length-6, 6)];
NSLog(@"%@", s1);
// insert
[s1 insertString:@" alive!" atIndex:s1.length];
NSLog(@"%@", s1);
// replace
[s1 replaceCharactersInRange:NSMakeRange(19, 4) withString:@"Excited"];
NSLog(@"%@", s1);
// replace occurrence
[s1 replaceOccurrencesOfString:@"Objective-C" withString:@"World" options:NSCaseInsensitiveSearch range:NSMakeRange(0, s1.length)];
NSLog(@"%@", s1);
// replace the entire string
[s1 setString:@"This is a new String"];
NSLog(@"%@", s1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment