Created
August 4, 2012 17:31
-
-
Save SONIC3D/3258865 to your computer and use it in GitHub Desktop.
NSMutableString Code Snippet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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